#!/bin/bash
#pMusic - progressbar/statusbar daemon
#Copyright 2008-2015 - GPL
#Sigmund Berglund

[ $UTF8 = false ] && export LC_ALL=C #UTF-8 makes grep very slow
WORKDIR=$HOME/.pmusic/tmp
IFS=$'\n'

#play info
if [ "`LC_ALL=C grep -m1 -F 'kbits' $WORKDIR/ffmpeg_output`" ]; then
	#if new song differs from last played, then read song info
	if [ ! "`cut -d'|' -f1 $HOME/.pmusic/nowplaying`" ]; then
		FILE=($(<"$WORKDIR/PLAYLIST"))
		DURATION="`grep -m1 -F "$FILE" $WORKDIR/playlist | cut -d'|' -f2`"
		NOW_PLAYING="`grep -m1 -F "$FILE" $WORKDIR/playlist | cut -d'|' -f3`"
		echo "$FILE" > $WORKDIR/nowplaying_statusbar
		LENGTH=`echo "(${DURATION%:*}*60)+${DURATION#*:}" | bc` #min*60+sec=total_sec -- using bc since bash struggles with leading 0 makes sum octal.
		echo "$LENGTH" > $WORKDIR/ffmpeg_length
		echo "$NOW_PLAYING|$FILE" > $HOME/.pmusic/nowplaying
		#window title
		echo "$NOW_PLAYING" > $WORKDIR/window_title
		#make svg
		if [ -f "$FILE" ]; then
			. $APPDIR/func_id3io "$FILE" -read_quick
			ALBUM="$ID3_ALBUM"
			YEAR="$ID3_YEAR"
		elif [ "`grep -F "cdda:" <<< "$FILE"`" ]; then
			ID3_TRACK=($(echo "$FILE" | cut -d: -f2))
			ALBUM="$(grep "|${ID3_TRACK}|" $WORKDIR/cddb | cut -d'|' -f4)"
			YEAR="$(grep "|${ID3_TRACK}|" $WORKDIR/cddb | cut -d'|' -f6)"
		else
			ALBUM=""
			YEAR=""
		fi
		$APPDIR/func_svg -nowplaying "$NOW_PLAYING" "$ALBUM $YEAR"
		[ -f $WORKDIR/gui_is_running ] && echo true > $WORKDIR/UPDATE_GUI
		#update trackinfo
		if [ $USE_META = true ]; then
			#search trackinfo
			$APPDIR/func_kill -trackinfo
			$APPDIR/func_trackinfo -quiet -refresh &
		fi
		#clear error-flag
		echo 0 > $WORKDIR/statusbar_error
	else
		#if playing radio we might want to show meta info
		if [ $USE_META = true ]; then
			[ "$(<$WORKDIR/input_type)" = "radio_streamripper" ] && tail -n 1 $WORKDIR/streamripper_output | cut -d']' -f2 | cut -d'[' -f1 > $WORKDIR/nowplaying_statusbar
		fi
		LENGTH=($(<"$WORKDIR/ffmpeg_length"))
	fi
	#find played seconds
	TMP=`tail -c 120 $WORKDIR/ffmpeg_output | cut -d'=' -f 3 | cut -d ' ' -f 1 | cut -d: -f 2-3 | cut -d. -f1`
	#ffmpeg shows min:sec while avconv shows only sec (ffmpeg earlier than 0.8 also used sec)
	if [ "`echo "$TMP" | grep -F ':'`" ]; then
		SEC_PLAYED=`echo "(${TMP%:*}*60)+${TMP#*:}+$(<"$WORKDIR/SS")" | bc`
	else
		SEC_PLAYED=`echo "${TMP#*:}+$(<"$WORKDIR/SS")" | bc`
	fi
	#echo percent for progress slider - skip this if playing a radio stream	
	[ "$LENGTH" != "0" ] && echo $((($SEC_PLAYED*100/$LENGTH)+1)) > $WORKDIR/PERCENT_BAR
	#calculate time of seconds played
	TIME_MIN=$(($SEC_PLAYED/60))
	TIME_SEC=$(($SEC_PLAYED-($TIME_MIN*60)))
	#skip this if playing a radio stream
	if [ "$LENGTH" != "0" ]; then
		#calculate time of seconds remaining if not playing a radio stream
		SEC_PLAYED=$(($LENGTH-$SEC_PLAYED))
		TIME_MIN2=$(($SEC_PLAYED/60))
		TIME_SEC2=$(($SEC_PLAYED-($TIME_MIN2*60)))
		TIME_REMAINING=" / ${TIME_MIN2}:$(printf "%02d" ${TIME_SEC2})"
	fi
	#output
	echo "$(<$WORKDIR/statusbar_playlist)   (${TIME_MIN}:$(printf "%02d" ${TIME_SEC})$TIME_REMAINING) $(<$WORKDIR/nowplaying_statusbar)" > $WORKDIR/statusbar
#output when no tracks playing
else
	export TEXTDOMAIN=pmusic
	TEXT=' '
	if [ ! -s $WORKDIR/ffmpeg_output ]; then #stopped
		echo " $(<$WORKDIR/statusbar_playlist)" > $WORKDIR/statusbar
	elif grep -E "I/O error|Connection refused" $WORKDIR/ffmpeg_output; then
		if grep -F '://' $WORKDIR/PLAYLIST; then #no connection
			echo " $(gettext 'No connection')" > $WORKDIR/statusbar
			echo "$(($(<$WORKDIR/statusbar_error)+1))" > $WORKDIR/statusbar_error
		else #corrupted file
			echo " $(gettext 'Corrupted file')" > $WORKDIR/statusbar
			echo "$(($(<$WORKDIR/statusbar_error)+1))" > $WORKDIR/statusbar_error
		fi
	elif grep -E "Unknown format|could not find codec parameters" $WORKDIR/ffmpeg_output; then
		echo " $(gettext 'Unknown format')" > $WORKDIR/statusbar
	else
#		if [ ! -f $WORKDIR/PLAYLIST ] && [ ! "`grep -F '://' $WORKDIR/PLAYLIST`" ]; then #file has been renamed/moved --> not found
#			echo " $(gettext 'not found')" > $WORKDIR/statusbar
#			echo "$(($(<$WORKDIR/statusbar_error)+1))" > $WORKDIR/statusbar_error
		if [ "`grep -F "$(gettext 'No tracks detected')" $WORKDIR/sourcelist`" ]; then #no disc inserted
			echo " $(gettext 'No disc inserted')" > $WORKDIR/statusbar
			echo "$(($(<$WORKDIR/statusbar_error)+1))" > $WORKDIR/statusbar_error
		#When adding a file, the source is checked, but when adding from ie. a m3u there is still a chance that source is not there.
		elif grep -F 'no such file or directory' $WORKDIR/ffmpeg_output || ! grep -F 'Input #0' $WORKDIR/ffmpeg_output ; then
			if [ ! "`grep -F '://' $WORKDIR/PLAYLIST`" ] && [ -s $WORKDIR/PLAYLIST ]; then
				TMP="`ps -eo pid,command`"
				if [ "`echo "$TMP" | grep -F 'func_player -playing'`" ] && [ "`echo "$TMP" | grep -F 'cdda2wav'`" ]; then  #reading CD
					echo " $(gettext 'Reading CD')..." > $WORKDIR/statusbar
				else 
					echo "$(gettext 'Source not detected - Searching db for alternative')..." > $WORKDIR/statusbar
					echo "$(($(<$WORKDIR/statusbar_error)+1))" > $WORKDIR/statusbar_error
					if [ "$(<$WORKDIR/statusbar_error)" -ge 3 ]; then
						#search for alternative source in db. File has probably been moved.
						OLD_PATH="$(<$WORKDIR/PLAYLIST)"
						OLD_INFO="$(<$WORKDIR/PLAYLIST_ALL)"
						TRACKNAME="`grep -F $(<$WORKDIR/PLAYLIST) $WORKDIR/playlist | cut -d'|' -f3`"
						ARTIST2="`echo "$TRACKNAME" | awk -F' - ' {'print $1'}`"
						ARTIST="`echo "$ARTIST2" | awk -F "(" '{print $1}' | awk -F ' feat' '{print $1}' | awk -F ' & ' '{print $1}' | awk -F ' / ' '{print $1}' | sed "s/ $//"`" #remove info in ()
						TITLE2="`echo "$TRACKNAME" | awk -F' - ' {'print $2'}`"
						TITLE="`echo "$TITLE2" | awk -F "(" '{print $1}' | sed "s/ $//" | sed -e 's/\.[^\.]*$//'`" #remove info in () and extension
						NEW_PATH="`cut -d'|' -f1-3 "$STORAGE_DIR/index_mymusic" | grep -iF "|${TITLE}" | grep -Fi "|${ARTIST}|" | cut -d'|' -f1 | grep -iEm1 "$AUDIO_FORMATS"`"
						[ ! "$NEW_PATH" ] && NEW_PATH="`cut -d'|' -f1-3 "$STORAGE_DIR/index_mymusic" | grep -iwF "${TITLE}" | grep -Fwi "${ARTIST}" | cut -d'|' -f1 | grep -iEm1 "$AUDIO_FORMATS"`"
						#alternative is found - let's play
						if [ -s "$NEW_PATH" ]; then
							LENGTH="$(grep -F "|${NEW_PATH}|" "$STORAGE_DIR/index_mymusic" | cut -d'|' -f12)"
							if [ ! "$LENGTH" ]; then
								ffmpeg -i "${NEW_PATH}" > $WORKDIR/tmp_progressbar_ffmpeg 2>&1
								LENGTH="`grep -wm1 "Duration:" $WORKDIR/tmp_progressbar_ffmpeg | cut -d. -f1 | awk -F':' '{print $3":"$4}'`"
							fi
							( flock -e 201
								TMP_PRE="$(grep -FB10000 "$OLD_INFO" $WORKDIR/playlist | grep -vF "$OLD_INFO")"
								TMP_POST="$(grep -FA10000 "$OLD_INFO" $WORKDIR/playlist | grep -vF "$OLD_INFO")"
								NEW_INFO="|${LENGTH}|${TRACKNAME}|${NEW_PATH}|$(date +%s%N) ${NEW_PATH}"
								echo -e "${TMP_PRE}\n${NEW_INFO}\n${TMP_POST}" > $WORKDIR/playlist
								#sed -i 's%$OLD_INFO%$NEW_INFO%g' $WORKDIR/playlist
							) 201>$WORKDIR/playlist.lockfile 
							echo "$NEW_PATH" > $WORKDIR/PLAYLIST
							echo "${NEW_INFO}" > $WORKDIR/PLAYLIST_ALL
							$APPDIR/func_player -playing -playing &
							echo 0 > $WORKDIR/statusbar_error
						fi
					fi
				fi
			else #connecting
				echo " $(gettext 'Connecting')..." > $WORKDIR/statusbar
			fi
		elif grep -F 'error' $WORKDIR/aplay_error; then #playback error - this might be the result of user 'works' too much with the <hscale> slider.
			echo " $(gettext 'Playback error')" > $WORKDIR/statusbar
			echo "$(($(<$WORKDIR/statusbar_error)+1))" > $WORKDIR/statusbar_error
		else #buffering
			echo " $(gettext 'Buffering...Please wait')" > $WORKDIR/statusbar
		fi
	fi
	#if error more than 5 loops (in most cases 5 sec), skip to next track.
	if [ "$(<$WORKDIR/statusbar_error)" -ge 5 ]; then
		pmusic -s next
		echo 0 > $WORKDIR/statusbar_error
	fi
fi
