#!/bin/bash
#
# Copyright 2010 
# for PCLinuxOS by Leiche and Neal Brooks
# License - GPLv2
#
# i18n - Internationalization - Internationalisierung
export TEXTDOMAIN=screenie
export TEXTDOMAINDIR="/usr/share/locale"
#
# create conf file - Konfigdatei erstellen
if [ -d $HOME/.config/screenie ]; then
	echo configfile exists
	else
	mkdir $HOME/.config/screenie
	echo "#Configuration file for screenie" > $HOME/.config/screenie/screenie.cfg
fi
CONF_DIR=$HOME/.config/screenie
if [ $(grep -c save_jpg=true $CONF_DIR/screenie.cfg) -gt 0 ];then
	EXT=jpg
	else
	EXT=png
fi
#
# Some variable - Einige Variable
VERSION=1.1.0
TITLE=Screenie
ICON=/usr/share/icons/screenie.png
TMP_DIR=$HOME/.cache
sleep 0.5
scrot -t 30 /$TMP_DIR/screen.$EXT
IMG=$TMP_DIR/screen-thumb.$EXT

#
# check if scrot available - prüfen ob scrot vorhanden ist
if [ ! -f /usr/bin/scrot ]; then
	yad --title "$TITLE"--"$VERSION" --window-icon=$ICON \
		--text $"Screenie requires scrot. Please install it using your package manager!" \
		--button="gtk-ok:0" --image=$ICON
	exit 1
fi	
if [ ! -f /usr/bin/ffmpeg ]; then
	yad --title "$TITLE"--"$VERSION" --window-icon=$ICON \
		--text $"Screenie requires ffmpeg. Please install it using your package manager! \n
The screen recording function is not working." \
		--button="gtk-ok:0" --image=$ICON
fi
if [ ! -f /usr/bin/xrandr ]; then
	yad --title "$TITLE"--"$VERSION" --window-icon=$ICON \
		--text $"Screenie requires xrandr. Please install it using your package manager! \n
The screen recording function is not working." \
		--button="gtk-ok:0" --image=$ICON
fi

function configure_settings() 
{
# read current settings 
if [ $(grep -c frame=true $CONF_DIR/screenie.cfg) -gt 0 ]; then
	FRAME_S=true
	else
	FRAME_S=false
fi
if [ $(grep -c save_jpg=true $CONF_DIR/screenie.cfg) -gt 0 ]; then
	JPG_S=true
	else
	JPG_S=false
fi
if [ $(grep -c /home $CONF_DIR/screenie.cfg) -gt 0 ]; then
	DIRECTORY_S=`grep /home $CONF_DIR/screenie.cfg`
	else
	DIRECTORY_S=$HOME
fi	
#
export SETTINGS_DIALOG='
<window title="'$TITLE' '$VERSION'" resizable="true" icon-name="screenie">
<vbox>
	<frame>
		<vbox>
			<text xalign="0" selectable="false" use-markup="True">
				<label>"<b>'$"Window shot with frame"'</b>"</label>
			</text>
			<checkbox  active="'$FRAME_S'">
				<label>'$"Enable"'</label>
				<variable>FRAME</variable>
			</checkbox>
		</vbox>	
		<hseparator></hseparator>
		<vbox>
			<text xalign="0" selectable="false" use-markup="True">
				<label>"<b>'$"Save as jpg"'</b>"</label>
			</text>
			<checkbox  active="'$JPG_S'">
				<label>'$"Enable"'</label>
				<variable>JPG</variable>
			</checkbox>
			<text xalign="0" selectable="false" use-markup="True">
				<label>"'$"Save as jpg will appear after restart screenie."'"</label>
			</text>
		</vbox>
		<hseparator></hseparator>
		<vbox>
			<text xalign="0" selectable="false" use-markup="True">
				<label>"<b>'$"Choose save directory"'</b>"</label>
			</text>
			<entry accept="newdirectory">
				<default>'$DIRECTORY_S'</default>
				<variable>DIRECTORY</variable>
			</entry>
		</vbox>	
	</frame>	
	<hbox start="true">
		<hbox start="true">
			<button>
				<label>"'$" Save "'"</label>
				<input file icon="document-save"></input>
				<action type="exit">save</action>
			</button>	
			<button>
				<label>"'$"Close"'"</label>
				<input file icon="application-exit"></input>
				<action type="exit">close</action>
			</button>
		</hbox>
	</hbox>
</vbox>
</window>
'

I=$IFS; IFS=""
for STATEMENT in  $(gtkdialog --center --program=SETTINGS_DIALOG); do
	eval $STATEMENT
done
IFS=$I

if [ "$EXIT" == "save" ];then
echo -e "#Configuration file for screenie.\nframe=$FRAME\nsave_jpg=$JPG\n$DIRECTORY"| \
sed -e '/^\s*$/d' > $CONF_DIR/screenie.cfg
fi

}

#
# Menu - Menü
function menu {
RET=$(yad --title "$TITLE $VERSION" --image=$IMG --window-icon=$ICON --list \
		  --button="gtk-save:4" \
		  --button="gtk-about:2" \
		  --button="gtk-preferences:3" \
		  --button="gtk-close:1" \
		  --column $"Select an option" \
			$"Delayed Screenshot " \
			$"Window Shot " \
			$"Record Session " \
		  --width=800 --height=400 \	
)

ret=$?

[[ $ret -eq 1 ]] && rm -r $TMP_DIR/screen*.$EXT && exit 0

if [[ $ret -eq 2 ]]; then
	yad --image=$ICON \
	--title=$"About $TITLE $VERSION" --timeout 30 --window-icon=$ICON \
	--text=$"Screenie is a simple way to take a screenshot!
Designed for PCLinuxOS-LXDE 2010.
Based on scrot, with a simple GUI written in YAD.

<b>Authors:</b> Neal Brooks, and D.M-Wilhelm (Leiche)


<b>License:</b> GPLv2
<b>Copyright:</b> 2010-2025"
  menu
fi
#
if [[ $ret -eq 3 ]]; then
  configure_settings	
  menu	
fi
#
if [[ $ret -eq 4 ]]; then
	if [ $(grep -c /home $CONF_DIR/screenie.cfg) -gt 0 ]; then
		DIRECTORY_S=$"`grep /home $CONF_DIR/screenie.cfg`"
		else
		DIRECTORY_S=$HOME
	fi
	SHOT=$(yad --file-selection --save --title=Screenie \
	--file-filter="*.png *.jpg" \
	--confirm-overwrite=$" file exist, you want to overwrite?" \
	--filename="$DIRECTORY_S"/shot.$EXT --window-icon=/usr/share/icons/screenie.png \
	--width=800 --height=600)
	if [ -z "$SHOT" ];then
		menu
		else
		cp $TMP_DIR/screen.$EXT $SHOT
	fi
	menu
fi

if echo "$RET" | grep $"Delayed"; then
		TIME=$(yad --entry --title=Screenie --window-icon=/usr/share/icons/screenie.png \
		--text $"Enter the seconds to wait to take the screenshot!")
	if [ "$TIME" = "" ];then
		menu
		else
		sleep $TIME
	fi
	if [ $(grep -c save_jpg=true $CONF_DIR/screenie.cfg) -gt 0 ];then
		EXT=jpg
		else
		EXT=png
	fi	
	scrot -t 30 -o /$TMP_DIR/screen.$EXT
	menu
fi

if echo "$RET" | grep $"Window"; then
		if [ $(grep -c frame=true $CONF_DIR/screenie.cfg) -gt 0 ];then
			scrot -s -b -t 30 -o /$TMP_DIR/screen.$EXT
			else
			scrot -s -t 30 -o /$TMP_DIR/screen.$EXT
		fi
	menu	
fi
if echo "$RET" | grep $"Record"; then
			ffmpeg -f x11grab -r 25 -s `xrandr | grep '*' | sed -e 's/*+// ' | cut -d " " -f 4` \
			-i :0.0 $HOME/`date +%Y.%m.%d.%H.%M`.mp4 &
			yad --text $"Record stop" --geometry 1x1+600+1 --undecorated --button="gtk-stop:0" \
			--title screenie-capture --window-icon=/usr/share/icons/screenie.png
			if [ $? = "0" ]; then
			kill `pgrep ffmpeg`
			fi 
			yad --text=$"Your recording will be saved to $HOME." --button="gtk-ok:0" \
			--title screenie-capture --window-icon=$ICON
	menu	
fi
}
menu
exit 0
