#! /bin/bash
#
Encoding=UTF-8
#############################################################################
#																			#
# Author D.M-Wilhelm aka Leiche <kellerleicheorig@aol.com> at Nov 05 2010	#
# Website <kellerleiche.bplaced.net> 										#
# Designed for PCLinuxOS'10													#
# Release 2010-2018 														#
# License GPL2																#
#																			#
#############################################################################
# i18n - Internationalization - Internationalisierung
export TEXTDOMAIN=pclos-flv-main
export TEXTDOMAINDIR="/usr/share/locale"

#
# main variables
TITLE=$"FLV-Player and Converter"
ICON="pclos_flv_main"
dirname="$(dirname "$(readlink -e "$0")")"
VERSION=" `cat "$dirname"/Version`"
ICONDIR="$dirname"/icons
LOGREAD="$dirname"/edit_dialog
CONVERTER=ffmpeg
TMP_DIR=$HOME/.config/pclos-flv-player
TMP_DIR1=$HOME/tmp/batch_convert
ECHO=echo #"`cat $TMP_DIR/log | sed -n 7p`"
if [ -f $TMP_DIR/save_directory ]; then
	SAVE_DIRECTORY=`cat $TMP_DIR/save_directory`
	else
	SAVE_DIRECTORY=$HOME
fi	
# create tmp directory
if [ ! -d $TMP_DIR1 ]; then
	mkdir $HOME/tmp/batch_convert
fi
if [ -f /usr/bin/wavbreaker ]; then
	WAVBREAKER=true
	else
	WAVBREAKER=false
fi
		
find $TMP_DIR1 -type f  | \
sort -f | while read programm; do echo "$programm" >> $TMP_DIR/batchfiles ; done

#
# main window
BATCH_CONVERTER='<window window_position="1" title="'$TITLE$VERSION'"  icon-name="'$ICON'" space-fill="true">
<vbox space-fill="true">
	<frame> 
		<vbox space-fill="true">
			<text use-markup="True">
			<label>"<b>'$"This files will all converted to audio, by clicking on convert button..."'</b>"</label>
			</text>
		</vbox>
		
		<vbox space-fill="true">   
			<tree rules_hint="true" exported_column="0" selection-mode="3" space-fill="true">
				<width>500</width><height>220</height>
				<variable>SELECT</variable>
				<label>"'$"Files from batch-list"'"</label>
				<input file>'$TMP_DIR'/batchfiles</input> 
			</tree>  
		</vbox> 
      
		<vbox  tooltip-text="'$"Choose your settings to convert your media files!"'">
			<text use-markup="True">
			<label>"<b>'$"Select a profile, and click on Convert!"'</b>"</label>
			</text>
		</vbox> 
		
		<vbox>
			<hbox>
				<tree selected-row="1" column-visible ="false|true|true">
					<variable>CONVERT</variable>
					<width>500</width><height>220</height>
					<label>|'$"Select a Profile				                         "'|'$"Format"'</label>
					<item>-acodec libmp3lame -ab 96k -ac 2 -ar 44100|Lame 96kb|MP3</item>
					<item>-acodec libmp3lame -ab 128k -ac 2 -ar 44100|Lame 128kb|MP3|'$"Default settings"'</item>
					<item>-acodec libmp3lame -ab 160k -ac 2 -ar 44100|Lame 160kb|MP3</item>
					<item>-acodec libmp3lame -ab 192k -ac 2 -ar 44100|Lame 192kb|MP3</item>
					<item>-acodec libmp3lame -ab 224k -ac 2 -ar 44100|Lame 224kb|MP3</item>
					<item>-acodec libmp3lame -ab 256k -ac 2 -ar 44100|Lame 256kb|MP3</item>
					<item>-acodec libmp3lame -ab 320k -ac 2 -ar 44100|Lame 320kb|MP3</item>
					<item>-acodec libvorbis -aq 0 -vn -ac 2|Vorbis 50-70kb|OGG</item>
					<item>-acodec libvorbis -aq 1 -vn -ac 2|Vorbis 65-85kb|OGG</item>
					<item>-acodec libvorbis -aq 2 -vn -ac 2|Vorbis 80-100kb|OGG</item>
					<item>-acodec libvorbis -aq 3 -vn -ac 2|Vorbis 90-130kb|OGG</item>
					<item>-acodec libvorbis -aq 4 -vn -ac 2|Vorbis 110-150kb|OGG</item>
					<item>-acodec libvorbis -aq 5 -vn -ac 2|Vorbis 145-185kb|OGG</item>
					<item>-acodec libvorbis -aq 6 -vn -ac 2|Vorbis 170-210kb|OGG</item>
					<item>-vn  -acodec wmav2 -ab 128k|Windows Media Audio 128kb|WMA</item>
					<item>-vn  -acodec wmav2 -ab 160k|Windows Media Audio 160kb|WMA</item>
					<item>-vn  -acodec wmav2 -ab 192k|Windows Media Audio 192kb|WMA</item>
					<item>-vn -acodec libfaac -ab 112k -ac 2|Advanced Audio Codec 112kb|AAC</item>
					<item>-vn -acodec libfaac -ab 128k -ac 2|Advanced Audio Codec 128kb|AAC</item>
					<item>-vn -acodec libfaac -ab 152k -ac 2|Advanced Audio Codec 152kb|AAC</item>
					<item>-acodec flac -ar 44100 -ac 2|RAW Audio 44,1 Hz|FLAC</item>
					<item>-acodec flac -ar 48000 -ac 2|RAW Audio 48 Hz|FLAC</item>
				</tree>
			</hbox>
		</vbox>
	</frame>
	
	<frame '$"Output"'>	
		<hbox tooltip-text="'$"Set a directory to save. Notice: No spaces in the name or path supported."'">
			<text><label>'$"Directory:"'</label></text>
				<entry accept="directory">
				<default>"'$SAVE_DIRECTORY'"</default>
				<variable>DIRECTORY</variable>
				</entry>
					<button>
						<input file>'$ICONDIR'/open24.png</input>
						<variable>FILE_BROWSE_FILENAME</variable>
						<action type="fileselect">DIRECTORY</action>
						<action>echo $DIRECTORY > "'$TMP_DIR'"/save_directory</action>
					</button>
		</hbox>		
	</frame>
   			
	<hbox>		
		<vbox tooltip-text="'$"Remove the selected track!"'">
			<button>
				<label>'$"Delete"'</label>
				<input file>'$ICONDIR'/delete24.png</input>
				<action>yad --text=$"Are you sure you want to delete $SELECT from your batch list?" --window-icon='$ICON' --width 600 --title=$"Remove..." ; if [ $? = 0 ]; then grep -v "$SELECT" '"$TMP_DIR"'/batchfiles > '"$TMP_DIR"'/batchfiles_tmp ; mv '"$TMP_DIR"'/batchfiles_tmp '"$TMP_DIR"'/batchfiles ; echo  "$SELECT" | sed "s:\\\n: :g"| parallel rm -rf '"{}"' ; fi</action>
				<action type="refresh">SELECT</action>
			</button>
		</vbox>
		
		<vbox tooltip-text="'$"Split a selected wave file! If you start wavbreaker first time, so set your output files to '$TMP_DIR1'"'">
			<button>
				<label>'$"Wavbreaker"'</label>
				<input file>/usr/share/icons/hicolor/24x24/apps/wavbreaker.png</input>
				<action>wavbreaker "$SELECT"</action>
				<action>rm -rf '$TMP_DIR'/batchfiles && find '$TMP_DIR1' -type f  | sort -f | while read programm; do echo "$programm" >> '$TMP_DIR'/batchfiles ; done</action>
				<action type="refresh">SELECT</action>
				<visible>'$WAVBREAKER'</visible>
			</button>
		</vbox>	

		<hbox tooltip-text="'$"Normalize all audio tracks"'">
			<button>
				<input file>'$ICONDIR'/correction24.png</input>
				<label>'$"Normalize"'</label>
				<action>(echo && normalize -l=0dBFS '$TMP_DIR1/*') | yad --undecorated --center --no-buttons --progress --pulsate --auto-close --width=500 --title '$TITLE' --image='$ICON' --window-icon='$ICON' --text=$"Normalize audio, this will take some minutes, please wait..." </action>
				<action type="refresh">SELECT</action>
			</button>
		</hbox>	
			
		<hbox tooltip-text="'$"Convert your files to mp3, ogg, mpeg4 or wma. Be sure you have set a profile!
Notice: No spaces in the folder name or path supported."'">
			<button>
				<input file>'$ICONDIR'/batch24.png</input>
				<label>'$"Convert"'</label>
				<action>echo $CONVERT > '$TMP_DIR'/converter_settings</action>
				<action>if [ $(grep -c libmp3lame "'$TMP_DIR'"/converter_settings) -gt 0 ]; then EXT=mp3; elif [ $(grep -c libvorbis "'$TMP_DIR'"/converter_settings) -gt 0 ]; then EXT=ogg ; elif [ $(grep -c wmav2 "'$TMP_DIR'"/converter_settings) -gt 0 ]; then EXT=wma ; elif [ $(grep -c libfaac "'$TMP_DIR'"/converter_settings) -gt 0 ]; then EXT=aac ; elif [ $(grep -c flac "'$TMP_DIR'"/converter_settings) -gt 0 ]; then EXT=flac ; elif [ $(grep -c ar "'$TMP_DIR'"/converter_settings) -gt 0 ]; then  EXT=wav ; else  echo copy audio  ; fi ; echo $EXT > "'$TMP_DIR'"/extension ; yad --text=$"Do you use a directory, and set a profile?\n\nProfil is: $CONVERT\n\nIf not use a directory, all files will store under home directory! \n\nDirectory is: $DIRECTORY \n\nNotice: No spaces in the folder name or path supported." --window-icon='$ICON' --title="Convert all..." ; if [ $? = 0 ]; then  (echo && find "'$TMP_DIR1'" -not -name "*.$EXT" |  parallel --no-notice '$CONVERTER' -y -i '{}' $CONVERT "{.}"."$EXT") | yad --undecorated --center --no-buttons --progress --pulsate --auto-close --width=500 --title '$TITLE' --image='$ICON' --window-icon='$ICON' --text=$"Convert audio, this will take some minutes, please wait..." ; find "'$TMP_DIR1'" -name "*.$EXT" | parallel --no-notice mv '"{}"' "$DIRECTORY" ; rm -rf "'$TMP_DIR1'"/* && echo done :-\) > "'$TMP_DIR'"/batchfiles ; fi</action>
				<action type="refresh">SELECT</action>
			</button>
		</hbox>
		
		<hbox tooltip-text="'$"Read logfile"'">
			<button>
				##<input file>'$ICONDIR'/find24.png</input>
				<label>'$"Log"'</label>
				<action>'$LOGREAD' &</action>
			</button>
		</hbox>
           				
		<vbox tooltip-text="'$"Exit $TITLE!"'">
			<button>
				<input file>'$ICONDIR'/exit24.png</input>
				<label>'$"Exit"'</label>
				<action>rm -r '$TMP_DIR'/batchfiles &</action>
				<action type="exit">BATCH_CONVERTER</action>
			</button>
		</vbox>	
	</hbox>
</vbox>
</window>
'
export GUI="`echo "$BATCH_CONVERTER" | sed -e 's/##.*//'`" 
gtkdialog -p GUI > $TMP_DIR/log 2>&1
rm -r $TMP_DIR/batchfiles
