#!/bin/bash
#pMusic
#Copyright 2013,2014
#Sigmund Berglund
export TEXTDOMAIN=pmusic

case $1 in
-add_favorites_with_timestamp)
	NOW_PLAYING="`cut -d'|' -f2 $HOME/.pmusic/nowplaying`"
	TIME="`cut -d'(' -f3 $WORKDIR/statusbar | cut -d'/' -f1 | tr -d ' '`"
	if [ ! "`grep -F ':' <<< "$TIME"`" ]; then
		TXT1="<b>$(gettext 'No timestamp found')</b>"
		TXT2="$(gettext 'This works only for a playing track.')"
		. $APPDIR/box_ok
		exit
	fi
	#splash
	gettext 'Add to Favorites' > $WORKDIR/splashtext
	$APPDIR/box_splash &
	#---
	TMP="`grep -Fm 1 "${NOW_PLAYING}|" "$STORAGE_DIR/index_mymusic"`"
	if [ "$TMP" ]; then #file-locking of db could give empty $TMP
		echo "$TMP" > $WORKDIR/tmp_favorites
		awk -F'|' -v VAR=$TIME '{ print "[" VAR "] " $1 "|[" VAR "] " $2 "|" $3 "|" $4 "|" $5 "|" $6 "|" $7 "|" $8 "|" $9 "|" $10 "|" $11 "|" $12 "|" $13 "|" $14 "|" $15}' $WORKDIR/tmp_favorites >> "$STORAGE_DIR/favorites"
		#update sourcelist if active
		if [ "`grep -F 'pmusic_favorite_lists' $WORKDIR/sourcelist`" ]; then
			TMP="`grep -FB 10000 'pmusic_favorite_marks' $WORKDIR/sourcelist | grep -vF 'pmusic_favorite_marks'`"
			TMP1="`grep -FA 10000 'pmusic_rating' $WORKDIR/sourcelist`"
			echo "$TMP" > $WORKDIR/sourcelist
			$APPDIR/func_favorites -show_favorites_marks
			echo -e "\n$TMP1" >> $WORKDIR/sourcelist
			echo true > $WORKDIR/UPDATE_SOURCELIST
		fi
	fi
	#---
	sleep 0.5
	echo 100 > $WORKDIR/splash
	;;
	
-add_favorites)
	#if new list we should maybe update sourcelist afterwards
	[ ! -s "$STORAGE_DIR/favorite_lists/${3}" ] && NEW_LIST=true
	#set name of valid items in both favorite lists and marks (these are sent to func_rating) 
	if [ "`echo "$2" | grep -F "http:"`" ]; then
		NAME="`grep -F "$2" "$STORAGE_DIR/index_radio" | cut -d'|' -f2`"
		[ ! "$NAME" ] && NAME="`grep -F "$(tail -c 7 <<< "$2")" "$STORAGE_DIR/index_webmusic_jamendo" | awk -F'|' '{print $2 " - " $3}'`"
		[ ! "$NAME" ] && NAME="$2"
	elif [ "`echo "$2" | grep -F "cdda:"`" ]; then
		NAME="`cut -d: -f5 <<< "$2"`"
	else
		NAME="$2"
	fi
	#---
	if [ "$3" ]; then #add to list
		if [ "`grep -F "${2}|" "$STORAGE_DIR/index_mymusic"`" ]; then
			grep -F "${2}|" "$STORAGE_DIR/index_mymusic" >> "$STORAGE_DIR/favorite_lists/${3}"
		else
			echo "${2}|${NAME}||||||||||||$2|gtk-audio" >> "$STORAGE_DIR/favorite_lists/${3}"
		fi
	else #add a mark
		if [ ! "`grep -F "${2}|" "$STORAGE_DIR/favorites"`" ]; then
			if [ -d "$2" ]; then
				TMP="`basename "$2"`"
				echo "${2}|${TMP}||||||||||||$2|gtk-directory" >> "$STORAGE_DIR/favorites"
			elif [ "`echo "$2" | grep -iE "\.pmu$|\.m3u$|\.pls$"`" ] && [ ! "`echo "$2" | grep -F 'http://'`" ]; then
				TMP="`echo "$2" | sed -e 's/pmusicbrowse_expand://g' -e 's/pmusicbrowse_collapse://g'`"
				TMP2="`basename "$TMP"`" # | sed -e 's/\.[^\.]*$//'
				echo "$2|$TMP2||||||||||||$TMP|gtk-audio_playlist" >> "$STORAGE_DIR/favorites"
			elif [ "`grep -F "${2}|" "$STORAGE_DIR/index_mymusic"`" ]; then
				grep -F "${2}|" "$STORAGE_DIR/index_mymusic" >> "$STORAGE_DIR/favorites"
			elif [ "`grep -F "${2}|" "$STORAGE_DIR/index_radio"`" ]; then
				echo "${2}|${NAME}||||||||||||$2|gtk-audio-url" >> "$STORAGE_DIR/favorites"
			else
				echo "${2}|${NAME}||||||||||||$2|gtk-file" >> "$STORAGE_DIR/favorites"
			fi
		fi
	fi
	#update sourcelist if active
	if [ "`grep -F 'pmusic_favorite_lists' $WORKDIR/sourcelist`" ]; then
		if [ "$NEW_LIST" = "true" ]; then
			tac $WORKDIR/sourcelist | head -n-1 | tac > $WORKDIR/tmp_favorites1
			echo "pmusic_favorite_lists|__ $(gettext 'F A V O R I T E   L I S T S') __" > $WORKDIR/sourcelist
			echo "pmusicbrowse_expand:$STORAGE_DIR/favorite_lists/$3|$3||||||||||||$STORAGE_DIR/favorite_lists/$3|gtk-audio_playlist_favorites" >> $WORKDIR/sourcelist
			cat $WORKDIR/tmp_favorites1 >> $WORKDIR/sourcelist
		fi
		if [ ! "$3" ]; then #a mark
			TMP="`grep -FB 10000 'pmusic_favorite_marks' $WORKDIR/sourcelist | grep -vF 'pmusic_favorite_marks'`"
			TMP1="`grep -FA 10000 'pmusic_rating' $WORKDIR/sourcelist`"
			echo "$TMP" > $WORKDIR/sourcelist
			$APPDIR/func_favorites -show_favorites_marks
			echo -e "\n$TMP1" >> $WORKDIR/sourcelist
		else #inside favorite list
			if [ "`grep -F "pmusicbrowse_collapse:$STORAGE_DIR/favorite_lists/$3" $WORKDIR/sourcelist`" ]; then #list is expanded - update is required
				SOURCE="pmusicbrowse_collapse:$STORAGE_DIR/favorite_lists/$3"
				$APPDIR/func -browse
				SOURCE="pmusicbrowse_expand:$STORAGE_DIR/favorite_lists/$3"
				$APPDIR/func -browse
			fi
		fi
		echo true > $WORKDIR/UPDATE_SOURCELIST
	fi
	;;

-show_favorites)
	echo "pmusic_favorite_lists|__ $(gettext 'F A V O R I T E   L I S T S') __" > $WORKDIR/sourcelist
	#favorite lists
	find "$STORAGE_DIR/favorite_lists/" -maxdepth 1 -mindepth 1 -type d -printf "pmusicbrowse_parent:pmusicbrowse_favorites:%p|%f||||||||||||%p|gtk-favorites_group\n" >> $WORKDIR/sourcelist
	find "$STORAGE_DIR/favorite_lists/" -maxdepth 1 -type f -printf "pmusicbrowse_expand:%p|%f||||||||||||%p|gtk-audio_playlist_favorites\n" >> $WORKDIR/sourcelist
	#bookmarks
	$APPDIR/func_favorites -show_favorites_marks
	#most played
	echo -e "\npmusic_rating|__ $(gettext 'M O S T   P L A Y E D') __|($(gettext 'last month'))" >> $WORKDIR/sourcelist
	head -n 5000 "$STORAGE_DIR/index_mymusic" > $WORKDIR/tmp_favorites
	$APPDIR/func_rating -buildlist "$(($(date +%s)-$RATING_PERIODE))" $WORKDIR/tmp_favorites > $WORKDIR/tmp_favorites2 #last x days - set in $RATING_PERIODE
	sort -rnst'|' -k9,9 $WORKDIR/tmp_favorites2 | head -n 50 | grep -vF '|0|' > $WORKDIR/tmp_favorites3
	cat $WORKDIR/tmp_favorites3 >> $WORKDIR/sourcelist
	[ `grep -c ^ $WORKDIR/tmp_favorites3` = 50 ] && echo "pmusic_rating_month|...$(gettext 'more')|||||||||||||gtk-go-down" >> $WORKDIR/sourcelist
	#album art
	if [ $SEARCH_ALBUMART = true ]; then
		cat $WORKDIR/tmp_favorites3 | awk -F'|' -v VAR1="$STORAGE_DIR" '{print VAR1 "/albumart/" $2 " - " $4 ".jpg"}' | awk '!x[toupper($0)]++' > $WORKDIR/tmp_search_albumart
		$APPDIR/func -search_albumart_update
	fi
	#---
	echo > $WORKDIR/filebrowser_dir
	$APPDIR/func -add_to_history "pMusic: Favorites" & #update history
	;;

-show_favorites_marks)
	echo -e "\npmusic_favorite_marks|__ $(gettext 'F A V O R I T E S') __" >> $WORKDIR/sourcelist
	AUDIO_FORMATS2="`echo "$AUDIO_FORMATS" | sed 's/\\$/\\\|/g'`" #example: |/path/file.mp3|
	grep -viE "$AUDIO_FORMATS2|^http:|^cdda:" "$STORAGE_DIR/favorites" | grep -v "^$" | sort -rst'|' -k15,15 >> $WORKDIR/sourcelist
	grep -iE "$AUDIO_FORMATS2|^http:|^cdda:" "$STORAGE_DIR/favorites" > $WORKDIR/tmp_favorites
	$APPDIR/func_rating -buildlist 1 $WORKDIR/tmp_favorites | sort -rnst'|' -k14,14 >> $WORKDIR/sourcelist
	;;
esac

