#!/bin/sh
# Prerequisites:
# 	* Get and setup http://www.red-bean.com/~decklin/software/lastfmsubmitd/
# 	* perl -MCPAN -e 'install MP3::Info' (check your distro for perl-MP3-Info package first)
# Install this script with some handy name, e.g. '/usr/local/bin/m'.
# Known problems:
#	* 'q' interrupts only playback of current file; press and *hold* ctrl-C
#	* even if you skip file immediately with Enter or 'q', it gets scrobbled - see below
#
# Song info will be submitted after you finish listening to the song;
# if you are SKIPPING a track and don't want it to appear, press Ctrl-C
# in mplayer - it will skip to the next track without scrobbling.

# ([ -f var/run/lastfm/lastfmsubmitd.pid ] && kill -0 `cat /var/run/lastfm/lastfmsubmitd.pid` 2>&1) || lastfmsubmitd
[ "$LASTFM_PLAYER" ] || LASTFM_PLAYER=mplayer

for f; do
	$LASTFM_PLAYER "$f" || continue

	case "$f" in
	*.mp3)
		perl -MMP3::Info -le \
			'my $f = $ARGV[0]; my $i = new MP3::Info $f;
				print $i->time; print $i->artist; print $i->album; print $i->title;' \
			"$f" |
		{
			read time;
			read artist;
			read album;
			read title;
			[ "$album" = "1" ] && album=""
			echo "## Last.FM : -l \"$time\" -a \"$artist\" -b \"$album\" --title \"$title\""
			/usr/lib/lastfmsubmitd/lastfmsubmit -e utf8 \
				-l "$time" -a "$artist" -b "$album" --title "$title"
		}
		;;
	esac
done
