#! /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													#
#License GPL2																#	
#Copyright 2010-2020														#
#																			#
#############################################################################
				

# i18n - Internationalization - Internationalisierung
export TEXTDOMAIN=pclos-converter
export TEXTDOMAINDIR="/usr/share/locale"
#
#------------------- some variables ------------------------------------
TITLE=$"PCLinuxOS-Converter"
VERSION=" 2.5"
dirname="$(dirname "$(readlink -e "$0")")"
GUI="$dirname"/pclos-converter
ICON="$dirname"/icons/pclos-converter.png
ICONDIR="$dirname"/icons
LEGENDE="$dirname"/HISTORY
TMP_DIR=$HOME/.config/pclos-converter
TMP_DIR1=$HOME/tmp/batch_convert_audio
#--------------- create config directory -------------------------------
if [ ! -d $TMP_DIR ]; then
	mkdir $HOME/.config/pclos-converter
fi
if [ ! -d $TMP_DIR1 ]; then
	mkdir $HOME/tmp/batch_convert_audio
fi
if [ ! -f $TMP_DIR/directory ]; then
	D_STATUS="$HOME"
	else
	D_STATUS=`cat "$TMP_DIR"/directory`
fi
if [ -f /usr/bin/wavbreaker ]; then
	WAVBREAKER=true
	else
	WAVBREAKER=false
fi
if [ -f /usr/bin/avconv ]; then
	CONVERTER=avconv
	echo $"avconv is used"
	elif [ -f /usr/bin/ffmpeg ]; then
	CONVERTER=ffmpeg
	echo $"ffmpeg is used"
	else
	yad --text $"<b>Sorry, no converter is installed, please install ffmpeg or libav!</b>" \
		--title "$TITLE $VERSION" --width=350 \
		--window-icon=$ICON --image=$ICON0 --button="gtk-ok:0" 
	echo $"no converter found!"
fi
#---------------- open and load files ----------------------------------
	FIND=$(yad  --file-selection --directory \
			--title "$TITLE $VERSION" \
			--window-icon="$ICON" \
			--width=800 --height=600 \
			--text=$"\t<b>A simple converter for your stored audio files </b>\n\
	With the converter you can convert the audio \n\
	to mp3, ogg, aac, flac or wma. It is easy to use.\n\
	You need only to select a directory.\n\
	<b>_________________________________________________________________________</b>"\
			)
			if [ "$FIND" = "" ]; then								      # add entry
	yad --button="gtk-ok:0" --title "$TITLE$VERSION" \
				--window-icon="$ICON" --image="$ICON"\
				--width=300 \
				--text $"No directory selected, exit <b>$TITLE</b>!" && exit
			fi	
			
OPEN=$(find "$FIND" -name "*.wma" -o -name "*.wav" -o -name "*.flac" -o -name \
"*.aac" -o -name "*.mp3" -o -name "*.ogg"| sort -f | \
while read programm; do echo "<item>$programm</item>"; done)
#--------------------------- INFO DIALOG -------------------------------
export ABOUT_DIALOG='
<window window_position="1" title="'$TITLE$VERSION'" icon-name="pclos-convert">
	<vbox>
		<frame '$"About PCLinuxOS-Converter"'>
			<vbox>
				<text use-markup="True">
					<label>"<b>'$"A Simple Converter..."'</b>"</label>
				</text>
				<pixmap>
					<input file>/usr/share/PCLinuxOS-Converter/pcloslogo.png</input>
				</pixmap>
				<text use-markup="True" width-chars="30">
					<label>"'$"<b>Author:</b> D.M.-Wilhelm [leiche]"'"</label>
				</text> 
				<text use-markup="True" width-chars="30">
					<label>"'$"<b>Licence:</b> GPL"'"</label>
				</text> 
				<text use-markup="True" width-chars="30">
					<label>"'$"<b>Release:</b> 2011-2021"'"</label>
				</text> 
				<text use-markup="True" width-chars="30">
					<label>"'$"<b>Crow</b> Spanish translation"'"</label>
				</text>
				<text>
					<label>**************************************</label>
				</text> 
				<text>
					<input>'"ffmpeg -version | grep -m 1 version"'</input>
				</text> 
				<text>
					<input>'"normalize --version | grep -m 1  normalize"'</input>
				</text> 
			</vbox> 
		</frame>
		<hbox>
			<button>
				<input file>'$ICONDIR'/info24.png</input>
				<label>'$"History"'</label>
				<action type="launch">HISTORY</action>
			</button>
			
			<button>
				<label>'$"Exit"'</label>
				<input file>'$ICONDIR'/exit24.png</input>
				<variable>ABOUT_DIALOG</variable>
				<action type="closewindow">ABOUT_DIALOG</action>
			</button>
		</hbox>
	</vbox>
</window>
'
#------------------ History Dialog -------------------------------------
export HISTORY='
<window window_position="1" title="'$TITLE$VERSION'" icon-name="pclos-convert">
	<vbox>
		<frame '$"History"'>
			<vbox scrollable="true" width="600" height="300">
				<text><input file>'$LEGENDE'</input></text> 
			</vbox>
		</frame>
		<hbox>
			<button>
				<label>'$"Exit"'</label>
				<input file>'$ICONDIR'/exit24.png</input>
				<variable>HISTORY</variable>
				<action type="closewindow">HISTORY</action>
			</button>
		</hbox>
	</vbox>
</window>
'
#
#--------------------- Main Window -------------------------------------
export PCLOS_CONVERTER='<window window_position="1" title="'$TITLE$VERSION'"  icon-name="pclos-convert">
	<vbox>
		<frame> 
			<notebook labels="'$"Available|Batch list"'" space-expand="true" space-fill="true" allow-shrink="true">
				<vbox  tooltip-text="'$"If no files are displayed below the list, maybe the list is to long, or no files are available!"'">
					<text use-markup="True">
						<label>"<b>'$"Click on the file in the list below that you want to open."'</b>"</label>
					</text> 
					<tree rules_hint="true" exported_column="0">
						<width>500</width><height>220</height>
						<variable>SELECT</variable>
						<label>"'$"Files"'"</label>
						'$OPEN' 
					</tree>  
				</vbox> 
				<vbox  tooltip-text="'$"Here is listed your audio files in wav!"'" space-fill="true">
					<text use-markup="True">
						<label>"<b>'$"This files will all converted, by clicking on convert button..."'</b>"</label>
					</text>   
					<tree rules_hint="true" exported_column="0" selection-mode="3" space-fill="true">
						<width>500</width><height>220</height>
						<variable>FILES</variable>
						<label>"'$"Files from batch-list"'"</label>
						<input file>'$TMP_DIR'/batchfiles</input> 
					</tree>  
					<frame '$"Output"'>	
						<hbox tooltip-text="'$"Set a directory to save"'">
							<text><label>'$"Directory:"'</label></text>
								<entry accept="newdirectory">
									<default>'$D_STATUS'</default>
									<variable>DIRECTORY</variable>
								</entry>
								<button>
									<input file>'$ICONDIR'/save24.png</input>
									<label>'$"Save"'</label>
									<variable>FILE_BROWSE_FILENAME</variable>
									<action type="fileselect">DIRECTORY</action>
									<action>echo $DIRECTORY > '"$TMP_DIR"'/directory</action>
								</button>
						</hbox>
					</frame>
					
					<hbox>
						<hbox tooltip-text="'$"Remove the selected track from batch list"'">
							<button>
								<input file>'$ICONDIR'/remove24.png</input>
								<label>'$"Remove"'</label>
								<action>if [ "$FILES" ]; then grep -v "$FILES" '"$TMP_DIR"'/batchfiles > '"$TMP_DIR"'/batchfiles_tmp ; mv '"$TMP_DIR"'/batchfiles_tmp '"$TMP_DIR"'/batchfiles ; echo  "$FILES" | sed "s:\\\n: :g"| parallel --no-notice rm -rf '"{}"' ; fi</action>
								<action type="refresh">FILES</action>
							</button>
						</hbox>
						
						<hbox tooltip-text="'$"Normalize all audio tracks"'">
							<button>
								<input file>'$ICONDIR'/correction24.png</input>
								<label>'$"Normalize"'</label>
								<action>(echo && normalize -b '$TMP_DIR1/*') | yad --undecorated --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">FILES</action>
							</button>
						</hbox>  

						<hbox tooltip-text="'$"Convert listet, and selected file to mp3, wav, ogg, mpeg4 or wma. Be sure you have set a profile!"'">
							<button>
								<input file>'$ICONDIR'/convert24.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" --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 --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 cp '"{}"' "$DIRECTORY" && rm -rf "'$TMP_DIR1'"/* && echo done... > "'$TMP_DIR'"/batchfiles ; fi</action>
								<action type="refresh">FILES</action>
							</button>
						</hbox>
					</hbox>
				</vbox>
			</notebook>
			<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> 
			<hbox homogeneous="false">
				<vbox>
					<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>
				</vbox>
		
				<vbox>
					<text use-markup="True" width-chars="71">
						<label>"<b>'$"INFO"'</b>"</label>
					</text>	
					<hseparator></hseparator>
					<text use-markup="True" width-chars="71">
					<input file>'$TMP_DIR'/info</input>
					<variable>DISPLAY1</variable>
					</text>
				</vbox>
			</hbox>
		</frame>	
				
		<hbox homogeneous="true">
			<vbox tooltip-text="'$"What is $TITLE, Maintainer, and more..."'">
				<button>
					<input file>'$ICONDIR'/help24.png</input>
					<label>'$"About"'</label>
					<action type="launch">ABOUT_DIALOG</action>
				</button>
			</vbox>  	 	
  	
			<vbox tooltip-text="'$"Load a directory or file..."'"> 
				<button>
					<input file>'$ICONDIR'/open24.png</input>
					<label>'$"Open"'</label>
					<action>'$GUI' &</action>
					<action type="exit">re-open</action>
				</button>
			</vbox> 
  			
			<vbox tooltip-text="'$"Play your selected audio."'">
				<button>
					<input file>'$ICONDIR'/play24.png</input>
					<label>'$"Play"'</label>
					<action>ffmpeg -i "$SELECT" > '$TMP_DIR'/now_playing 2>&1 ; grep -i input -A11 '$TMP_DIR'/now_playing > '$TMP_DIR'/info</action>
					<action>killall ffplay ; ASK=`basename "$SELECT"` ; ffplay "$SELECT" -nodisp -window_title "$ASK"  &</action>
					<action type="refresh">DISPLAY1</action>
				</button>
			</vbox>
     
			<vbox tooltip-text="'$"Stop audio."'">
				<button>
					<input file>'$ICONDIR'/stop24.png</input>
					<label>'$"Stop"'</label>
					<action>echo '$"playback stopped"' > '$TMP_DIR'/info ; killall ffplay &</action>
					<action type="refresh">DISPLAY1</action>
				</button>
			</vbox>  
			
			<vbox tooltip-text="'$"Add current file to batch-list!"'">
				<button>
					<input file>'$ICONDIR'/add24.png</input>
					<label>'$"Add"'</label>
					<variable>BATCH_CONVERT</variable>
					<action>if [ "$SELECT" ]; then ASK=`basename "$SELECT"` && CUT=$(echo "$ASK" |  cut -d "." -f 1) && ffmpeg -y -i "$SELECT" -vn -ar 44100  "'$TMP_DIR1'/$CUT.wav" ; find '$TMP_DIR1' -type f  > '$TMP_DIR'/batchfiles ; fi </action>
					<action type="refresh">FILES</action>
				</button>
			</vbox> 
			
           				
			<vbox tooltip-text="'$"Exit $TITLE!"'">
				<button>
					<input file>'$ICONDIR'/exit24.png</input>
					<label>'$"Exit"'</label>
					<action>killall ffplay &</action>
					<action>rm -rf '$TMP_DIR'/audiofiles</action>
					<action>rm -rf '$TMP_DIR'/now_playing</action>
					<action>rm -rf '$TMP_DIR'/info</action>
					<action type="exit">PCLOS_Converter</action>
				</button>
			</vbox>	
		</hbox>
	</vbox>	
</window>
'

gtkdialog --program=PCLOS_CONVERTER #> /dev/null 2>&1
#--- we must delete files, when not exit by clicking the exit button ---
rm -rf $TMP_DIR/audiofiles
rm -rf $TMP_DIR/now_playing
rm -rf $TMP_DIR/info
