#!/bin/bash
#pMusic - export
#GPL - Copyright Sigmund Berglund

export TEXTDOMAIN=pmusic
cd $HOME
echo ' ' > $WORKDIR/splash_copy_text #echo nothing will delete existing splash

. $APPDIR/gui_mixer

#read config
[ -f $WORKDIR/rcexport ] && . $WORKDIR/rcexport
#check if rc really exist
if [ ! "$COPY_BITRATE" ]; then
	COPYDIR=$HOME
	SPLIT_RADIOSTREAM=true
	ADD_NUMBERING=false
	FIRST_NR=1
	CHK_OUTPUT_STREAM=false
	COPY_BITRATE=192000
	COPY_FORMAT=mp3
	COPY_SAMPLE=44100
	COPY_CHANNEL=2
	CD_FILE_NAME='%a - %t'
	ADD_META=true
fi
echo "$COPY_FORMAT" > $WORKDIR/export_copy_format
echo "$COPY_BITRATE" > $WORKDIR/export_copy_bitrate
echo "$COPY_SAMPLE" > $WORKDIR/export_copy_sample
echo "$COPY_CHANNEL" > $WORKDIR/export_copy_channel
[ ! -s $WORKDIR/export_presets ] &&	echo "$(gettext 'Music medium quality')|mp3,192000,44100,2
$(gettext 'Music loseless quality')|flac,,,
$(gettext 'Audio-book compressed')|mp3,32000,22050,1
$(gettext 'CD-audio')|wav,1411000,44100,2" > $WORKDIR/export_presets

if [ $CHK_OUTPUT_STREAM = true ]; then VISIBLE_CONVERT=true; else VISIBLE_CONVERT=false; fi 
if [ $ADD_NUMBERING = true ]; then VISIBLE_ADDNR=true; else VISIBLE_ADDNR=false; fi 

help_tag (){
	export FRAME="$(gettext 'Info')"
	export IMG=info
	export TXT1="$(gettext 'pMusic will try to grab CD-disc information from the internet. You can decide which part of the info to use for naming of the copied tracks.')

<b>%a</b> - $(gettext 'Artist')
<b>%A</b> - $(gettext 'Album')
<b>%n</b> - $(gettext 'Track-number')
<b>%t</b> - $(gettext 'Title')"
	$APPDIR/box_ok &
}
export -f help_tag

GUI_CDAUDIO='
<vbox space-expand="false" space-fill="false">
<frame '$(gettext 'CD-Audio tracks')'>
 <hbox>
  <text><label>'$(gettext 'Define name of new files')'</label></text>
  <entry>
   <variable>CD_FILE_NAME</variable>
   <input>echo "'$CD_FILE_NAME'"</input>
  </entry>
  <button>
   <input file stock="gtk-help"></input>
   <action>help_tag</action>
  </button>
 </hbox>
 <checkbox tooltip-text="'$(gettext 'Works for')' .mp3 .ogg .wma .ape">
  <label>'$(gettext 'Add meta-information to new files')'</label>
  <default>'$ADD_META'</default>
  <variable>ADD_META</variable>
 </checkbox>
</frame>
</vbox>
'



S='
<window title="pMusic - '$(gettext 'Export')'" icon-name="pmusic">
 <vbox spacing="0" space-expand="false" space-fill="false">
  '"$($APPDIR/func_svg -heading "$(gettext 'Export tracks in playlist')")"'
  <frame '$(gettext 'Destination')'>
   <hbox>
    <text><label>'$(gettext 'Target directory')'</label></text>
    <entry accept="directory">
     <variable>COPYDIR</variable>
     <input>echo '$COPYDIR'</input>
    </entry>
    <button>
     <input file stock="gtk-open"></input>
     <action type="fileselect">COPYDIR</action>
    </button>
   </hbox>
   <text><label>""</label></text>
   <checkbox tooltip-text="'$(gettext 'Filename will be based on the meta information recieved together with the downloaded stream.')'">
    <label>'$(gettext 'Split radio-stream into tracks. (See also music source Radio Hits)')'</label>'
    if ! type streamripper > /dev/null 2>&1 || [ ! "`cat $WORKDIR/playlist $WORKDIR/PLAYLIST | grep -F 'http:'`" ]; then S=$S'<sensitive>false</sensitive><default>false</default>'; fi #also grep $WORKDIR/PLAYLIST for the radio-frontend which has no ordinary playlist 
    S=$S'
    <default>'$SPLIT_RADIOSTREAM'</default>
    <variable>SPLIT_RADIOSTREAM</variable>
   </checkbox>
   <hbox>
    <checkbox tooltip-text="'$(gettext 'Add numbering to keep sort order (not radio)')'">
     <label>'$(gettext 'Add numbering')'</label>
     <variable>ADD_NUMBERING</variable>
     <default>'$ADD_NUMBERING'</default>
     <action>if true enable:HBOX_FIRSTNR</action>
     <action>if false disable:HBOX_FIRSTNR</action>
    </checkbox>
    <text space-expand="true" space-fill="true"><label>""</label></text>
    <hbox>
     <text><label>'$(gettext 'First number')'</label></text>
     <hbox width-request="50">
      <entry accept="directory">
       <variable>FIRST_NR</variable>
       <input>echo '$FIRST_NR'</input>
      </entry>
     </hbox>
     <variable>HBOX_FIRSTNR</variable>
     <sensitive>'$VISIBLE_ADDNR'</sensitive>
    </hbox>
   </hbox>
   <checkbox tooltip-text="'$(gettext 'Converting is required for internet streams, - like tracks from jamendo.com or similar.')''
   type streamripper > /dev/null 2>&1 && S=$S"
   
$(gettext 'NOTE! Radio-streams can not be converted. They will be recorded in their original format (mp3/ogg). Converting has to be done in a second stage.')"
    S=$S'">
    <label>'$(gettext 'Convert exported tracks')'</label>
    <variable>CHK_OUTPUT_STREAM</variable>
    <default>'$CHK_OUTPUT_STREAM'</default>
    <action>if true enable:VBOX_CONVERT2</action>
    <action>if false disable:VBOX_CONVERT2</action>
   </checkbox>
  </frame>

  <vbox spacing="0" space-expand="true" space-fill="true">
    <frame '$(gettext 'Convert')'>
     <checkbox>
      <variable>BYPASS_FILTERS</variable>
      <default>true</default>
      <label>'$(gettext 'Bypass sound processing (keep original sound)')'</label>
      <action>if true disable:VBOX_SOUND_INPUT</action>
      <action>if false enable:VBOX_SOUND_INPUT</action>
     </checkbox>
     <notebook labels="'$(gettext 'Quality')'|'$(gettext 'Sound processing')'" space-expand="true" space-fill="true">
      <vbox space-expand="true" space-fill="true">'
       S=$S'<hbox spacing="0" space-expand="true" space-fill="true">
        <vbox spacing="0" space-expand="true" space-fill="true">
         <tree column-visible="true|false" exported_column="1" spacing="0" space-expand="true" space-fill="true">
          <label>'$(gettext 'Preset')'|b</label>
          <variable>EXPORT_PRESET</variable>
          <height>100</height><width>150</width>
          <input file>'$WORKDIR'/export_presets</input>
          <action signal="button-release-event">echo "$EXPORT_PRESET" | cut -d, -f1 > '$WORKDIR'/export_copy_format</action>
          <action signal="button-release-event">echo "$EXPORT_PRESET" | cut -d, -f2 > '$WORKDIR'/export_copy_bitrate</action>
          <action signal="button-release-event">echo "$EXPORT_PRESET" | cut -d, -f3 > '$WORKDIR'/export_copy_sample</action>
          <action signal="button-release-event">echo "$EXPORT_PRESET" | cut -d, -f4 > '$WORKDIR'/export_copy_channel</action>
          <action signal="button-release-event">refresh:COPY_FORMAT</action>
          <action signal="button-release-event">refresh:COPY_BITRATE</action>
          <action signal="button-release-event">refresh:COPY_SAMPLE</action>
          <action signal="button-release-event">refresh:COPY_CHANNEL</action>
          <action signal="button-release-event">echo COPY_FORMAT=$COPY_FORMAT</action>
          <action signal="button-release-event" condition="command_is_false([ $COPY_FORMAT != flac ] && echo false)">enable:VBOX_CONVERT1</action>
          <action signal="button-release-event" condition="command_is_true([ $COPY_FORMAT = flac ] && echo true)">disable:VBOX_CONVERT1</action>
         </tree>
        </vbox>
        <text width-request="20"><label>""</label></text>
        <vbox margin="10" space-expand="false" space-fill="false">
         <hbox space-expand="false" space-fill="false">
          <text><label>'$(gettext 'Audio format')'</label></text>
          <hbox width-request="80">
           <entry><variable>COPY_FORMAT</variable><input file>'$WORKDIR'/export_copy_format</input></entry>
          </hbox>
          <hbox width-request="30">
           <menubar>
            <menu>
             <menuitem><label>aiff</label><action>echo aiff > '$WORKDIR'/export_copy_format</action><action>refresh:COPY_FORMAT</action><action>enable:VBOX_CONVERT1</action></menuitem>
             <menuitem><label>asf</label><action>echo asf > '$WORKDIR'/export_copy_format</action><action>refresh:COPY_FORMAT</action><action>enable:VBOX_CONVERT1</action></menuitem>
             <menuitem><label>au</label><action>echo au > '$WORKDIR'/export_copy_format</action><action>refresh:COPY_FORMAT</action><action>enable:VBOX_CONVERT1</action></menuitem>
             <menuitem><label>flac</label><action>echo flac > '$WORKDIR'/export_copy_format</action><action>refresh:COPY_FORMAT</action><action>disable:VBOX_CONVERT1</action></menuitem>
             <menuitem><label>mp3</label><action>echo mp3 > '$WORKDIR'/export_copy_format</action><action>refresh:COPY_FORMAT</action><action>enable:VBOX_CONVERT1</action></menuitem>
             <menuitem><label>ogg</label><action>echo ogg > '$WORKDIR'/export_copy_format</action><action>refresh:COPY_FORMAT</action><action>enable:VBOX_CONVERT1</action></menuitem>
             <menuitem><label>rm</label><action>echo rm > '$WORKDIR'/export_copy_format</action><action>refresh:COPY_FORMAT</action><action>enable:VBOX_CONVERT1</action></menuitem>
             <menuitem><label>voc</label><action>echo voc > '$WORKDIR'/export_copy_format</action><action>refresh:COPY_FORMAT</action><action>enable:VBOX_CONVERT1</action></menuitem>
             <menuitem><label>wav</label><action>echo wav > '$WORKDIR'/export_copy_format</action><action>refresh:COPY_FORMAT</action><action>enable:VBOX_CONVERT1</action></menuitem>
             <label>..</label>
            </menu>
           </menubar>
          </hbox>
         </hbox>
         <text height-request="8"><label>""</label></text>
         <vbox>
          <hbox>
           <text height-request="18"><label>'$(gettext 'Bitrate (bits/sec)')'</label></text>
           <hbox width-request="80">
            <entry height-request="18"><variable>COPY_BITRATE</variable><input file>'$WORKDIR'/export_copy_bitrate</input></entry>
           </hbox>
           <text width-request="30"><label>""</label></text>
          </hbox>
          <hbox>
           <text height-request="18"><label>'$(gettext 'Samplerate (hz)')'</label></text>
           <hbox width-request="80">
            <entry height-request="18"><variable>COPY_SAMPLE</variable><input file>'$WORKDIR'/export_copy_sample</input></entry>
           </hbox>
           <text width-request="30"><label>""</label></text>
          </hbox>
          <hbox>
           <text height-request="18"><label>'$(gettext 'Channels')'</label></text>
           <hbox width-request="80">
            <entry height-request="18"><variable>COPY_CHANNEL</variable><input file>'$WORKDIR'/export_copy_channel</input></entry>
           </hbox>
           <text width-request="30"><label>""</label></text>
          </hbox>
          <variable>VBOX_CONVERT1</variable>
         </vbox>
        </vbox>
       </hbox>
      </vbox>
      <vbox margin="10" space-expand="true" space-fill="true">
       '$GUI_SOUND_INPUT'
       <variable>VBOX_SOUND_INPUT</variable>
       <sensitive>false</sensitive>
      </vbox>
     </notebook>
    </frame>
    <sensitive>'$VISIBLE_CONVERT'</sensitive>
    <variable>VBOX_CONVERT2</variable>
  </vbox>'
  [ "`grep -F 'cdda:' $WORKDIR/playlist`" ] && S=$S$GUI_CDAUDIO
  S=$S'<hbox space-expand="false" space-fill="false">
   <button cancel></button>
   <button can-default="true" has-default="true" use-stock="true">
    <input file stock="gtk-save-as"></input>
    <label>'$(gettext 'Export')'</label>
    <action type="exit">ok</action>
   </button>
  </hbox>
 </vbox>
</window>'

export pmusic_copy="$S"
[ $TOOLTIPS = false ] && pmusic_copy="`echo "$pmusic_copy" | sed 's%tooltip-text%tooltipXXXtext%g'`" #deactivate tooltips
echo "$pmusic_copy" | sed 's/##.*//' > $WORKDIR/xml_pmusic_export #I use double hash (##) for comments. --> as #FF0000 is valid xml-code
. $APPDIR/func -reset_gtk
I=$IFS; IFS=""
for STATEMENTS in $($GTKDIALOG -f $WORKDIR/xml_pmusic_export); do
	eval $STATEMENTS
done
IFS=$I

if [ $EXIT = "ok" ]; then
	#write rc
	echo "export COPYDIR=\"$COPYDIR\"
export SPLIT_RADIOSTREAM=$SPLIT_RADIOSTREAM
export ADD_NUMBERING=$ADD_NUMBERING
export FIRST_NR=$FIRST_NR

export CHK_OUTPUT_STREAM=$CHK_OUTPUT_STREAM
export COPY_BITRATE=$COPY_BITRATE
export COPY_FORMAT=$COPY_FORMAT
export COPY_SAMPLE=$COPY_SAMPLE
export COPY_CHANNEL=$COPY_CHANNEL

export CD_FILE_NAME=\"$CD_FILE_NAME\"
export ADD_META=$ADD_META" > $WORKDIR/rcexport

	#splash
	echo 0 > $WORKDIR/splash_copy
	export pmusic_copy_splash='
	<window title="pMusic" icon-name="pmusic" allow-grow="false">
	<vbox>
	 <frame>
	  <text><label>""</label></text>
	  '"<text use-markup=\"true\"><label>\"<b><span color='"'gray30'"' size='"'x-large'"'>pMusic  </span><span color='"'gray30'"' size='"'large'"'>$VERSION</span></b>\"</label></text>
	  <text><label>$(gettext 'Export')</label></text>
	 </frame>
	 <progressbar>
	  <input>"'while [ "$I" != "100" ]; do I=`cat '$WORKDIR'/splash_copy`; TXT=`cat '$WORKDIR'/splash_copy_text`; echo $I; echo "$TXT"; sleep 1; done</input>
	  <action type="exit">Ready</action>
	 </progressbar>
	 <hbox>
	  <button cancel>
	   <action>killall exec_copy</action>
	   <action>killall cdda2wav</action>
	   <action>killall ffmpeg</action>
	   <action>killall streamripper</action>
	   <action>eject</action>
	   <action>echo 100 > '$WORKDIR'/splash_copy</action>
	   <action>exit:EXIT</action>
	  </button> 
	 </hbox>
	</vbox></window>'
	$GTKDIALOG -p pmusic_copy_splash --center &


	cut -d '|' -f 4 $WORKDIR/playlist > $WORKDIR/tmp_export3
	TMP="`echo "$COPYDIR""/" | sed -e s%//%/%g`" #be sure to end with one /
	COPYDIR="$TMP"
	COUNT_TOTAL=$(wc -l $WORKDIR/playlist | awk '{print $1}') #for progress in splash-window
	COUNT=0
	echo -n $WORKDIR/exportlist

	#NORMALIZE
	#we calculate nomalizing values first. this because of issues with ffmpeg when ruuning it inside the big loop below
	echo '#!/bin/sh' > $WORKDIR/exec_copy
	echo -n > $WORKDIR/tmp_export_normalize
	echo "$(gettext 'Calculating normalizing values')..." > $WORKDIR/splash_copy_text #echo nothing will delete existing splash
	while read I; do
		#Check if sound filters is activated in sound processing filters or plugins
		if [ "$BYPASS_FILTERS" = "false" ]; then
			if [ $NORMALIZE = true ]; then
				echo -n "ffmpeg -i \"" >> $WORKDIR/exec_copy
				echo -n "$I" | sed -e 's/`/\\\`/g' -e 's/"/\\\"/g' >> $WORKDIR/exec_copy #allow ` and " in filename 
				echo '" -af volumedetect -f null - 2> '$WORKDIR'/ffmpeg_volumedetect_export' >> $WORKDIR/exec_copy #find possible gain increase

				echo 'TMP="`grep -F max_volume '$WORKDIR'/ffmpeg_volumedetect_export | cut -d: -f2 | cut -d" " -f2 | cut -d"-" -f2`"' >> $WORKDIR/exec_copy

				echo -n 'echo "' >> $WORKDIR/exec_copy
				echo -n "$I" | sed -e 's/`/\\\`/g' -e 's/"/\\\"/g' >> $WORKDIR/exec_copy #allow ` and " in filename 
				echo '|volume=${TMP}dB" >> '$WORKDIR'/tmp_export_normalize' >> $WORKDIR/exec_copy
			fi
		fi
	done < $WORKDIR/tmp_export3
	chmod 722 $WORKDIR/exec_copy
	$WORKDIR/exec_copy
	#---

	echo '#!/bin/sh' > $WORKDIR/exec_copy
	while read I; do
		#define numbering
		if [ $ADD_NUMBERING = true ]; then
			TMP="$(($COUNT+$FIRST_NR))"
			ADD_NR="`echo $(printf "%04d" $TMP)`"
		fi
		#filters
		if [ "$BYPASS_FILTERS" = "false" ]; then
			grep "$I" $WORKDIR/tmp_export_normalize | cut -d'|' -f2 > $WORKDIR/ffmpeg_filter_NORMALIZE #normalize values are calculated in loop above
			FILTER="`echo -af \\"$(<$WORKDIR/ffmpeg_filter_NORMALIZE)$(<$WORKDIR/ffmpeg_filter_EQ)$(<$WORKDIR/ffmpeg_filter_basic)$(<$WORKDIR/ffmpeg_filter)\\" | sed 's/,"$/"/'`" #_EQ and _basic is used by known plugins. The last is open for external input.
			[ "$FILTER" = '-af ""' ] && FILTER=""
		fi
		#---
		if [ "$CHK_OUTPUT_STREAM" = "true" ]; then
			[ "$COPY_FORMAT" ] && OUTPUT_STREAM="-f $COPY_FORMAT "
			[ "$COPY_CHANNEL" ] && OUTPUT_STREAM="$OUTPUT_STREAM -ac $COPY_CHANNEL"
			[ "$COPY_SAMPLE" ] && OUTPUT_STREAM="$OUTPUT_STREAM -ar $COPY_SAMPLE"
			[ "$COPY_BITRATE" ] && OUTPUT_STREAM="$OUTPUT_STREAM -ab $COPY_BITRATE"
		fi
		FILE="$I"
		#update progress in spash-window
		COUNT=$(($COUNT+1))
		echo "echo \"$(($COUNT*100/($COUNT_TOTAL+1)))\" > $WORKDIR/splash_copy" >> $WORKDIR/exec_copy
		echo "echo \"$(basename $FILE | sed -e 's/\.[^\.]*$//') ( $COUNT / $COUNT_TOTAL )\" > $WORKDIR/splash_copy_text" >> $WORKDIR/exec_copy

		#CD-audio
		if [ "`echo $I | grep -F cdda:`" ]; then
			pmusic -s stop
			#define tags which we stored when reading CD
			ID3_NR="`echo "$FILE" | cut -d: -f2`"
			ID3_ARTIST="`grep "|${ID3_NR}|" $WORKDIR/cddb | cut -d'|' -f2`"
			ID3_TITLE="`grep "|${ID3_NR}|" $WORKDIR/cddb | cut -d'|' -f3`"
			ID3_ALBUM="`grep "|${ID3_NR}|" $WORKDIR/cddb | cut -d'|' -f4`"
			ID3_YEAR="`grep "|${ID3_NR}|" $WORKDIR/cddb | cut -d'|' -f6`" #only avaliable if using cddb_query
			ID3_GENRE="`grep "|${ID3_NR}|" $WORKDIR/cddb | cut -d'|' -f7`" #only avaliable if using cddb_query
			[ ! "$ID3_TITLE" ] && ID3_TITLE="CD-track $ID3_NR" #not all CDs are available in the cddb
			#define name
			if [ $ADD_NUMBERING = true ]; then
				NAME="${ADD_NR}`echo "$CD_FILE_NAME" | sed -e "s/%a/$ID3_ARTIST/" -e "s/%t/$ID3_TITLE/" -e "s/%A/$ID3_ALBUM/" -e "s/%n//"`"
			else
				NAME="`echo "$CD_FILE_NAME" | sed -e "s/%a/$ID3_ARTIST/" -e "s/%t/$ID3_TITLE/" -e "s/%A/$ID3_ALBUM/" -e "s/%n/$ID3_NR/"`"
			fi
			[ ! "$NAME" ] && NAME="CD-track_`echo $I | cut -d ' ' -f 1 | sed -e 's/\.[^\.]*$//'`" #if no metatags exists
			#---
			[ "$CHK_OUTPUT_STREAM" = "false" ] && COPY_FORMAT=wav #no formating is chosen, but we need one for ffmpeg
			CDDA_TRACK="`echo $I | cut -d: -f 2`" 
			echo -n "cdda2wav dev=$CD_DEVICE -t $CDDA_TRACK -Owav - | " >> $WORKDIR/exec_copy
			echo -n "ffmpeg -i pipe:0 $FILTER $OUTPUT_STREAM " >> $WORKDIR/exec_copy
			#ffmpeg troubles with " in metainfo, so we better skip those
			TMP_ARTIST="`echo "$ID3_ARTIST" | tr -d '"'`"
			TMP_TITLE="`echo "$ID3_TITLE" | tr -d '"'`"
			TMP_ALBUM="`echo "$ID3_ALBUM" | tr -d '"'`"
			#---
			[ "$ADD_META" = "true" ] && echo -n "-id3v2_version 3 -metadata artist=\"$TMP_ARTIST\" -metadata title=\"$TMP_TITLE\" -metadata album=\"$TMP_ALBUM\" -metadata year=\"$ID3_YEAR\" -metadata track=\"$ID3_NR\" -metadata genre=\"$ID3_GENRE\"" >> $WORKDIR/exec_copy
			echo -n " -y \"" >> $WORKDIR/exec_copy
			echo -n "$COPYDIR$NAME.$COPY_FORMAT" | sed -e 's/`/\\\`/g' -e 's/"/\\\"/g' >> $WORKDIR/exec_copy #allow ` and " in filename 
			echo "\"" >> $WORKDIR/exec_copy
			continue
		fi
		#start streamripper if available
		if type streamripper > /dev/null 2>&1 && [ "`echo "$FILE" | grep -Fv 'youtube.com' | grep -F 'http:'`" ]; then
			#test if it is really a radio stream and not a song with a defined length.
			ffmpeg -i "$FILE" 2> $WORKDIR/tmp_export
			if [ "`grep 'Duration: N/A,' $WORKDIR/tmp_export`" ] || [ ! "`grep 'Duration:' $WORKDIR/tmp_export`" ] ; then
				if [ $SPLIT_RADIOSTREAM = true ]; then #split tracks 
					echo "streamripper '$FILE' -d '$COPYDIR' -s -o never -k 1 -u WinampMPEG/5.0" >> $WORKDIR/exec_copy
				else
					echo "streamripper '$FILE' -d '$COPYDIR' -A -a -u WinampMPEG/5.0" >> $WORKDIR/exec_copy
				fi
				continue
			fi
		fi

		#use ffmpeg
		TARGET="`basename "$FILE"`"
		[ $ADD_NUMBERING = true ] && TARGET="$ADD_NR"_"`basename "$FILE"`"
		#Convert output
		if [ "$CHK_OUTPUT_STREAM" = "true" ]; then
			NAME="`echo "$TARGET" | sed -e 's/\.[^\.]*$//'`" #remove extension
			#check if already exist
			if [ -f "$COPYDIR""$NAME.$COPY_FORMAT" ]; then
				IMG=dialog-warning; FRAME="$(gettext 'Warning')"; TXT1="<b>$COPYDIR$NAME.$COPY_FORMAT $(gettext 'already exists')</b>"; TXT2="$(gettext 'Do you want to overwrite existing file?')"
				sleep 1
				. $APPDIR/box_yesno
				if [ $EXIT != yes ]; then continue; fi
			fi
			#midi - to make it simple, we just make a temporary *.wav file that ffmpeg later will convert
			if [ "`echo "$FILE" | grep -iE "\.kar$|\.mid$"`" ]; then
				echo "timidity -OF -o $WORKDIR/tmp_midi_export.flac \"$FILE\"" >> $WORKDIR/exec_copy
				FILE=$WORKDIR/tmp_midi_export.flac
			fi
			#---
			echo -n "ffmpeg -i \"" >> $WORKDIR/exec_copy
			#if youtube url we need to find the exact URL. And if exist, use name from the webmusic_index.
			if [ "`echo "$FILE" | grep -F 'youtube.com'`" ]; then
				. $APPDIR/func -get_youtube_url "$FILE"
				FILE=$URL
				NAME="$ARTIST - $TITLE"
			fi
			#jamendo
			[ "`echo "$FILE" | grep -F 'jamendo.com'`" ] && NAME="`grep -F "$FILE" $WORKDIR/playlist | cut -d'|' -f3`"
			#---
			echo -n "$FILE" | sed -e 's/`/\\\`/g' -e 's/"/\\\"/g' >> $WORKDIR/exec_copy #allow ` and " in filename 
			echo -n "\" $FILTER $OUTPUT_STREAM -y \"" >> $WORKDIR/exec_copy
			echo -n "$COPYDIR$NAME.$COPY_FORMAT" | sed -e 's/`/\\\`/g' -e 's/"/\\\"/g' >> $WORKDIR/exec_copy #allow ` and " in filename 
			echo "\"" >> $WORKDIR/exec_copy
			#midi - remove temp file
			[ "`grep -F "$WORKDIR/tmp_midi_export.flac" $WORKDIR/exec_copy`" ] && echo "rm $WORKDIR/tmp_midi_export.flac" >> $WORKDIR/exec_copy
		else
			#check if already exist
			if [ -f "$COPYDIR""$TARGET" ]; then
				IMG=dialog-warning; FRAME="$(gettext 'Warning')"; TXT1="<b>$FILE $(gettext 'already exists')</b>"; TXT2="$(gettext 'Do you want to overwrite existing file?')"
				sleep 1
				. $APPDIR/box_yesno
				if [ $EXIT != yes ]; then continue; fi
			fi
			#---
			echo -n "cp -f \"" >> $WORKDIR/exec_copy
			echo -n "$FILE" | sed -e 's/`/\\\`/g' -e 's/"/\\\"/g' >> $WORKDIR/exec_copy #allow ` and " in filename 
			echo -n "\" \"" >> $WORKDIR/exec_copy 
			echo -n "$COPYDIR$TARGET" | sed -e 's/`/\\\`/g' -e 's/"/\\\"/g' >> $WORKDIR/exec_copy #allow ` and " in filename 
			echo "\"" >> $WORKDIR/exec_copy
		fi
	done < $WORKDIR/tmp_export3
	chmod 700 $WORKDIR/exec_copy
	$WORKDIR/exec_copy
	echo 100 > $WORKDIR/splash_copy #reset progress bar
fi
