#! /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=$"PCLinuxOS-Cutter&Shrink"
ICON="pclos_flv_main"
dirname="$(dirname "$(readlink -e "$0")")"
VERSION=" `cat "$dirname"/Version`"
ICONDIR="$dirname"/icons
LOGREAD="$dirname"/edit_dialog
CONVERTER=ffmpeg
#############################################################################
if [ $# -eq 0 ];
then
usage
fi
#
OPEN1="$1"
##############################################################################
OPEN=`cat $HOME/.mplayer/cut_file`
NEWNAME=`cat $HOME/.mplayer/cut_file | cut -d . -f 1`
EXT=`cat $HOME/.mplayer/cut_file | cut -d . -f 2`
NEWNAME1=`echo $OPEN1 | cut -d . -f 1`
EXT1=`echo $OPEN1 | cut -d . -f 2`
TMP_DIR=$HOME/.config/pclos-flv-player

# main window
MP4_CUTTER='<window window_position="1" title="'$TITLE$VERSION'"  icon-name="'$ICON'" space-fill="true">
<vbox space-fill="true">
	<frame '$"Load File"'>
		<hbox tooltip-text="'$"Load a file."'">
			<entry accept="filename">
				<default>"'$OPEN''$OPEN1'"</default>
				<variable>VID</variable>
			</entry>
		<button>
			<input file>'$ICONDIR'/open24.png</input>
			<variable>FILE_BROWSE_FILENAME</variable>
			<action type="fileselect">VID</action>
		</button>
		</hbox>
	</frame>
	<frame> 
		<vbox space-fill="true">
			<text use-markup="True">
			<label>"<b>'$"Set the codec to shrink a video in order to free up storage space."'</b>"</label>
			</text>
		</vbox>	
		<vbox tooltip-text="'$"Use Encoding options (default is Copy). Select your settings!"'">
			<tree rules-hint="true" selected-row="0" column-visible ="false|true">
				<variable>QUALITY</variable>
				<label>|Set Codec</label>
				<item>copy|Copy</item>
				<item>libx265|Libx265</item>
			</tree>
		</vbox>	
		<vbox space-fill="true">
			<text use-markup="True">
			<label>"<b>'$"Set start position and duration. The format is: hh:mm:ss[.xxx]. For example: 00:10:24."'</b>"</label>
			</text>
		</vbox>	
		<hbox>
			<checkbox>
				<label>'$"Select"'</label>
				<variable>SSSTART</variable>
				<action>if true enable:START</action>
				<action>if true echo -ss 00:00:00> '$HOME'/.mplayer/start_time</action>
				<action>if false disable:START</action>
				<action>if false echo > '$HOME'/.mplayer/start_time</action>
				<action type="refresh">START</action>
			</checkbox>
			<text><label>'$"Start position"':</label></text>
				<entry>
					<input>cat '$HOME'/.mplayer/start_time</input>
					<variable>START</variable>
					<visible>disabled</visible>
				</entry>
		</hbox>
		<hbox>
			<checkbox>
				<label>'$"Select"'</label>
				<variable>SSEND</variable>
				<action>if true enable:END</action>
				<action>if true echo -to 00:00:00> '$HOME'/.mplayer/end_time</action>
				<action>if false disable:END</action>
				<action>if false echo > '$HOME'/.mplayer/end_time</action>
				<action type="refresh">END</action>
			</checkbox>
			<text><label>'$"Duration"':</label></text>
				<entry>
					<input>cat '$HOME'/.mplayer/end_time</input>
					<variable>END</variable>
					<visible>disabled</visible>
				</entry>
		</hbox>
	</frame>
	<frame '$"Save Outputfile"'>
		<hbox>
			<entry accept="savefilename">
			<default>"'$NEWNAME''$NEWNAME1'(NEW).'$EXT''$EXT1'"</default>
			<variable>SAVE</variable>
			</entry>
		<button>
			<input file>'$ICONDIR'/open24.png</input>
			<variable>FILE_BROWSE_SAVEFILENAME</variable>
			<action type="fileselect">SAVE</action>
		</button>
		</hbox>
	</frame>
	     <hbox>
			<button>
				<input file>/usr/share/PCLinuxOS-FLV-Player/icons/play24.png</input>
				<label>'$"Play"'</label>
				<action>kill `cat '$TMP_DIR'/pid`</action>
				<action>mplayer -osd-fractions 1 -osdlevel 3 $START "$VID" &</action>
				<action>sleep 1 ; pidof mplayer > '$TMP_DIR'/pid</action>
			</button>
			<button>
				<input file>/usr/share/PCLinuxOS-FLV-Player/icons/stop24.png</input>
				<label>'$"Stop"'</label>
				<action>kill `cat '$TMP_DIR'/pid`</action>
			</button>
			<button>
				<input file>/usr/share/PCLinuxOS-FLV-Player/icons/convert24.png</input>
				<label>'$"Run"'</label>
				<action>(echo && '$CONVERTER' -y -i "$VID" -vcodec $QUALITY -acodec copy  $START $END "$SAVE") | yad --undecorated --center --progress --pulsate --auto-close --width=300 --title Cutter --window-icon=pclos_flv_main --text=$"Convert\n\n$SELECT \n\nto \n\n$SAVE \n\nplease wait..." ; sleep 1</action>
				<action>if [ -f "$SAVE" ]; then yad --title Cutter --window-icon=pclos_flv_main --button=gtk-ok --text=$"The `basename "$SAVE"` has been saved" ; else yad --title Cutter --window-icon=pclos_flv_main --button=gtk-ok --text=$"Something went wrong, check Your settings"; fi</action>
			</button>				
			<button>
				<input file>'$ICONDIR'/exit24.png</input>
				<label>'$"Exit"'</label>
				<action>kill `cat '$TMP_DIR'/pid`</action>				
				<action>rm -r '$HOME'/.mplayer/cut_file &</action>
				<action type="exit">MP4_CUTTER</action>
			</button>
		</hbox>
</vbox>
</window>
'
export GUI="`echo "$MP4_CUTTER" | sed -e 's/##.*//'`" 
gtkdialog -p GUI  > /dev/null 2>&1
rm -r $HOME/.mplayer/start_time	
rm -r $HOME/.mplayer/end_time	
