#!/bin/bash
#Pmusic
#Copyright 2008, 2009
#Sigmund Berglund

if [ $USE_TAGS = true ]; then #change name from filename to metatag artist - title
	if [ -s $WORKDIR/pmusic-LAST_INDEX ]; then
		LAST_INDEX="`cat $WORKDIR/pmusic-LAST_INDEX`"
		TMP="`date +%s`"
		INDEX_TIME=`expr $TMP / 60` #minutes since 1970. For -mtime.
		CMIN=`expr $LAST_INDEX - $INDEX_TIME`
	else
		INDEX_TIME=20000000 #minutes since 1970. For -cmin
	fi
	if [ $INDEX_CHANGES = false ]; then #reset
		rm -f $WORKDIR/generated_playlists/*
		CMIN="-$INDEX_TIME"
	fi
	#find both changed files and new added files
	echo -e "\c" > $WORKDIR/pmusic-index_tmp
	echo -e "\c" > $WORKDIR/pmusic-index_cmin
	while read I; do
		find -P "$I" -printf "  %p|%f\n" | grep -i --extended-regexp "$AUDIO_FORMATS|.m3u|.pls" >> $WORKDIR/pmusic-index_tmp
		find -P "$I" -cmin $CMIN -printf "  %p|%f\n" | grep -i --extended-regexp "$AUDIO_FORMATS|.m3u|.pls" >> $WORKDIR/pmusic-index_cmin
	done < $HOME/.pmusic/index_directories
	diff -e $HOME/.pmusic/index_all $WORKDIR/pmusic-index_tmp > $WORKDIR/pmusic-index_new 2> /dev/null
	grep '/' $WORKDIR/pmusic-index_new > $WORKDIR/pmusic-tmp #remove head info
	mv -f $WORKDIR/pmusic-tmp $WORKDIR/pmusic-index_new
	#combine files detected by cmin and new files
	cat $WORKDIR/pmusic-index_cmin >> $WORKDIR/pmusic-index_new
	sort -u --output=$WORKDIR/pmusic-tmp $WORKDIR/pmusic-index_new
	mv -f $WORKDIR/pmusic-tmp $WORKDIR/pmusic-index_new
	#check meta tag. file by file
	echo true > $WORKDIR/pmusic-USE_TAGS
	COUNT=0
	TOTAL=`cat $WORKDIR/pmusic-index_new | wc -l`
	echo -e "\c" > $WORKDIR/pmusic-index_tmp
	while read I; do
		FILE="`echo "$I" | cut -d '|' -f 1`"
		ID3_ARTIST="`id3info "$FILE" | grep 'Lead performer' | cut -d ':' -f 2- | cut -b 2-`"
		if [ "$ID3_ARTIST" ] && [ "$ID3_ARTIST" != " " ]; then
			ID3_TITLE="`id3info "$FILE" | grep 'Title' | cut -d ':' -f 2- | cut -b 2-`"
			NAME="`echo "$I" | cut -d '|' -f 2`"
			EXTENSION="`echo "$I" | cut -d '|' -f 2 | sed -e "s/^.*\.//g"`"
			echo "  $I" | sed -e "s%|$NAME%|$ID3_ARTIST - $ID3_TITLE.$EXTENSION%g" >> $WORKDIR/pmusic-index_tmp
		else
			echo "  $I" >> $WORKDIR/pmusic-index_tmp
		fi
		#generate playlists of directories containing music files.
		case "$INDEX_DIR" in
			"../$LOC_ARTIST - $LOC_ALBUM/$LOC223")
				TMP="`echo "$I" | cut -d '|' -f 1`"
				DIR_ARTIST_ALBUM="`dirname "$TMP"`"
				ARTIST_ALBUM="`basename "$DIR_ARTIST_ALBUM"`"
				find "$DIR_ARTIST_ALBUM" -type f | grep -i --extended-regexp "$AUDIO_FORMATS" > $WORKDIR/generated_playlists/"$ARTIST_ALBUM".dir
				;;
			"../$LOC_ARTIST/$LOC_ALBUM/$LOC223")
				TMP="`echo "$I" | cut -d '|' -f 1`"
				DIR_ALBUM="`dirname "$TMP"`" #level 1
				DIR_ARTIST="`dirname "$DIR_ALBUM"`" #level 2
				ALBUM="`basename "$DIR_ALBUM"`"
				ARTIST="`basename "$DIR_ARTIST"`"
				find "$DIR_ALBUM" -type f | grep -i --extended-regexp "$AUDIO_FORMATS" > $WORKDIR/generated_playlists/"$ARTIST - $ALBUM".dir
				find "$DIR_ARTIST" -type f | grep -i --extended-regexp "$AUDIO_FORMATS" > $WORKDIR/generated_playlists/"$ARTIST".dir
				;;
			"../$LOC_ARTIST/$LOC223")
				TMP="`echo "$I" | cut -d '|' -f 1`"
				DIR_ARTIST="`dirname "$TMP"`" #level 1
				ARTIST="`basename "$DIR_ARTIST"`"
				find "$DIR_ARTIST" -type f | grep -i --extended-regexp "$AUDIO_FORMATS" > $WORKDIR/generated_playlists/"$ARTIST".dir
				;;
		esac
		#update progress bar
		PERCENT=`expr $COUNT \* 100 / $TOTAL`
		PERCENT=`expr $PERCENT + 1`
		echo $PERCENT > $WORKDIR/pmusic-index_splash #update progressbar
		COUNT=`expr $COUNT + 1`
	done < $WORKDIR/pmusic-index_new
	if [ $INDEX_CHANGES = false ]; then
		mv -f $WORKDIR/pmusic-index_tmp $HOME/.pmusic/index_all
	else
		cat $WORKDIR/pmusic-index_tmp >> $HOME/.pmusic/index_all
	fi
else #only use filenames in add-list
	rm -f $WORKDIR/generated_playlists/*
	echo false > $WORKDIR/pmusic-USE_TAGS
	echo -e "\c" > $HOME/.pmusic/index_all
	while read I; do
		find -P "$I" -printf "  %p|%f\n" | grep -i --extended-regexp "$AUDIO_FORMATS|.m3u|.pls|" >> $HOME/.pmusic/index_all
	done < $HOME/.pmusic/index_directories
fi
#spread index to subindexes
echo -e "\c" > $HOME/.pmusic/index_alphabetic
echo -e "\c" > $HOME/.pmusic/index_playlists
find $WORKDIR/generated_playlists -printf "  %p|%f\n" >> $HOME/.pmusic/index_all #generated directories
grep -i --extended-regexp "$AUDIO_FORMATS" $HOME/.pmusic/index_all >> $HOME/.pmusic/index_alphabetic
grep -i "('$\|\.m3u$\|\.pls$\|\.dir$\|\.{END}$ ')" $HOME/.pmusic/index_all >> $HOME/.pmusic/index_playlists
sort -u --output=$WORKDIR/pmusic-tmp $HOME/.pmusic/index_alphabetic
mv -f $WORKDIR/pmusic-tmp $HOME/.pmusic/index_alphabetic
sort -u --output=$WORKDIR/pmusic-tmp $HOME/.pmusic/index_playlists
mv -f $WORKDIR/pmusic-tmp $HOME/.pmusic/index_playlists
echo 100 > $WORKDIR/pmusic-index_splash #reset progress bar
