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

TITLE=$"FLV-Player and Converter"
ICON="pclos_flv_main"
dirname="$(dirname "$(readlink -e "$0")")"
#VERSION=" `cat "$dirname"/Version`"
ICONDIR="$dirname"/icons
HELP="$dirname"/help
SETTINGS="$dirname"/settings
TMP_DIR=$HOME/.config/pclos-flv-player
if [ -f $TMP_DIR/audio-quality ];then
	QLTY=`cat $TMP_DIR/audio-quality`
	else
	QLTY=5
	touch $TMP_DIR/audio-quality
	echo 5 > $TMP_DIR/audio-quality
fi	
if [ -f $TMP_DIR/codec ];then
	CDC=`cat $TMP_DIR/codec`
	else
	CDC=mp3
	touch $TMP_DIR/codec
	echo mp3 > $TMP_DIR/codec
fi
if [ -f $TMP_DIR/db-gain ];then
	DBG=`cat $TMP_DIR/db-gain`
	else
	DBG=4
	touch $TMP_DIR/db-gain
	echo 4 > $TMP_DIR/db-gain
fi	
if [ -f $TMP_DIR/cookies ];then
	CKS=`cat $TMP_DIR/cookies`
	else
	CKS=""
fi	
if [ -f $TMP_DIR/terminal ];then
	HOLD=`cat $TMP_DIR/terminal`
	else
	HOLD=""
fi

DIRECTORY=`cat $TMP_DIR/directory`	
if [ `cat $TMP_DIR/codec` = mp3 ];then
	MP3GN='<frame '$"MP3GAIN"'>
				<hbox tooltip-text="'$"MP3Gain analyzes and adjusts mp3 files so that they have the same volume in a folder. \
Note: Required mp3gain."'">
					<text><label>'$"Directory"'</label></text>
					<entry accept="directory">
						<default>"'$DIRECTORY'/"</default>
						<variable>DIRECTORY</variable>
					</entry>
					<button>
						<input file stock="gtk-open"></input>
						<variable>FILE_BROWSE_SAVEFILENAME</variable>
						<action type="fileselect">DIRECTORY</action>
					</button> 
					<text><label>'$"Gain"'</label></text>
						<comboboxentry width-request="60" case-sensitive="false" value-in-list="false">
							<default>'$DBG'</default>
							<variable>SET_D</variable>
							<item>1</item>
							<item>2</item>
							<item>3</item>
							<item>4</item>
							<item>5</item>
							<item>6</item>
							<item>7</item>
							<item>8</item>
							<item>9</item>
						</comboboxentry>
					<button>
						<label>'$"Okay"'</label>
						<action>xterm -geometry 90x5 -hold -e mp3gain -r -d $SET_D -p "$DIRECTORY"/*</action>
						<action>echo $SET_D > '$TMP_DIR'/db-gain</action>
					</button>
				</hbox>
			</frame>'
fi	

if  [ -f $TMP_DIR/youtube-dl ]; then
	YOUTUBE_DL_V=`$TMP_DIR/youtube-dl --version`
	YOUTUBE_DL="$TMP_DIR/youtube-dl"
	elif [ -f /usr/bin/yt-dlp ]; then
	YOUTUBE_DL_V=`yt-dlp --version`
	YOUTUBE_DL="yt-dlp"
	else
	YOUTUBE_DL_V=$"yt-dlp must be installed. \
Use synaptic to get yt-dlp, please."
fi
{
export INFO='
<window window_position="1" icon-name="'$ICON'" title="MovGrab">
	<vbox wrap-mode="1">
		<frame>
			<vbox scrollable="true">
				<text selectable="true">
					<input file>'$TMP_DIR'/fileout</input>
				</text>
			</vbox>
		</frame>	
		<vbox><hbox>  
			<button>
				<label>OK</label>
				<variable>INFO</variable>
				<action type="closewindow">INFO</action>
			</button>
		</hbox></vbox>
	</vbox>		
</window>
'
}

MOVGRAB='
<window window_position="1" title="MovGrab'$VERSION'"  icon-name="'$ICON'" space-fill="true">
	<<vbox space-fill="true">
			<menubar>
			<menu>
				<menuitem stock="gtk-quit">
					<action type="exit">MOVGRAB</action>
				</menuitem>
				<label>'$"File"'</label>
			</menu>
			
			<menu>
				<menuitem icon-name="gtk-preferences"> 
					<label>'$"Settings"'</label>
					<action>'$SETTINGS' &</action>
				</menuitem> 
				<label>'$"Edit"'</label>
			</menu>

			<menu>
				<menuitem stock="gtk-help">
					<label>'$"About"'</label>
					<action>'$HELP' &</action>
				</menuitem>
					<label>'$"Help"'</label>
			</menu>
		</menubar>
		
		<frame Movgrab>
			<hbox tooltip-text="'$"Select a directory to store a grab movie..."'">
				<text><label>'$"Save File"'</label></text>
					<entry accept="savefilename">
						<default>"'$DIRECTORY'/"</default>
						<variable>SAVEGRAB</variable>
						<action>echo $SAVEGRAB > '$TMP_DIR'/fileout</action>
					</entry>
					<button>
						<input file stock="gtk-open"></input>
						<variable>FILE_BROWSE_SAVEFILENAME</variable>
						<action type="fileselect">SAVEGRAB</action>
						<action>echo $SAVEGRAB > '$TMP_DIR'/fileout</action>
						<action>echo `dirname $SAVEGRAB` > '$TMP_DIR'/directory</action>
					</button> 
			</hbox>
			
			<hbox tooltip-text="'$"Simple movgrab based on youtube-dl. Paste your link here from a movie like from web site as youtube. Click on Grab to get it."'">
				<text><label>Link:</label></text>
					<entry>
						<variable>GRAB</variable>
					</entry>
					<hbox tooltip-text="'$"Download the best format, that is available."'">
						<button>
							<label>'$"Grab"'</label>
							<action>xterm -geometry 90x5 '$HOLD' -e '$YOUTUBE_DL' "$GRAB" '$CKS' -o "$SAVEGRAB"</action>
							<action>if [ -f "$SAVEGRAB".mp4 ]; then echo "$SAVEGRAB".mp4 completed > '$TMP_DIR'/fileout ; elif [ -f "$SAVEGRAB".mkv ]; then echo "$SAVEGRAB".mkv completed > '$TMP_DIR'/fileout ; elif [ -f "$SAVEGRAB".webm ]; then echo "$SAVEGRAB".webm completed > '$TMP_DIR'/fileout ; else echo "'$"No files have been downloaded"'" > '$TMP_DIR'/fileout ; fi</action>
							<action type="launch" >INFO</action>
						</button>
					</hbox>							
			</hbox>
			
			<frame Custom>

			<frame '$"Only Audio"'>
				<hbox>
					<hbox tooltip-text="'$" Specify audio format: aac, flac, mp3, m4a, ogg, or wav"'">
						<text><label>'$"Audio format"'</label></text>
							<comboboxentry width-request="60" case-sensitive="false" value-in-list="false">
								<default>'$CDC'</default>
								<variable>FORMAT</variable>
								<item>mp3</item>
								<item>flac</item>
								<item>aac</item>
								<item>wav</item>
								<item>m4a</item>
								<item>vorbis</item>
							</comboboxentry>
					</hbox>
					<hbox tooltip-text="'$"Specify audio quality, insert a value between 0 (better) and 9 (worse) for VBR or a specific bitrate like 128K (default 5)"'">
						<text><label>'$"Audio quality"'</label></text>
							<comboboxentry width-request="60" case-sensitive="false" value-in-list="false">
								<default>'$QLTY'</default>
								<variable>QUALITY</variable>
								<item>0</item>
								<item>1</item>
								<item>2</item>
								<item>3</item>
								<item>4</item>
								<item>5</item>
								<item>6</item>
								<item>7</item>
								<item>8</item>
								<item>9</item>
							</comboboxentry>
					</hbox>
			<vbox tooltip-text="'$"Download only audio."'">
				<button>
					<label>'$"Grab Audio"'</label>
					<action>xterm -geometry 90x5 '$HOLD' -e '$YOUTUBE_DL' -x --audio-format $FORMAT --audio-quality $QUALITY "$GRAB" '$CKS' -o "$SAVEGRAB.%(ext)s"</action>
					<action>if [ -f "$SAVEGRAB".mp3 ]; then echo "$SAVEGRAB".mp3 completed > '$TMP_DIR'/fileout ; elif [ -f "$SAVEGRAB".flac ]; then echo "$SAVEGRAB".flac completed > '$TMP_DIR'/fileout ; elif [ -f "$SAVEGRAB".aac ]; then echo "$SAVEGRAB".aac completed > '$TMP_DIR'/fileout ; elif [ -f "$SAVEGRAB".wac ]; then echo "$SAVEGRAB".wac completed > '$TMP_DIR'/fileout ; elif [ -f "$SAVEGRAB".m4a ]; then echo "$SAVEGRAB".m4a completed > '$TMP_DIR'/fileout ; elif [ -f "$SAVEGRAB".ogg ]; then echo "$SAVEGRAB".ogg completed > '$TMP_DIR'/fileout ; else echo "'$"No files have been downloaded"'" > '$TMP_DIR'/fileout ; fi</action>
					<action>echo $QUALITY > '$TMP_DIR'/audio-quality</action>
					<action>echo $FORMAT > '$TMP_DIR'/codec</action>
					<action type="launch">INFO</action>
				</button>
			</vbox>
			<vbox tooltip-text="'$"Download a playlist only as audio files."'">	
				<button>
					<label>'$"Grab Playlist"'</label>
					<action>xterm -geometry 90x5 '$HOLD' -e '$YOUTUBE_DL' -x --audio-format $FORMAT --audio-quality $QUALITY '$CKS' "$GRAB"</action>
					<action>echo "'$"Files have been downloaded in your home folder"'" > '$TMP_DIR'/fileout</action>
					<action type="launch">INFO</action>
				</button>
			</vbox>	
			</hbox>
			</frame>
			'$MP3GN'
			<frame yt-dlp Version>
			<text><label>'$YOUTUBE_DL_V'</label></text>
				<hbox tooltip-text="'$"Update your Youtube-dl"'">
					<button>
						<label>'$"Update"'</label>
						<action>curl -L https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp -o '$TMP_DIR'/youtube-dl</action>
						<action>chmod a+rx '$TMP_DIR'/youtube-dl</action>
						<action>'$dirname'/movgrabgui &</action>
						<action type="exit">MOVGRAB</action>
					</button>
				</hbox>
			</frame>
			</frame>	
		</frame>	
	</vbox>	
</window> 
'	

export GUI="`echo "$MOVGRAB" | sed -e 's/##.*//'`" 
gtkdialog -p GUI  #> /dev/null 2>&1
