#!/bin/bash
#
# ::copy::
# ::maintainer::
#
CMD=`basename $0`

usage()
{
	echo "
usage: $CMD [options] mp3files ...

options:
  --flat	don't create album subdirs (embed album title in filename)
  --noartist	don't embed artist name in filename
  --cdsafe	same as --flat --noartist
  --defaults	use defaults from dirname/filename
  --fix		try to fix ID3 infos when missing (implies --defaults)
  --fixonly	same of --fix, but don't rename files (only fix ID3 infos)

* command output must be piped to shell
" >&2
	exit 1
}



fixname()
{
	echo "$1" | sed \
       		-e 's/^title$//g' \
       		-e 's/^no title$//g' \
       		-e 's/^Napster$//g' \
       		-e 's/_L001//g' \
	       	-e 's/^[ _-]*//g' \
	       	-e 's/[ _-]*$//g' \
		-e 's/ - /, /g' \
		-e 's/^\.$//'
}


[ $# = 0 ] && usage


f_album_in_subdir=true
f_artist=true
f_usedefaults=false
f_fixinfos=false
f_rename=true


#	first try to determine artist and album name from
#	current directory (for defaults, only if dirname
#	has first letter capitalized)
#
default_artist=
default_album=

pwd=`pwd`
pwd=`basename "$pwd"`
case "$pwd" in
	[A-Z]*" - "*)
		default_artist=`echo "$pwd" | sed -e 's/ *- *.*//'`
		default_album=`echo "$pwd" | sed -e 's/.*- *//'`
		echo "#"
		echo "# default artist: $default_artist"
		echo "# default album:  $default_album"
		echo "#"
		;;
	[A-Z]*)
		if [ -f .artist ]
		then
			default_artist=$pwd
		else
			default_album=$pwd
		fi
		echo "#"
		echo "# default artist: $default_artist"
		echo "# default album:  $default_album"
		echo "#"
		;;
esac


for file in "$@"
do
	case "$file" in
		--flat)	f_album_in_subdir=false
			continue
			;;
		--noartist)
			f_artist=false
			continue
			;;
		--cdsafe)
			f_album_in_subdir=false
			f_artist=false
			continue
			;;
		--fix)
			f_usedefaults=true
			f_fixinfos=true
			continue
			;;
		--fixonly)
			f_usedefaults=true
			f_fixinfos=true
			f_rename=false
			;;
		--defaults)
			f_usedefaults=true
			continue
			;;
	esac

	[ -f "$file" ] || continue

	echo
	echo "# processing: '$file'"

	norm=`echo "$file" | sed -e 's/_/ /g' -e 's/  */ /g'`

	info=`mp3info "$file" 2>/dev/null`	# || continue

	case "$info" in
		"")
	       		artist= title= track= album=
			echo "# mp3: no infos"
			;;

		*)
			artist=`mp3info -p "%a" "$file"`
			title=`mp3info -p "%t" "$file"`
			track=`mp3info -p "%n" "$file"`
			album=`mp3info -p "%l" "$file"`
			echo "# mp3: a='$artist' t='$title' n='$track' l='$album'"

			# fixup
			artist=`fixname "$artist"`
			album=`fixname "$album"`
			title=`fixname "$title"`
			echo "# fix: a='$artist' t='$title' n='$track' l='$album'"
		;;
	esac

	
	#	defaults
	#
	$f_usedefaults && {
		f_changed=false
		[ "$artist" ] || {
			[ "$default_artist" ] && {
				artist=`fixname "$default_artist"`
				echo "# no artist, forcing '$artist'"
				f_changed=true
			}
		}
		[ "$album" ] || {
			[ "$default_album" ] && {
				album=`fixname "$default_album"`
				echo "# no album, forcing '$album'"
				f_changed=true
			}
		}

		# missing infos, try to get them from filename, but only
		# if the filename contains at least one ' - '
		#
		echo "$norm" | fgrep -q ' - ' && {
			[ "$artist" ] || {
				artist=`echo "$norm" | sed -e 's/  *- .*//'`
				artist=`fixname "$artist"`
				echo "# no artist, forcing from filename '$artist'"
				f_changed=true
			}

			[ "$track" ] || {
				[ "$album" ] && {
				    echo "$norm" | egrep -q -- "- *[0-9][0-9]* *-" && {
					track=`echo "$file" | sed -e 's/.*- *\([0-9][0-9]*\) *-.*/\1/' -e 's/^0//'`
					[ "$track" ] && {
						echo "# no track, forcing from filename '$track'"
						f_changed=true
					}
				    }
				}
			}
			[ "$title" ] || {
				title=`echo "$norm" | sed -e 's/.* - *//' -e 's/\.mp3//' -e 's/ *- *$track//'`
				title=`fixname "$title"`
				echo "# no title, forcing from filename '$title'"
				f_changed=true
			}
		} # grep ' - '


		$f_fixinfos && {
			$f_changed && {
				echo "# fixing ID3 infos ..."
				printf 'mp3info -a "%s" -l "%s" -t "%s" -n "%s" -c "" "%s"\n' \
		       			"$artist" "$album" "$title" "$track" "$file"
			}
		}
	}


	[ "$artist" ] || {
		$f_artist && {
			echo "# ERROR no artist : $file"
			echo "ERR-NO-ARTIST $file" >&2
			continue
		}
	}
	[ "$title" ] || {
		echo "# ERROR no title : $file"
		echo "ERR-NO-TITLE $file" >&2
		continue
	}

	[ "$track" ] && {
		track=`printf %02d $track`
	}

	if $f_album_in_subdir
	then
		dir=
		if [ "$track" -a "$album" ]
		then
			if $f_artist
			then
				dir="$artist - $album"
			else
				dir="$album"
			fi
			new="$dir<SLASH>$track - $title.mp3"
		elif [ "$album" ]
		then
			if $f_artist
			then
				dir="$artist - $album"
			else
				dir="$album"
			fi
			new="$dir<SLASH>$title.mp3"
		else
			new="$title.mp3"
		fi
		[ "$dir" ] && {
			[ -d "$dir" ] || echo mkdir \"$dir\"
		}
	else
		if [ "$track" -a "$album" ]
		then
			if $f_artist
			then
				new="$artist - $album - $track - $title.mp3"
			else
				new="$album - $track - $title.mp3"
			fi
		elif [ "$album" ]
		then
			if $f_artist
			then
				new="$artist - $album - $title.mp3"
			else
				new="$album - $title.mp3"
			fi
		else
			if $f_artist
			then
				new="$artist - $title.mp3"
			else
				new="$title.mp3"
			fi
		fi
	fi

	$f_rename || continue

	[ "$new" != "$file" ] || continue

	file=`echo "$file" | sed -e 's/"/\\\\\"/g'`
	new=`echo "$new" | sed -e 's/"/\\\\\"/g'`
	new=`echo "$new" | sed -e 's/\// - /g'`
	new=`echo "$new" | sed -e 's/<SLASH>/\//g'`

	printf "mv %-35s %s\n" "\"$file\"" "\"$new\""

done
