#!/bin/bash
#pMusic - external menu
#Copyright 2013,2014
#Sigmund Berglund

[ $PTR_BTN != 3 ] && exit

template(){
	if [ "$4" = "require_file" ] && [ "`echo "$PLAYLIST" | grep -E 'cdda:|http'`" ]; then SENSITIVE='<sensitive>false</sensitive>'; fi #in case cdda or stream, disable some choices and some new
	if [ "$5" = "require_editor" ] && [ ! "$AUDIO_EDITOR" ]; then SENSITIVE='<sensitive>false</sensitive>'; fi

	echo '<hbox spacing="0">
	 <pixmap height-request="20" icon_size="1" space-expand="false" space-fill="false"><input file stock="gtk-'$3'"></input></pixmap>
	 <button height-request="20" xalign="0" can-focus="no" relief="2" space-expand="true" space-fill="true">
	  '$SENSITIVE'
	  <label>" '$1'"</label>
	  <action>echo '$2' > '$WORKDIR'/OUTPUT</action>
	  <action>EXIT:exit</action>
	 </button>
	</hbox>'
}

echo true > $WORKDIR/RIGHT_MENU #if menu, exit -playlist_move)
echo -n > $WORKDIR/OUTPUT

#define favorites menuitem
MENUITEMS="`find "$STORAGE_DIR/favorite_lists/" -type f -printf "<menuitem><label>\\\"%f\\\"</label><action>echo \\\"%f\\\" > $WORKDIR/tmp_favorite_list</action><action>refresh:FAVORITE_LIST</action></menuitem>\n" | sed 's%.pmd%%g'`"
[ ! "$MENUITEMS" ] && MENUITEMS="<menuitem><label>".."</label></menuitem>"
gettext 'My favorite list' > $WORKDIR/tmp_favorite_list #default value
	
export gtkdialog_menu='
<window decorated="false" skip_taskbar_hint="true" resizable="false" height-request="250" width-request="330" modal="true" space-expand="true" space-fill="true">
  <hbox scrollable="true" hscrollbar-policy="2" vscrollbar-policy="2" space-expand="true" space-fill="true">
  <text width-request="1" space-expand="false" space-fill="false"><label>""</label></text>
  <vbox spacing="2">
   <text height-request="2" space-expand="false" space-fill="false"><label>""</label></text>
   '"
  $(template " $(gettext 'Play')" play media-play)
  $(template " $(gettext 'Remove from playQueue (middle-button)')" remove pmusic_remove)
  <hseparator></hseparator>
  $(template " $(gettext 'Track info')" info info)"'
	<hbox spacing="2">
	 <pixmap height-request="20" icon_size="1" space-expand="false" space-fill="false"><input file stock="gtk-favorites_add"></input></pixmap>
	 <togglebutton height-request="20" xalign="0" can-focus="no" relief="2" space-expand="true" space-fill="true">
	  <label>" '$(gettext 'Add to Favorites')'"</label>
	  <action>if true show:HBOX_FAV</action>
	  <action>if false hide:HBOX_FAV</action>
	  <action>if true show:ICO_FAV</action>
	  <action>if false hide:ICO_FAV</action>
	 </togglebutton>
	</hbox>
	<hbox height-request="22" visible="false" spacing="2" tooltip-text="'$(gettext 'What favorite list should the item be added to?
An empty field adds a favorite Mark')'">
	 <text width-request="18"><label>""</label></text>
	 <entry secondary-icon-stock="gtk-clear">
	  <variable>FAVORITE_LIST</variable>
	  <input file>'$WORKDIR'/tmp_favorite_list</input>
	  <output file>'$WORKDIR'/tmp_favorite_list</output>
	  <action signal="key-pressed-event">save:FAVORITE_LIST</action>
	  <action signal="secondary-icon-release">clear:FAVORITE_LIST</action>
	  <action signal="activate">echo add_favorites > '$WORKDIR'/OUTPUT</action>
	  <action signal="activate">EXIT:exit</action>
	 </entry>
	 <menubar>
	  <menu>
	  '$MENUITEMS'
	  <label>..</label>
	  </menu>
	 </menubar>
	 <button height-request="20" stock-icon-size="1">
	  <input file stock="gtk-favorites_add"></input>
	  <action>echo add_favorites > '$WORKDIR'/OUTPUT</action>
	  <action>save:FAVORITE_LIST</action>
	  <action>EXIT:exit</action>
	 </button>
	 <variable>HBOX_FAV</variable>
	</hbox>'"


  $(template " $(gettext 'Show location')" show_location open require_file)
  $(template " $(gettext 'Edit audio-stream (and keep meta-info)')" edit_audio audio require_file require_editor)
  $(template " $(gettext 'Send to masstagger')" masstagger go-forward require_file)
 <hseparator></hseparator>
  $(template " $(gettext 'Rename source file')" rename convert require_file)
  $(template " $(gettext 'Copy source file')" copy copy require_file)
  $(template " $(gettext 'Delete source file')" delete delete require_file)
 "'</vbox></hbox>
 <action signal="focus-out-event">EXIT:exit</action>
</window>'

$GTKDIALOG -p gtkdialog_menu -G +"$(($PTR_X_ROOT-300))"+"$(($PTR_Y_ROOT-5))" > /dev/null

export PTR_BTN=""
OUTPUT=($(<"$WORKDIR/OUTPUT"))
case "$OUTPUT" in
	play)
		$APPDIR/func -playlist_play &
	;;
	remove)
		$APPDIR/func -remove_from_list
	;;
	info)
		$APPDIR/func_trackinfo "$(cut -d' ' -f2- <<< "$PLAYLIST")" -norefresh &
	;;
	masstagger)
		TMP="`ps -eo pid,command`"
		if [ "`echo "$TMP" | grep -F pmusic | grep -F func_id3tagger`" ]; then #already running --> add to list
			cut -d' ' -f2- <<< "$PLAYLIST" >> $WORKDIR/id3tagger
		else
			$APPDIR/func_id3tagger &
			sleep 0.1
			cut -d' ' -f2- <<< "$PLAYLIST" > $WORKDIR/id3tagger
		fi
	;;
	add_favorites)
		gettext 'Add to Favorites' > $WORKDIR/splashtext
		$APPDIR/box_splash &
		#---
		[ "$(<$WORKDIR/tmp_favorite_list)" ] && LIST="$(<$WORKDIR/tmp_favorite_list).pmd"
		TMP="`cut -d' ' -f2- <<< "$PLAYLIST"`"
		$APPDIR/func_favorites -add_favorites "$TMP" "$LIST"
		#---
		sleep 0.5
		echo 100 > $WORKDIR/splash
	;;
	show_location)
		TMP="`echo "$PLAYLIST" | cut -d' ' -f2-`" #remove numbering
		$APPDIR/func -show_location "$TMP" &
	;;
	edit_audio)
		#store meta-info because editor will most likely destroy them
		FILE="$(cut -d' ' -f2- <<< "$PLAYLIST")"
		. $APPDIR/func_id3io "$FILE" -read
		OLD_LENGTH="$FF_LENGTH"
		#Run editor
		export GTK2_RC_FILES="$HOME/.gtkrc-2.0" #use global gtk-theme for editor
		$AUDIO_EDITOR "$(cut -d' ' -f2- <<< "$PLAYLIST")"
		#save meta-info to new (edited) file
		. $APPDIR/func_id3io "$FILE" -write
		#update length in db
		ffmpeg -i "$FILE" > $WORKDIR/tmp_id3ffmpeg 2>&1
		FF_LENGTH="`grep -wm1 "Duration:" $WORKDIR/tmp_id3ffmpeg | cut -d. -f1 | awk -F':' '{print $3":"$4}'`"
		( flock -e 200
			grep -F "$FILE" "$STORAGE_DIR/index_mymusic" | awk -F'|' -v VAR1="$FF_LENGTH" '{print $1 "|" $2 "|" $3 "|" $4 "|" $5 "|" $6 "|" $7 "|" $8 "|" $9 "|" $10 "|" $11 "|" VAR1 "|" $13 "|" $14 "|" $15 "|" $16 "|" $17 "|" $18 "|" $19 "|" $20 "|" $21}' > $WORKDIR/tmp_edit_audio
			grep -Fv "$FILE" "$STORAGE_DIR/index_mymusic" >> $WORKDIR/tmp_edit_audio
			mv $WORKDIR/tmp_edit_audio "$STORAGE_DIR/index_mymusic"
		) 200>$WORKDIR/mymusic_db.lockfile
		#update playlist
		( flock -e 201
			sed -i "s%$OLD_LENGTH%$FF_LENGTH%g" $WORKDIR/playlist
		) 201>$WORKDIR/playlist.lockfile
		echo true > $WORKDIR/UPDATE_GUI
	;;
	rename)
		TMP="`echo "$PLAYLIST" | cut -d' ' -f2-`" #remove numbering
		$APPDIR/func -rename "$TMP"
	;;
	copy)
		TMP="`echo "$PLAYLIST" | cut -d' ' -f2-`" #remove numbering
		$APPDIR/func -copy "$TMP"
	;;
	delete)
		TMP="`echo "$PLAYLIST" | cut -d' ' -f2-`" #remove numbering
		$APPDIR/func -delete "$TMP"
	;;
esac
