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

#reset gtk-theme
if [ "`cat $APPDIR/themes/$THEME/themerc > /dev/null 2>&1 | grep THEME_ALL | grep '=true'`" ]; then 
	echo
else
	if [ -f /etc/gtk-2.0/gtkrc ]; then
		export GTK2_RC_FILES=':/etc/gtk-2.0/gtkrc' #/etc/gtk-2.0/gtkrc is for < Puppy4
	else
		export GTK2_RC_FILES="`grep -m 1 gtkrc $HOME/.gtkrc-2.0 | cut -d'\"' -f2 2> /dev/null`" #get active theme
	fi
fi
#---

case "$1" in
-lyrics_search)
	$APPDIR/box_splash_progress &
	#azlyrics
	echo "http://www.azlyrics.com
http://www.elyrics.net
http//www.lyricswiki.org" > $WORKDIR/pmusic-splashtext
	echo 20 > $WORKDIR/pmusic-splash
	TMP_ARTIST="`echo $LYRICS_SONG | tr '_' ' ' | sed -e 's/ - /_/g' | cut -d '_' -f 1 | tr [A-Z] [a-z] | tr -d ' ' | tr -d \' | tr -d '-'`"
	TMP_SONG="`echo $LYRICS_SONG | tr '_' ' ' | sed -e 's/ - /_/g' | cut -d '_' -f 2 | tr [A-Z] [a-z] | tr -d ' ' | tr -d \' | tr -d '-'`"
	wget -O $WORKDIR/pmusic-lyrics http://www.azlyrics.com/lyrics/$TMP_ARTIST/$TMP_SONG.html
	LYRICS="`cat $WORKDIR/pmusic-lyrics | grep -A 1000 -m 1 'END OF RINGTONE' | grep -B 1000 -m 1 'azlyrics' | grep -v azlyrics | grep -vi ringtone `"
	echo "$LYRICS" > $WORKDIR/pmusic-lyrics
	sed -i -e s/\<.\>//g $WORKDIR/pmusic-lyrics
	sed -i -e s/\<..\>//g $WORKDIR/pmusic-lyrics
	sed -i -e s/\<...\>//g $WORKDIR/pmusic-lyrics
	sed -i -e s/\<....\>//g $WORKDIR/pmusic-lyrics
	#elyrics
	if [ ! "$LYRICS" ]; then
		echo 50 > $WORKDIR/pmusic-splash
		TMP_ARTIST="`echo $LYRICS_SONG | tr '_' ' ' | sed -e 's/ - /_/g' | sed -e 's/-/,,/g' | tr ' ' '-' | cut -d '_' -f 1 | tr [A-Z] [a-z] | tr -d \'`"
		TMP_SONG="`echo $LYRICS_SONG | tr '_' ' ' | sed -e 's/ - /_/g' | sed -e 's/-/,,/g' | tr ' ' '-' | cut -d '_' -f 2 | tr [A-Z] [a-z] | tr -d \'`"
		TMP_CHAR="`echo $TMP_ARTIST | cut -c 1`"
		wget -O $WORKDIR/pmusic-lyrics http://www.elyrics.net/read/$TMP_CHAR/$TMP_ARTIST-lyrics/$TMP_SONG-lyrics.html
		LYRICS="`cat $WORKDIR/pmusic-lyrics | grep -A 1000 -m 1 'terms of service' | grep -B 1000 -m 1 'Ringtone to your Cell Phone' | grep -v 'terms of service' | grep -v 'Ringtone to your Cell Phone' `"
		echo "$LYRICS" > $WORKDIR/pmusic-lyrics
		sed -i -e s/\<.\>//g $WORKDIR/pmusic-lyrics
		sed -i -e s/\<..\>//g $WORKDIR/pmusic-lyrics
		sed -i -e s/\<...\>//g $WORKDIR/pmusic-lyrics
		sed -i -e s/\<....\>//g $WORKDIR/pmusic-lyrics
	fi
	#lyricwiki.org
	if [ ! "$LYRICS" ]; then
		echo 80 > $WORKDIR/pmusic-splash
		TMP_ARTIST="`echo $LYRICS_SONG | tr '_' ' ' | sed -e 's/ - /_/g' | cut -d '_' -f 1 | tr ' ' _`"
		TMP_SONG="`echo $LYRICS_SONG | tr '_' ' ' | sed -e 's/ - /_/g' | cut -d '_' -f 2 | tr ' ' _`"
		LYRICS="`wget -O $WORKDIR/pmusic-tmp http://lyricwiki.org/$TMP_ARTIST:$TMP_SONG ; sed '/lyricbox\|h1/!d;/class/!d;s/.*lyricbox//;s/<h1 class=\"firstHeading\"//;s/<\/h1>//;s/<br \///g;' $WORKDIR/pmusic-tmp|tr '>' '\n'`"
		[ `echo $LYRICS | wc -l` -lt 5 ] && LYRICS=""
		echo "$LYRICS" > $WORKDIR/pmusic-lyrics
	fi
	#---
	[ ! "$LYRICS" ] && echo "$LOC325" > $WORKDIR/pmusic-lyrics
	echo 100 > $WORKDIR/pmusic-splash
	;;
-undo)
	$APPDIR/func_store_playlist
	PLAYLIST_NAME="`cat $WORKDIR/pmusic-PLAYLIST_NAME`"
	if [ ! "$PLAYLIST_NAME" ]; then PLAYLIST_NAME="`ls $HOME/.pmusic/storage_playlist/ | tail -n 1`"; fi
	TMP="`ls $HOME/.pmusic/storage_playlist/ | grep --before-context=1 -x "$PLAYLIST_NAME" | head -n 1`"
	cp $HOME/.pmusic/storage_playlist/"$TMP" $WORKDIR/pmusic-playlist
	echo "$TMP" > $WORKDIR/pmusic-PLAYLIST_NAME
	;;
-redo)
	PLAYLIST_NAME="`cat $WORKDIR/pmusic-PLAYLIST_NAME`"
	TMP="`ls $HOME/.pmusic/storage_playlist/ | grep --after-context=1 -x "$PLAYLIST_NAME" | tail -n 1`"
	cp $HOME/.pmusic/storage_playlist/"$TMP" $WORKDIR/pmusic-playlist
	echo "$TMP" > $WORKDIR/pmusic-PLAYLIST_NAME
	;;
-search_for_song_in_playlists)
	echo "$LOC500..." > $WORKDIR/pmusic-splashtext
	echo 0 > $WORKDIR/pmusic-splash #reset progress bar
	$APPDIR/box_splash_progress &
	#---
	SELECTION="`cat $WORKDIR/pmusic-SELECTION`"
	COUNT=1
	while read I; do
		TMP_SONG="`basename "$SELECTION"`"
		TMP_PLAYLIST="`echo "$I" | cut -d '|' -f 1 | sed -e "s/  \//\//g"`"
		grep -l "$TMP_SONG" "$TMP_PLAYLIST" >> $WORKDIR/pmusic-song_info
		#update progress
		TMP=`$HOME/.pmusic/index_playlists | wc -l`
		PERCENT=`expr $COUNT \* 100 / $TMP`
		echo $PERCENT > $WORKDIR/pmusic-splash #update progressbar
		COUNT=`expr $COUNT + 1`
	done < $HOME/.pmusic/index_playlists
	echo 100 > $WORKDIR/pmusic-splash #reset progress bar
	;;
-song_info)
	if [ ! -s $WORKDIR/pmusic-SELECTION ]; then
		TXT1="<b>$LOC501</b>"
		. $APPDIR/box_ok
		exit
	fi
	echo -e "\c" > $WORKDIR/pmusic-song_info
	SELECTION="`cat $WORKDIR/pmusic-SELECTION`"
	echo 'Bitrate: unknown
Frequency: unknown' > $WORKDIR/pmusic-id3_info
	if [ "`echo "$SELECTION" | grep -i "('$\|\.ape$\|\.mp3$\|\.ogg$\|\.wma$\|\.{END}$ ')"`" ]; then
		ID3_ARTIST="`id3info "$SELECTION" | grep 'Lead performer' | cut -d ':' -f 2- | cut -b 2-`"
		ID3_TITLE="`id3info "$SELECTION" | grep 'Title' | cut -d ':' -f 2- | cut -b 2-`"
		ID3_ALBUM="`id3info "$SELECTION" | grep 'Album' | cut -d ':' -f 2- | cut -b 2-`"
		ID3_YEAR="`id3info "$SELECTION" | grep 'Year' | cut -d ':' -f 2- | cut -b 2-`"
		ID3_TRACK="`id3info "$SELECTION" | grep 'Track number' | cut -d ':' -f 2- | cut -b 2-`"
		ID3_GENRE="`id3info "$SELECTION" | grep 'Content' | cut -d ':' -f 2- | cut -b 3- | cut -d ')' -f 1`"
		ID3_COMMENT="`id3info "$SELECTION" | grep 'Comments' | grep -v ID3v1 | cut -d ':' -f 3- | cut -b 2-`"
		id3info "$SELECTION" | tail -n 3 > $WORKDIR/pmusic-id3_info
		VISIBLE_ID3=enabled
	else
		VISIBLE_ID3=disabled
	fi
	export Pmusic_info="
<window title=\"Pmusic\">
<vbox>
 <frame>
  <text use-markup=\"true\"><label>\"<b>$SELECTION</b>\"</label></text>
 </frame>
 <notebook labels=\"$LOC250|$LOC251\">
  <vbox>
   <hbox>
    <button>
     <label>$LOC252</label>
     <input file icon=\"gtk-clear\"></input>
     <visible>$VISIBLE_ID3</visible>
     <action type=\"exit\">clear_tag</action>
    </button>
   </hbox>
   <hbox>
    <text><label>$LOC_ARTIST</label><visible>$VISIBLE_ID3</visible></text>
    <entry>
     <variable>ID3_ARTIST</variable>
     <input>echo $ID3_ARTIST</input>
     <visible>$VISIBLE_ID3</visible>
    </entry>
   </hbox>
   <hbox>
    <text><label>$LOC_TITLE</label><visible>$VISIBLE_ID3</visible></text>
    <entry>
     <variable>ID3_TITLE</variable>
     <input>echo $ID3_TITLE</input>
     <visible>$VISIBLE_ID3</visible>
    </entry>
   </hbox>
   <hbox>
    <text><label>$LOC_ALBUM</label><visible>$VISIBLE_ID3</visible></text>
    <entry>
     <variable>ID3_ALBUM</variable>
     <input>echo $ID3_ALBUM</input>
     <visible>$VISIBLE_ID3</visible>
    </entry>
   </hbox>
   <hbox>
    <text><label>$LOC_YEAR</label><visible>$VISIBLE_ID3</visible></text>
    <entry>
     <variable>ID3_YEAR</variable>
     <input>echo $ID3_YEAR</input>
     <visible>$VISIBLE_ID3</visible>
    </entry>
   </hbox>
   <hbox>
    <text><label>$LOC_TRACK</label><visible>$VISIBLE_ID3</visible></text>
    <entry>
     <variable>ID3_TRACK</variable>
     <input>echo $ID3_TRACK</input>
     <visible>$VISIBLE_ID3</visible>
    </entry>
   </hbox>
   <hbox>
    <text><label>$LOC_GENRE</label><visible>$VISIBLE_ID3</visible></text>
    <entry>
     <variable>ID3_GENRE</variable>
     <input>echo $ID3_GENRE</input>
     <visible>$VISIBLE_ID3</visible>
    </entry>
   </hbox>
   <hbox>
    <text><label>$LOC253</label><visible>$VISIBLE_ID3</visible></text>
    <entry>
     <variable>ID3_COMMENT</variable>
     <input>echo $ID3_COMMENT</input>
     <visible>$VISIBLE_ID3</visible>
    </entry>
   </hbox>
  </vbox>
  <vbox>
   <text><input file>$WORKDIR/pmusic-id3_info</input></text>
   <text><label>\"\"</label></text>
   <text><label>$LOC254:</label></text>
   <hbox>
    <edit><variable>EDIT_SONG_INFO</variable><input file>$WORKDIR/pmusic-song_info</input></edit>
    <button>
     <input file icon=\"gtk-find\"></input>
     <label>Search</label>
     <action>$APPDIR/func -search_for_song_in_playlists</action>
     <action>refresh:EDIT_SONG_INFO</action>
    </button>
   </hbox>
  </vbox>
 </notebook>
 <hbox>
  <button>
   <label>$LOC_CANCEL</label>
   <input file icon=\"gtk-cancel\"></input>
  </button>
  <button>
   <label>$LOC_OK</label>
   <input file icon=\"gtk-ok\"></input>
   <action type=\"exit\">ok</action>
  </button>
 </hbox>
</vbox>
</window>"
	I=$IFS; IFS=""
	for STATEMENTS in $($GTKDIALOG --program=Pmusic_info --center); do
		eval $STATEMENTS
	done
	IFS=$I
	case $EXIT in
		ok)
			id3tag -a "$ID3_ARTIST" -s "$ID3_TITLE" -A "$ID3_ALBUM" -y "$ID3_YEAR" -t$ID3_TRACK -g "$ID3_GENRE" -c "$ID3_COMMENT" "$SELECTION"
			;;
		clear_tag)
			id3tag -a ' ' -s ' ' -A ' ' -y ' ' -t99 -g ' ' -c ' ' "$SELECTION"
			;;
	esac
	;;
-copy_files)
	GET_OUTDIR="true"; TITLE="- $LOC502"; . $APPDIR/box_chooser
	if [ $EXIT = "OK" ]; then
		while read I; do
			FILE="`echo "$I" | cut -d '|' -f 4`"
			TMP="`basename "$FILE"`"
			 if [ -f "$CHOOSER/$TMP" ]; then #already exist
				IMG=warning
				FRAME="$LOC_WARNING"
				TXT1="<b>$FILE $LOC503</b>"
				TXT2="$LOC504"
				. $APPDIR/box_yesno
				if [ $EXIT = yes ]; then 
					cp "$FILE" "$CHOOSER"
				fi
			else
				cp "$FILE" "$CHOOSER"
			fi
		done < $WORKDIR/pmusic-playlist
	fi
	;;
-burn)
	if [ ! `which pburn` ]; then
		TXT1="<b>Pburn</b> $LOC505"
		. $APPDIR/box_ok
		exit
	fi
	cut -d '|' -f 4 $WORKDIR/pmusic-playlist > $WORKDIR/pmusic-tmp
	pburn -i $WORKDIR/pmusic-tmp
	;;
-preferences)
	export THEMES=`ls "$APPDIR"/themes | awk '{print "<item>"$1"</item>"}'` #make theme list
	export LANGUAGES=`ls "$APPDIR"/locals | awk '{print "<item>"$1"</item>"}'` #make theme list
	export Pmusic_preferences="
<window title=\"Pmusic\">
<vbox>
 <notebook labels=\"$LOC260|$LOC261\">
  <vbox>
   <frame $LOC260>
    <hbox>
     <text><label>$LOC262</label></text>
     <combobox>
      <variable>THEME</variable>
      <item>$THEME</item>
      <item>$LOC263</item>
      $THEMES
     </combobox>
    </hbox>
    <hbox>
     <text><label>$LOC270</label></text>
     <combobox>
      <variable>LANGUAGE</variable>
      <item>$LANGUAGE</item>
      <item>auto</item>
      $LANGUAGES
     </combobox>
    </hbox>
    <checkbox>
     <label>$LOC264</label>
     <variable>HIGH_CPU</variable>
     <default>$HIGH_CPU</default>
    </checkbox>
    <checkbox>
     <label>$LOC265</label>
     <variable>SHOW_PROGRESSBAR</variable>
     <default>$SHOW_PROGRESSBAR</default>
    </checkbox>
   </frame>
  </vbox>
  <vbox>
   <frame $LOC261>
    <checkbox>
     <label>$LOC266</label>
     <variable>SHOW_PLAYLIST</variable>
     <default>$SHOW_PLAYLIST</default>
    </checkbox>
    <checkbox>
     <label>$LOC267</label>
     <variable>SHOW_MULTIPLE_PLAYLISTS</variable>
     <default>$SHOW_MULTIPLE_PLAYLISTS</default>
    </checkbox>
    <text><label>\"\"</label></text>
    <text><label>$LOC268</label></text>
    <entry activates-default=\"true\"><variable>PLAYLIST1_NAME</variable><input>echo '$PLAYLIST1_NAME'</input></entry>
    <entry activates-default=\"true\"><variable>PLAYLIST2_NAME</variable><input>echo '$PLAYLIST2_NAME'</input></entry>
    <entry activates-default=\"true\"><variable>PLAYLIST3_NAME</variable><input>echo '$PLAYLIST3_NAME'</input></entry>
    <entry activates-default=\"true\"><variable>PLAYLIST4_NAME</variable><input>echo '$PLAYLIST4_NAME'</input></entry>
   </frame>
  </vbox>
 </notebook>
 <text use-markup=\"true\"><label>\"<b>$LOC269</b>\"</label></text>
 <hbox>
  <button>
   <input file icon=\"gtk-cancel\"></input>
   <label>$LOC_CANCEL</label>
  </button>
  <button can-default=\"true\" has-default=\"true\" use-stock=\"true\">
   <input file icon=\"gtk-apply\"></input>
   <label>$LOC_OK</label>
   <action type=\"exit\">ok</action>
  </button>
 </hbox>
</vbox>
</window>"
	I=$IFS; IFS=""
	for STATEMENTS in $($GTKDIALOG --program=Pmusic_preferences --center); do
		eval $STATEMENTS
	done
	IFS=$I
	if [ "$EXIT" = "ok" ]; then
		echo "#Pmusic config"								 > $CONFIG
		echo "export VERSION=$VERSION"						>> $CONFIG
		echo "export THEME=\"$THEME\""						>> $CONFIG
		echo "export LANGUAGE=$LANGUAGE"					>> $CONFIG
		echo "export HIGH_CPU=$HIGH_CPU"					>> $CONFIG
		echo "export SHOW_PROGRESSBAR=$SHOW_PROGRESSBAR"	>> $CONFIG
		echo "export SHOW_PLAYLIST=$SHOW_PLAYLIST"			>> $CONFIG
		echo "export SHOW_MULTIPLE_PLAYLISTS=$SHOW_MULTIPLE_PLAYLISTS"	>> $CONFIG
		echo "export PLAYLIST1_NAME=\"$PLAYLIST1_NAME\""	>> $CONFIG
		echo "export PLAYLIST2_NAME=\"$PLAYLIST2_NAME\""	>> $CONFIG
		echo "export PLAYLIST3_NAME=\"$PLAYLIST3_NAME\""	>> $CONFIG
		echo "export PLAYLIST4_NAME=\"$PLAYLIST4_NAME\""	>> $CONFIG
	fi
	;;
-show_playlist1234)
	if [ ! -f $WORKDIR/pmusic-playlist1 ]; then echo -e "\c" > $WORKDIR/pmusic-playlist1; fi
	if [ ! -f $WORKDIR/pmusic-playlist2 ]; then echo -e "\c" > $WORKDIR/pmusic-playlist2; fi
	if [ ! -f $WORKDIR/pmusic-playlist3 ]; then echo -e "\c" > $WORKDIR/pmusic-playlist3; fi
	if [ ! -f $WORKDIR/pmusic-playlist4 ]; then echo -e "\c" > $WORKDIR/pmusic-playlist4; fi
	if [ $RADIO_PLAYLIST1 = true ]; then 
		TMP="`cat $WORKDIR/pmusic-PLAYLIST1234`"
		cp $WORKDIR/pmusic-playlist $WORKDIR/pmusic-"$TMP"
		cp $WORKDIR/pmusic-playlist1 $WORKDIR/pmusic-playlist
	fi
	if [ $RADIO_PLAYLIST2 = true ]; then 
		TMP="`cat $WORKDIR/pmusic-PLAYLIST1234`"
		cp $WORKDIR/pmusic-playlist $WORKDIR/pmusic-"$TMP"
		cp $WORKDIR/pmusic-playlist2 $WORKDIR/pmusic-playlist
	fi
	if [ $RADIO_PLAYLIST3 = true ]; then 
		TMP="`cat $WORKDIR/pmusic-PLAYLIST1234`"
		cp $WORKDIR/pmusic-playlist $WORKDIR/pmusic-"$TMP"
		cp $WORKDIR/pmusic-playlist3 $WORKDIR/pmusic-playlist
	fi
	if [ $RADIO_PLAYLIST4 = true ]; then 
		TMP="`cat $WORKDIR/pmusic-PLAYLIST1234`"
		cp $WORKDIR/pmusic-playlist $WORKDIR/pmusic-"$TMP"
		cp $WORKDIR/pmusic-playlist4 $WORKDIR/pmusic-playlist
	fi
	;;
-rename)
	PLAYLIST="`cat $WORKDIR/pmusic-SELECTION`"
	if [ -z "$PLAYLIST" ]; then
		TXT1="<b>$LOC501</b>"
		. $APPDIR/box_ok
	else
		NAME="`basename "$PLAYLIST" | sed -e 's/\.[^\.]*$//'`"
		EXTENSION="`basename "$PLAYLIST" | sed -e "s/^.*\.//g"`"
#		basename "`echo "$I"`" | sed -e 's/\.[^\.]*$//' > $WORKDIR/pmusic-tmp
#		NAME="`cat $WORKDIR/pmusic-tmp`"
		DIR="`dirname "$PLAYLIST"`"
		FRAME="$LOC132"; IMG="convert"; TXT1="$NAME"; DEFAULT="$NAME"
		. $APPDIR/box_input
		if [ $EXIT = "ok" ]; then
			echo > $WORKDIR/pmusic-error
			mv "$DIR"/"$NAME"."$EXTENSION" "$DIR"/"$INPUT"."$EXTENSION" 2> $WORKDIR/pmusic-error
			TXT1=`cat $WORKDIR/pmusic-error`
			if [ -n "$TXT1" ]; then
				. $APPDIR/box_ok #error
			else
				sed -i -e "s%$NAME%$INPUT%" $WORKDIR/pmusic-playlist
				sed -i -e "s%$DIR/$NAME.$EXTENSION%$DIR/$INPUT.$EXTENSION%" $WORKDIR/pmusic-playlist
			fi
		fi
	fi
	echo -e "\c" > $WORKDIR/pmusic-SELECTION
	;;
-copy)
	PLAYLIST="`cat $WORKDIR/pmusic-SELECTION`"
	if [ -z "$PLAYLIST" ]; then
		TXT1="<b>$LOC501</b>"
		. $APPDIR/box_ok
	else
		FRAME="$LOC133"; IMG="copy"; TXT1="$PLAYLIST"; DEFAULT="$PLAYLIST"
		. $APPDIR/box_input
		if [ $EXIT = "ok" ]; then
			echo > $WORKDIR/pmusic-error
			cp "$PLAYLIST" "$INPUT" 2> $WORKDIR/pmusic-error
			TXT1=`cat $WORKDIR/pmusic-error`
			if [ -n "$TXT1" ]; then
				. $APPDIR/box_ok #error
			else
				echo "$INPUT" > /tmp/picker-treeitems$PROGPID
			fi
		fi
	fi
	echo -e "\c" > $WORKDIR/pmusic-SELECTION
	;;
-delete)
	PLAYLIST="`cat $WORKDIR/pmusic-SELECTION`"
	if [ -z "$PLAYLIST" ]; then
		TXT1="<b>$LOC501</b>"
		. $APPDIR/box_ok
	else
		FRAME="$LOC134"; IMG="clear"; TXT1="$PLAYLIST"
		. $APPDIR/box_yesno
		if [ $EXIT = "yes" ]; then
			echo > $WORKDIR/pmusic-error
			rm "$PLAYLIST" 2> $WORKDIR/pmusic-error
			TXT1=`cat $WORKDIR/pmusic-error`
			if [ -n "$TXT1" ]; then
				. $APPDIR/box_ok #error
			else
				. $APPDIR/func -remove_from_list
			fi
		fi
	fi
	echo -e "\c" > $WORKDIR/pmusic-SELECTION
	;;
-set_SAVEFILE)
	echo "$CHOOSER" > $WORKDIR/pmusic-SAVEFILE
	;;
-save)
	export pmusic_save="
	<window title=\"Pmusic - $LOC280\" icon-name=\"gtk-save\">
	 <vbox>
	  <frame $LOC280>
	   <hbox>
	    <text><label>$LOC281</label></text>
	    <entry accept=\"file\">
	     <variable>SAVEFILE</variable>
	     <input>cat $WORKDIR/pmusic-last_added_playlist</input>
	    </entry>
	    <button>
	     <input file icon=\"gtk-open\"></input>
	     <action type=\"fileselect\">SAVEFILE</action>
	     <action>refresh:SAVEFILE</action>
	    </button>
	   </hbox>
	   <checkbox>
	    <label>$LOC282</label>
	    <variable>SAVE_PATH</variable>
	    <default>true</default>
	   </checkbox>
	  </frame>
	  <hbox>
	   <button><input file icon=\"gtk-cancel\"></input><label>$LOC_CANCEL</label></button>
	   <button can-default=\"true\" has-default=\"true\" use-stock=\"true\">
	    <input file icon=\"gtk-ok\"></input>
	    <label>$LOC_OK</label>
	    <action type=\"exit\">ok</action>
	   </button>
	  </hbox>
	 </vbox>
	</window>"
	I=$IFS; IFS=""
	for STATEMENTS in $($GTKDIALOG --program=pmusic_save --center); do
		eval $STATEMENTS
	done
	IFS=$I
	if [ "$EXIT" = "ok" ]; then
		SAVEFILE=`echo "$SAVEFILE" | sed -e "s%\.m3u%%g"` #remove extension if exist.
		if [ -s "$SAVEFILE".m3u ]; then #already exist
			IMG=warning
			FRAME="$LOC_WARNING"
			TXT1="<b>$SAVEFILE.m3u $LOC503</b>"
			TXT2="$LOC504"
			. $APPDIR/box_yesno
			if [ $EXIT = no ]; then 
				echo -e "\c" > $WORKDIR/pmusic-SAVEFILE
			fi
		fi
		if [ "$SAVEFILE" ]; then
			if [ $SAVE_PATH = true ]; then #keep path
				echo "`cut -d '|' -f 4 $WORKDIR/pmusic-playlist`" > "$SAVEFILE".m3u
			else #remove path
				echo -e "\c" > $WORKDIR/pmusic-tmp
				while read I; do
					basename "$I" >> $WORKDIR/pmusic-tmp
				done < $WORKDIR/pmusic-playlist
				cp $WORKDIR/pmusic-tmp "$SAVEFILE".m3u
			fi
			echo "$SAVEFILE".m3u > $WORKDIR/pmusic-last_added_playlist
		fi
	fi
	;;
-open_all)
	TMP="`dirname "$INDEX"`/"
	find -P "$TMP" -printf "  %p|%f\n" | grep -i --extended-regexp "$AUDIO_FORMATS" > $WORKDIR/pmusic.m3u
	cut -d '|' -f 1 $WORKDIR/pmusic.m3u | sort > $WORKDIR/pmusic-tmp
	mv -f $WORKDIR/pmusic-tmp $WORKDIR/pmusic.m3u
	INDEX="$WORKDIR/pmusic.m3u"
	. $APPDIR/func -add
	;;
-add_random)
	oIFS=$IFS IFS=$'\n' LINES=($(<"$HOME/.pmusic/index")) IFS=$oIFS
	n=${#LINES[@]}
	r=$((RANDOM % n))
	INDEX="`echo "${LINES[r]}" | cut -d '|' -f 1`"
	. $APPDIR/func -add
	;;
-add_all)
	TMP=`cat $HOME/.pmusic/index | wc -l`
	if [ $TMP -gt 200 ]; then
		IMG=dialog-warning
		FRAME="$LOC_WARNING"
		TXT1="<b>$LOC510</b>"
		TXT2="$LOC511 $TMP $LOC512"
		. $APPDIR/box_yesno
		if [ $EXIT = no ]; then exit; fi
	fi
	cut -d '|' -f 1 $HOME/.pmusic/index 2> /dev/null > $WORKDIR/pmusic.m3u
	INDEX="$WORKDIR/pmusic.m3u"
	. $APPDIR/func -add
	;;
-add)
	if [ ! -s $WORKDIR/pmusic-playlist ]; then AUTOPLAY=true; fi #if nothing in playlist, play added song.
	INDEX="`echo "$INDEX" | sed -e "s/  \//\//g"`"
	#m3u playlist
	if [ "`echo "$INDEX" | grep -iE "\.m3u|\.dir"`" ]; then
		echo "$LOC506..." > $WORKDIR/pmusic-splashtext
		echo 0 > $WORKDIR/pmusic-splash #reset progress bar
		$APPDIR/box_splash_progress &
		echo "$INDEX" > $WORKDIR/pmusic-last_added_playlist
		grep -v '#' "$INDEX" | sed -e "s%\r%%g" > $WORKDIR/pmusic-tmp #remove metadata of playlist
		#check if path is valid in playlist
		echo -e "\c" > $WORKDIR/pmusic-songs_in_list
		while read I; do
			if [ "`echo "$I" | grep '/'`" ]; then #path exist, use path of playlist
				echo "$I" >> $WORKDIR/pmusic-songs_in_list
			else #no path for files in list. Use current dir
				TMP="`dirname "$INDEX"`"
				echo "$TMP/$I" >> $WORKDIR/pmusic-songs_in_list
			fi
		done < $WORKDIR/pmusic-tmp
	fi
	#pls playlist
	if [ "`echo "$INDEX" | grep -i '.pls'`" ]; then
		echo "$LOC506..." > $WORKDIR/pmusic-splashtext
		echo 0 > $WORKDIR/pmusic-splash #reset progress bar
		$APPDIR/box_splash_progress &
		echo "$INDEX" > $WORKDIR/pmusic-last_added_playlist
		grep 'File' "$INDEX" | cut -d '=' -f 2 > $WORKDIR/pmusic-songs_in_list #use only FileX=
	fi
	#single file
	if [ "`echo "$INDEX" | grep -i --extended-regexp "$AUDIO_FORMATS"`" ]; then
		echo "$INDEX" > $WORKDIR/pmusic-songs_in_list
	fi
	SELECTION="`cat $WORKDIR/pmusic-SELECTION`"
	COUNT=1
	while read I; do
		if [ "`cat $WORKDIR/pmusic-USE_TAGS 2> /dev/null`" = "true" ]; then #use meta info in playlist AND in index
			TMP="`id3info "$I" | grep 'Lead performer' | cut -d ':' -f 2- | cut -b 2-`" #artist
			TMP2="`id3info "$I" | grep 'Title' | cut -d ':' -f 2- | cut -b 2-`" #title
			if [ "$TMP" ] && [ "$TMP" != " " ]; then
				NAME="$TMP - $TMP2"
			else #use filename
				basename "`echo "$I"`" | sed -e 's/\.[^\.]*$//' > $WORKDIR/pmusic-tmp
				NAME="`cat $WORKDIR/pmusic-tmp`"
			fi
		else #use filename
			basename "`echo "$I"`" | sed -e 's/\.[^\.]*$//' > $WORKDIR/pmusic-tmp
			NAME="`cat $WORKDIR/pmusic-tmp`"
		fi
		ffmpeg -i "$I" 2> $WORKDIR/pmusic-tmp2
		LENGTH=`cat $WORKDIR/pmusic-tmp2 | grep Duration | awk '{print $2}' | cut -d "." -f 1 | cut -d ":" -f 2-`
		LINE_SELECTED="`grep "$SELECTION" $WORKDIR/pmusic-playlist`" 
		NR=`echo "$LINE_SELECTED" | cut -d '|' -f 1`
		NR=`expr $NR + 1`
		if [ $ADD_AFTER_SELECTION = true ] && [ "$SELECTION" ] && [ $NR -le `cat $WORKDIR/pmusic-playlist | wc -l` ]; then #add the song after selection
			LINE_NEW="|$NAME|$LENGTH|$I"
			if [ "`cat $WORKDIR/pmusic-playlist | grep --before-context=1 "$LINE_SELECTED" | head -n 1 | cut -d '|' -f 2-`" != "`echo $LINE_NEW | cut -d '|' -f 2-`" ]; then #do not add same song twice
				sed -i $NR\i\\"$LINE_NEW" $WORKDIR/pmusic-playlist
			fi
		else #add at the end of list
			if [ ! "`tail -n 1 $WORKDIR/pmusic-playlist | grep "$NAME"`" ]; then #do not add same song twice
				echo "|$NAME|$LENGTH|$I" >> $WORKDIR/pmusic-playlist
			fi
		fi
		TMP=`cat $WORKDIR/pmusic-songs_in_list | wc -l`
		PERCENT=`expr $COUNT \* 100 / $TMP`
		echo $PERCENT > $WORKDIR/pmusic-splash #update progressbar
		COUNT=`expr $COUNT + 1`
	done < $WORKDIR/pmusic-songs_in_list
	$APPDIR/func -update_numbering
	#if nothing was previously in playlist, play added song.
	if [ "$AUTOPLAY" = "true" ]; then
		$APPDIR/func -stop
		echo "`head -n 1 $WORKDIR/pmusic-playlist | cut -d '|' -f 4`" > $WORKDIR/pmusic-PLAYLIST
		$APPDIR/func -playing &
	fi
	echo 100 > $WORKDIR/pmusic-splash #be sure to close progress dialog
	;;
-new)
	echo -e "\c" > $WORKDIR/pmusic-playlist
	echo -e "\c" > $WORKDIR/pmusic-SELECTION
	;;
-repeat)
	echo "$REPEAT" > $WORKDIR/pmusic-REPEAT
	;;
-shuffle)
	echo "$SHUFFLE" > $WORKDIR/pmusic-SHUFFLE
	;;
-to_top)
	#is something selected
	if [ -z "$PLAYLIST" ]; then
		TXT1="<b>$LOC501</b>"
		. $APPDIR/box_ok
		exit
	fi
	grep -w "$PLAYLIST" $WORKDIR/pmusic-playlist > $WORKDIR/pmusic-tmp
	grep -v "$PLAYLIST" $WORKDIR/pmusic-playlist >> $WORKDIR/pmusic-tmp
	mv -f $WORKDIR/pmusic-tmp $WORKDIR/pmusic-playlist
	. $APPDIR/func -update_numbering
	;;
-to_bottom)
	#is something selected
	if [ -z "$PLAYLIST" ]; then
		TXT1="<b>$LOC501</b>"
		. $APPDIR/box_ok
		exit
	fi
	grep -v "$PLAYLIST" $WORKDIR/pmusic-playlist > $WORKDIR/pmusic-tmp
	grep -w "$PLAYLIST" $WORKDIR/pmusic-playlist >> $WORKDIR/pmusic-tmp
	mv -f $WORKDIR/pmusic-tmp $WORKDIR/pmusic-playlist
	. $APPDIR/func -update_numbering
	;;
-up)
	#define PLAYLIST for several moves
	if [ "$PLAYLIST" ]; then
		echo "$PLAYLIST" > $WORKDIR/pmusic-SELECTION
	else
		PLAYLIST=`cat $WORKDIR/pmusic-SELECTION`
	fi
	#is something selected
	if [ -z "$PLAYLIST" ]; then
		TXT1="<b>$LOC501</b>"
		. $APPDIR/box_ok
		exit
	fi
	#start swapping
	LINE_UP_TXT=`grep -m 1 "$PLAYLIST" $WORKDIR/pmusic-playlist`
	LINE_UP_NR=`grep -m 1 -n "$PLAYLIST" $WORKDIR/pmusic-playlist | awk -F : '{print $1}' | head -1`
	LINE_DOWN_TXT=`grep -m 1 -B1 "$PLAYLIST" $WORKDIR/pmusic-playlist | sed -e "s%$LINE_UP_TXT%%g"`
	LINE_DOWN_NR=`expr $LINE_UP_NR - 1`
	cp $WORKDIR/pmusic-playlist $WORKDIR/pmusic-tmp
	case $LINE_UP_NR in
		1)	exit;;
		2)	echo -e "\c" > $WORKDIR/pmusic-playlist;;
		*)	sed '1,'`expr $LINE_DOWN_NR "-" 1`'!d' $WORKDIR/pmusic-tmp > $WORKDIR/pmusic-playlist;;
	esac
	echo "$LINE_UP_TXT" >> $WORKDIR/pmusic-playlist
	echo "$LINE_DOWN_TXT" >> $WORKDIR/pmusic-playlist
	sed `expr $LINE_UP_NR + 1`',$!d' $WORKDIR/pmusic-tmp >> $WORKDIR/pmusic-playlist
	grep -v "^$" $WORKDIR/pmusic-playlist > $WORKDIR/pmusic-tmp #remove empty lines
	mv -f $WORKDIR/pmusic-tmp $WORKDIR/pmusic-playlist
	. $APPDIR/func -update_numbering
	;;
-down)
	#define PLAYLIST for several moves
	if [ "$PLAYLIST" ]; then
		echo "$PLAYLIST" > $WORKDIR/pmusic-SELECTION
	else
		PLAYLIST=`cat $WORKDIR/pmusic-SELECTION`
	fi
	#is something selected
	if [ -z "$PLAYLIST" ]; then
		TXT1="<b>$LOC501</b>"
		. $APPDIR/box_ok
		exit
	fi
	#start swapping
	LINE_DOWN_TXT=`grep -m 1 "$PLAYLIST" $WORKDIR/pmusic-playlist`
	LINE_DOWN_NR=`grep -m 1 -n "$PLAYLIST" $WORKDIR/pmusic-playlist | awk -F : '{print $1}' | head -1`
	LINE_UP_TXT=`grep -m 1 -A1 "$PLAYLIST" $WORKDIR/pmusic-playlist | sed -e "s%$LINE_DOWN_TXT%%g"`
	LINE_UP_NR=`expr $LINE_DOWN_NR + 1`
	TOTAL_LINES=`cat $WORKDIR/pmusic-playlist | wc -l`
	cp $WORKDIR/pmusic-playlist $WORKDIR/pmusic-tmp
	if [ $LINE_DOWN_NR -gt 1 ]; then
		sed '1,'`expr $LINE_DOWN_NR "-" 1`'!d' $WORKDIR/pmusic-tmp > $WORKDIR/pmusic-playlist
		echo "$LINE_UP_TXT" >> $WORKDIR/pmusic-playlist
	else
		echo "$LINE_UP_TXT" > $WORKDIR/pmusic-playlist
	fi
	echo "$LINE_DOWN_TXT" >> $WORKDIR/pmusic-playlist
	case $LINE_UP_NR in
		$TOTAL_LINES)	break;;
		*)				sed `expr $LINE_UP_NR + 1`',$!d' $WORKDIR/pmusic-tmp >> $WORKDIR/pmusic-playlist;;
	esac
	grep -v "^$" $WORKDIR/pmusic-playlist > $WORKDIR/pmusic-tmp #remove empty lines
	mv -f $WORKDIR/pmusic-tmp $WORKDIR/pmusic-playlist
	. $APPDIR/func -update_numbering
	;;
-remove_from_list)
	#is something selected
	if [ -z "$PLAYLIST" ]; then
		TXT1="<b>$LOC501</b>"
		. $APPDIR/box_ok
		exit
	fi
	grep -v "$PLAYLIST" $WORKDIR/pmusic-playlist > $WORKDIR/pmusic-tmp
	mv -f $WORKDIR/pmusic-tmp $WORKDIR/pmusic-playlist
#	echo -e "\c" >> $WORKDIR/pmusic-playlist
	. $APPDIR/func -update_numbering
	;;
-update_numbering)
	NR=1
	echo -e "\c" > $WORKDIR/pmusic-tmp
	while read I; do
		TMP=`echo $I | cut -d '|' -f 2-`
		echo "$NR|$TMP" >> $WORKDIR/pmusic-tmp
		NR=`expr $NR + 1`
	done < $WORKDIR/pmusic-playlist
	cp $WORKDIR/pmusic-tmp $WORKDIR/pmusic-playlist
	case "`cat $WORKDIR/pmusic-PLAYLIST1234 2> /dev/null`" in
		playlist1)
			cp $WORKDIR/pmusic-tmp $WORKDIR/pmusic-playlist1;;
		playlist2)
			cp $WORKDIR/pmusic-tmp $WORKDIR/pmusic-playlist2;;
		playlist3)
			cp $WORKDIR/pmusic-tmp $WORKDIR/pmusic-playlist3;;
		playlist4)
			cp $WORKDIR/pmusic-tmp $WORKDIR/pmusic-playlist4;;
	esac
	;;
-search)
	TEXT="`cat $HOME/.pmusic/index_all`"
	for I in $SEARCH; do
		TEXT="`echo "$TEXT" | grep -i "$I"`"
	done
	echo "$TEXT" | sort -u > $HOME/.pmusic/index
	;;
-update_index_check)
	#check if first time
	if [ ! -f $HOME/.pmusic/index_all ]; then
		TXT_FIRST_INDEX="$LOC301"
		. $APPDIR/func -update_index
	fi
	;;
-update_index)
	if [ "`cat $WORKDIR/pmusic-USE_TAGS 2> /dev/null`" = "true" ]; then
		USE_TAGS=true
		VISIBLE_INDEX_CHANGES=enabled
	else
		USE_TAGS=false
		VISIBLE_INDEX_CHANGES=disabled
	fi
	echo 0 > $WORKDIR/pmusic-index_splash #reset progress bar
	export Pmusic_index="	
<window title=\"Pmusic\">
<vbox>
<text use-markup=\"true\"><label>\"<b>$LOC302</b>\"</label></text>
<text><label>\"$TXT_FIRST_INDEX\"</label></text>
<hbox>
 <tree headers_visible=\"false\">
  <label>a</label>
  <variable>INDEX_SOURCE</variable>
  <input>cat $HOME/.pmusic/index_directories</input>
  <width>250</width><height>150</height>
 </tree>
 <vbox>
  <button>
   <input file icon=\"gtk-add\"></input>
   <action>. $APPDIR/func -add_to_indexlist</action>
   <action>refresh:INDEX_SOURCE</action>
  </button>
  <button>
   <input file icon=\"gtk-remove\"></input>
   <action>. $APPDIR/func -remove_from_indexlist \$INDEX_SOURCE</action>
   <action>refresh:INDEX_SOURCE</action>
  </button>
 </vbox>
</hbox>
 <frame $LOC303>
  <checkbox>
   <label>$LOC304</label>
   <variable>USE_TAGS</variable>
   <default>$USE_TAGS</default>
   <action>if true enable:INDEX_CHANGES</action>
   <action>if true enable:INDEX_DIR</action>
   <action>if true enable:TXT_INDEX_DIR</action>
   <action>if false disable:INDEX_CHANGES</action>
   <action>if false disable:INDEX_DIR</action>
   <action>if false disable:TXT_INDEX_DIR</action>
  </checkbox>
  <hbox>
   <text>
    <variable>TXT_INDEX_DIR</variable>
    <label>$LOC305</label>
    <visible>$VISIBLE_INDEX_CHANGES</visible>
   </text>
   <combobox>
    <variable>INDEX_DIR</variable>
    <item>../$LOC_ARTIST - $LOC_ALBUM/$LOC223</item>
    <item>../$LOC_ARTIST/$LOC_ALBUM/$LOC223</item>
    <item>../$LOC_ARTIST/$LOC223</item>
    <item>$LOC306</item>
    <visible>$VISIBLE_INDEX_CHANGES</visible>
   </combobox>
  </hbox>
  <checkbox>
   <label>$LOC307</label>
   <variable>INDEX_CHANGES</variable>
   <default>true</default>
   <visible>$VISIBLE_INDEX_CHANGES</visible>
  </checkbox>
 </frame>
 <hbox>
  <button>
   <input file icon=\"gtk-cancel\"></input>
   <label>$LOC_CANCEL</label>
   <action type=\"exit\">cancel</action>
  </button>
  <button can-default=\"true\" has-default=\"true\" use-stock=\"true\">
   <input file icon=\"gtk-apply\"></input>
   <label>$LOC308</label>
   <action>$APPDIR/func_index &</action>
  </button>
 </hbox>
 <progressbar>
  <input>"'while [ "$I" != "100" ]; do I=`cat '"$WORKDIR"'/pmusic-index_splash`; echo $I; sleep 1; done'"</input>
  <action type=\"exit\">cancel</action>
 </progressbar>
</vbox>
</window>"
	I=$IFS; IFS=""
	for STATEMENTS in  $($GTKDIALOG --program=Pmusic_index --center); do
		eval $STATEMENTS
	done
	IFS=$I
	if [ "$EXIT" = "cancel" ]; then
		for I in `ps | grep -i pmusic | grep index | awk '{print $1}'`; do kill -9 $I; done
	fi
	;;
-add_to_indexlist)
	GET_OUTDIR="true"; TITLE="- $LOC309"; . $APPDIR/box_chooser
	if [ "$EXIT" = "OK" ]; then
		if [ ! -d "$CHOOSER" ]; then #dir not found
			TXT1="<b>$CHOOSER $LOC507</b>"
			. $APPDIR/box_ok
		else
			echo "$CHOOSER" >> $HOME/.pmusic/index_directories
		fi
	fi
	;;
-remove_from_indexlist)
	grep -v "$INDEX_SOURCE" $HOME/.pmusic/index_directories > $WORKDIR/pmusic-tmp
	mv -f $WORKDIR/pmusic-tmp $HOME/.pmusic/index_directories
	;;
-index_songs)
	cp $HOME/.pmusic/index_alphabetic $HOME/.pmusic/index
	;;
-index_recent)
	NR=1000
	echo -e "\c" > $WORKDIR/pmusic-tmp
	while read I; do
		echo "$NR|  $I" >> $WORKDIR/pmusic-tmp
		NR=`expr $NR + 1` 
	done < $HOME/.pmusic/index_recent
	sort -r $WORKDIR/pmusic-tmp | cut -d '|' -f 2- > $HOME/.pmusic/index
	tail -n 1000 $WORKDIR/pmusic-tmp | cut -d '|' -f 2- > $HOME/.pmusic/index_recent #keep only the last 1000
	;;
-index_most_played)
	tail -n 1000 $HOME/.pmusic/index_most_played | sort -r > $WORKDIR/pmusic-tmp
	cut -d '|' -f 2- $WORKDIR/pmusic-tmp > $HOME/.pmusic/index
	;;
-index_playlists)
	cp $HOME/.pmusic/index_playlists $HOME/.pmusic/index
	;;
-slide)
	TMP="`cat $WORKDIR/pmusic-ffmpeg_length | cut -d '|' -f 2`"
	echo "$[$TMP*$2/100]" > $WORKDIR/pmusic-SS
	$APPDIR/func_kill
	$APPDIR/func -playing &
	;;
-jump_to) #start song with given time
	FRAME="$LOC155"; IMG=""; TXT1="$LOC508:"; DEFAULT='0:00'
	. $APPDIR/box_input
	if [ $EXIT = "ok" ]; then
		if [ "`echo $INPUT | grep ':'`" ]; then
			MIN=`echo $INPUT | cut -d ':' -f 1`
			SEC=`echo $INPUT | cut -d ':' -f 2`
			if [ ! $SEC ]; then SEC=0; fi
		else
			MIN=0
			SEC=$INPUT
		fi
		echo "$[($MIN*60)+$SEC]" > $WORKDIR/pmusic-SS
		$APPDIR/func_kill
		$APPDIR/func -playing &
	fi
	;;
-back)
	TREE_ITEM="`cat $WORKDIR/pmusic-TREE_ITEM`"
	PLAYLIST="`grep -w --before-context=1 "$TREE_ITEM" $WORKDIR/pmusic-playlist | cut -d '|' -f 4 | head -n 1`"
	echo "$PLAYLIST" > $WORKDIR/pmusic-PLAYLIST
	grep "$PLAYLIST" $WORKDIR/pmusic-playlist > $WORKDIR/pmusic-TREE_ITEM
	#start new process
	$APPDIR/func_kill
	echo 0 > $WORKDIR/pmusic-SS
	echo 0 > $WORKDIR/pmusic-PERCENT_BAR
	#---
	$APPDIR/func -playing &
	;;
-rewind)
	TIME=`tail -c 50 $WORKDIR/pmusic-ffmpeg_output | cut -d '=' -f 3 | cut -d ' ' -f 1 | cut -d . -f 1`
	SS_OLD=`cat $WORKDIR/pmusic-SS`
	if [ $SS_OLD -lt 30 ]; then TIME=0; SS_OLD=30; fi #rewind can return negative value
	if [ ! "`expr $TIME + 1`" ]; then TIME=0; fi #ffmpeg has not got time to return time
	echo `expr $TIME - 30 + $SS_OLD` > $WORKDIR/pmusic-SS
	#start new process
	echo true > $WORKDIR/pmusic-REW_FWD
	$APPDIR/func_kill
	$APPDIR/func -playing &
	;;
-stop)
	$APPDIR/func_kill
	echo "pmusic_stop" > $WORKDIR/pmusic-ffmpeg_output
	echo 0 > $WORKDIR/pmusic-SS
	echo true > $WORKDIR/pmusic-stop
	echo -e "\c" > $WORKDIR/pmusic-TREE_ITEM
	echo false > $WORKDIR/pmusic-REW_FWD
	usleep 100000 #wait for func_progressbar to update 
	echo 0 > $WORKDIR/pmusic-PERCENT_BAR
	;;
-play)
	TMP=`ps`
	if [ "`echo $TMP | grep aplay`" ]; then #aplay is running, use PAUSE
		if [ "`echo $TMP | grep aplay | grep ' T '`" ]; then #process is paused
			for I in `ps | grep aplay | awk '{print $1}'`; do kill -18 $I; done #continue
		else
			for I in `ps | grep aplay | awk '{print $1}'`; do kill -19 $I; done #pause
		fi
		echo true > $WORKDIR/pmusic-stop
	else #go to 'play song'
		$APPDIR/func -stop
		$APPDIR/func -playing &
	fi
	;;
-playing)
	SS=`cat $WORKDIR/pmusic-SS`
	PLAYLIST="`cat $WORKDIR/pmusic-PLAYLIST`"
	grep "$PLAYLIST" $WORKDIR/pmusic-playlist > $WORKDIR/pmusic-TREE_ITEM #holds all coloums
	##### add to index #####
	if [ "$PLAYLIST" ]; then #iso 8859-1 sometimes break $PLAYLIST
		#add to index_recent
		if [ ! "`tail -n 1 $HOME/.pmusic/index_recent | grep "$PLAYLIST"`" ]; then
			grep -w "$PLAYLIST" $HOME/.pmusic/index_alphabetic 2> /dev/null >> $HOME/.pmusic/index_recent
		fi
		#add to index_most_played
		if [ "`grep -w -m 1 "$PLAYLIST" $HOME/.pmusic/index_most_played`" ]; then #played before. - Increase status
			NR="`grep -w -m 1 "$PLAYLIST" $HOME/.pmusic/index_most_played | cut -d '|' -f 1`"
			TMP="`grep -w -m 1 "$PLAYLIST" $HOME/.pmusic/index_most_played | cut -d '|' -f 2-`"
			grep -v -w -m 1 "$PLAYLIST" $HOME/.pmusic/index_most_played > $HOME/.pmusic/tmp/pmusic-tmp
			NR_NEW=$[$NR+1]
			mv -f $HOME/.pmusic/tmp/pmusic-tmp $HOME/.pmusic/index_most_played
			echo "$NR_NEW|$TMP" >> $HOME/.pmusic/index_most_played
			#sed -i -e "s%$NR|$TMP%$NR_NEW|$TMP%g" $HOME/.pmusic/index_most_played #increase by 1
		else
			TMP="`grep -w -m 1 "$PLAYLIST" $HOME/.pmusic/index_alphabetic 2> /dev/null`"
			echo "10001|$TMP" >> $HOME/.pmusic/index_most_played
		fi
	fi
	######
	echo false > $WORKDIR/pmusic-stop #if stopped it shouldn't start next
	echo -e "\c" > $WORKDIR/pmusic-ffmpeg_output
	TMP="`cut -d '|' -f 3 $WORKDIR/pmusic-TREE_ITEM`"
	if [ "$TMP" = "00:00" ] || [ "$TMP" = "00:01" ]; then #if length is under 2 sec --> piping ffmpeg | aplay fails
		echo "ffmpeg -i \"$PLAYLIST\" -f au -y $WORKDIR/pmusic.au 2> /dev/null" > $WORKDIR/pmusic-exec
		echo "aplay $WORKDIR/pmusic.au 2> /dev/null" >> $WORKDIR/pmusic-exec
	else #Normal way
		echo "exec ffmpeg -i \"$PLAYLIST\" -ss $SS -f au - 2>> $WORKDIR/pmusic-ffmpeg_output | aplay -f cd 2> /dev/null" > $WORKDIR/pmusic-exec
	fi
	chmod 700 $WORKDIR/pmusic-exec
	if [ "$HIGH_CPU" = "true" ]; then
		nice --18 $WORKDIR/pmusic-exec 2> /dev/null &
	else
		$WORKDIR/pmusic-exec 2> /dev/null
	fi
	#loop until ffmpeg is finnished
	while [ "`cat $WORKDIR/pmusic-ffmpeg_output | wc -c`" != "$OLD_WC" ]; do #ffmpeg is working
		OLD_WC=`cat $WORKDIR/pmusic-ffmpeg_output | wc -c`
		sleep 1
	done
	#run next song in playlist
	if [ "`cat $WORKDIR/pmusic-stop`" = "false" ]; then $APPDIR/func -next; fi
	;;
-forward)
	TIME="`tail -c 50 $WORKDIR/pmusic-ffmpeg_output | cut -d '=' -f 3 | cut -d ' ' -f 1 | cut -d . -f 1`"
	SS_OLD=`cat $WORKDIR/pmusic-SS`
	if [ ! "$[$TIME+1]" ]; then TIME=0; fi #ffmpeg has not got time to return time
	echo $[$TIME+30+$SS_OLD] > $WORKDIR/pmusic-SS
	#start new process
	echo true > $WORKDIR/pmusic-REW_FWD
	$APPDIR/func_kill
	$APPDIR/func -playing &
	;;
-next)
	if [ "`cat $WORKDIR/pmusic-SHUFFLE 2> /dev/null`" = "true" ]; then #shuffle
		#get random line in playlist
		oIFS=$IFS IFS=$'\n' LINES=($(<"$WORKDIR/pmusic-playlist")) IFS=$oIFS
		n=${#LINES[@]}
		r=$((RANDOM % n))
		echo "${LINES[r]}" | cut -d '|' -f 4 > $WORKDIR/pmusic-PLAYLIST
	else #follow list, item by item
		TMP=`grep "from '" $WORKDIR/pmusic-ffmpeg_output | cut -d "'" -f 2- | sed -e "s/'://g"`
		if [ "`grep "$TMP" $WORKDIR/pmusic-playlist`" ]; then
			TREE_ITEM="`cat $WORKDIR/pmusic-TREE_ITEM`"
			PLAYLIST="`grep -w --after-context=1 "$TREE_ITEM" $WORKDIR/pmusic-playlist | cut -d '|' -f 4 | tail -n 1`"
			echo "$PLAYLIST" > $WORKDIR/pmusic-PLAYLIST
			#if last song
			if [ "$PLAYLIST" = "`echo $TREE_ITEM | cut -d '|' -f 4`" ]; then
				if [ "`cat $WORKDIR/pmusic-REPEAT 2> /dev/null`" = "true" ]; then #repeat
					head -n 1 $WORKDIR/pmusic-playlist | cut -d '|' -f 4 > $WORKDIR/pmusic-PLAYLIST
				else
					$APPDIR/func -stop
					exit
				fi
			fi
		else #if played song is not in playlist - play first song
			head -n 1 $WORKDIR/pmusic-playlist | cut -d '|' -f 4 > $WORKDIR/pmusic-PLAYLIST
		fi
	fi
	grep "$PLAYLIST" $WORKDIR/pmusic-playlist > $WORKDIR/pmusic-TREE_ITEM
	#start new process
	$APPDIR/func_kill
	echo 0 > $WORKDIR/pmusic-SS
	echo 0 > $WORKDIR/pmusic-PERCENT_BAR
	#---
	$APPDIR/func -playing &
	;;
esac
