#!/bin/bash

# Pi-rate radio juke box.  Make your neighbour's TV go fuzzy while you listen
# to the mp3s on your PC with your FM radio.  Either give it a list of mp3s
# to play or point it to a directory with mp3s in.  Track info is shown on a
# web page.

PIFM_FREQUENCY="102.5" # Transmitter frequency
PIFM_BASE="/home/naich/pirjb" # Directory this was installed in
SERVER_PORT="8080" # Point your browser to http://your_pi_ip:8080

# The rest can be left as defaults unless you want to change them
PIFM_BINARY="$PIFM_BASE/pifm"
WEBSERVER_SCRIPT="$PIFM_BASE/pirjb_webserver.sh"
TRACKINFO_FILE="$PIFM_BASE/pirjb_info" # File used to tell the webserver what is playing
TEMPLATE_FILE="$PIFM_BASE/pirjb_template.html" # Template for the web server page
TMPNAME="/tmp/pirjbtmp"

# Compander settings to do AGC.  Change to blank for no AGC.
# SOX_COMPAND="compand 0.3,0.8 6:-70,-60,-20 -11 -90 0.2"
SOX_COMPAND=""

# Nothing more to edit under here

# Clean up when CTRL-C pressed
trap shutdown INT

function shutdown() {
	echo "Bye!"

	# Clean up the temporary files
	rm $TMPNAME.* 2>/dev/null

	# Shutdown the transmitter
	$PIFM_BINARY &>/dev/null

	# Tell the webserver to die
	echo die |nc  localhost $SERVER_PORT &>/dev/null
	exit 0
}

function usage() {
cat << EOF
Useage: $0 [-sr] <-p playlist | -d directory>

OPTIONS:
  -s  Shuffle
  -r  Repeat the playlist continuously
  -p  Playlist file.  Each line contains the filename of an mp3 to play.
  -d  Directory containing the music files.  Scanned recursively for mp3s.
EOF
}
	
shuffle=0
isplaylist=0
repeat=0

# Sort out the options
while getopts ":shrp:d:" OPTION; do
case $OPTION in 
	s)
		shuffle=1
		;;
	p)
		playlistfile=$OPTARG
		isplaylist=1
		;;
	d)
		playdirectory=$OPTARG
		;;
	h)
		usage
		exit 0
		;;
	r)
		repeat=1
		;;		
	?)
		usage
		exit 1
		;;
	esac
done

if [[ -z "$playlistfile" ]] && [[ -z "$playdirectory" ]]; then
	echo You must specify a playlist file or directory to scan
	echo 
	usage
	exit 1
fi

if [[ $isplaylist -eq 0 ]]; then
	echo Generating playlist from all files in directory $playdirectory
	playlistfile="$(mktemp $TMPNAME.XXXXXX)"
	find "$playdirectory" -name "*.mp3" 2>/dev/null | sort >"$playlistfile"
fi

if [[ $shuffle -eq 1 ]]; then 
	playlist="$(mktemp $TMPNAME.XXXXXX)"
	sort -R <$playlistfile >$playlist		
else 
	playlist="$playlistfile"
fi

playlistsize=$(wc -l "$playlist"|cut -f1 -d " " )

if [[ $playlistsize -eq 0 ]]; then
	echo "No tracks in playlist"
	exit 1
fi

$WEBSERVER_SCRIPT $SERVER_PORT $TRACKINFO_FILE $TEMPLATE_FILE &

echo Found $playlistsize tracks.

oneloop=1

until [[ $oneloop -eq 0 ]] && [[ $repeat -eq 0 ]]; do
	while read mp3track; do
		trackinfo=$(mp3info -p "%a: %l - %n %t\n" "$mp3track")
		mp3info -p "%a\n%l\n%n\n%t\n" "$mp3track" > $TRACKINFO_FILE
		echo "Playing $trackinfo"
		
		# Put it into background and use "wait" so that ctrl-c is handled properly
		sox "$mp3track" -t wav - channels 1 rate 22050 $SOX_COMPAND vol 1 2>/dev/null | $PIFM_BINARY - $PIFM_FREQUENCY &
		pifm_pid=$!
		
		# These are used by the web server to control the player
		echo $pifm_pid >> $TRACKINFO_FILE
		echo $BASHPID >> $TRACKINFO_FILE

		# Clear out the old page from the server.  Pipe rubbish to
		# it to stop it reading all the stdin.
   		echo wibble | nc localhost $SERVER_PORT &>/dev/null

		wait $pifm_pid
#		sleep 3

	done <$playlist
(( oneloop-- ))
done
shutdown
exit 0
