#!/usr/bin/bash

# AUTHOR:   (c) karlo@latnet.lv 2024.07.24.
# NAME:     byzanz window recorder 0.2.0
# DESCRIPTION:  A script to record GIF, OGV, FLV and WebM screencasts.
# LICENSE:  GNU GPL v3 (http://www.gnu.org/licenses/gpl.html)
# DEPENDENCIES:   byzanz, zenity, gettext, xwininfo
# Based on http://askubuntu.com/questions/107726/how-to-create-animated-gif-images-of-a-screencast
# To get strings for translation, use
#     xgettext -L Shell --from-code UTF-8 byzanz-record-window  -o byzanz-record-window.pot

# Delay before starting
DELAY=6

# Recording duration
DURATION=600

# Icon of message windows
# ICON=/usr/share/icons/gnome/16x16/actions/media-record.png
ICON=media-record

#Internacionaliztion
export TEXTDOMAIN="byzanz-record-window"
. gettext.sh

# Sound notification to let one know when recording is about to start and ends
beep() {
    paplay /usr/share/sounds/freedesktop/stereo/complete.oga
}

# Encapsulated byzanz-record command (with sound signals and delay)
komanda (){
	sleep $DELAY
	beep
	byzanz-record --verbose --cursor --delay=0 --duration=$DURATION --x=$X --y=$Y --width=$W --height=$H "$FAILS"
	beep
	exit 0
}

if [ $(command -v byzanz-record) ]; then
	echo "„byzanz-record”" `eval_gettext 'exist'`"."  
else
	echo `eval_gettext 'Error:'` "„byzanz-record”" `eval_gettext 'not found'`"." 
	exit 1
fi
if [ $(command -v zenity) ]; then
	echo "„zenity”" `eval_gettext 'exist'`"." 
else
	echo `eval_gettext 'Error:'` "„zenity”" `eval_gettext 'not found'`"."  
	exit 1
fi
if [ $(command -v zenity) ]; then
	echo "„xwininfo”" `eval_gettext 'exist'`"." 
else
	echo `eval_gettext 'Error:'` "„xwininfo”" `eval_gettext 'not found'`"."  
	exit 1
fi


FAILS=$(zenity \
	--title="`eval_gettext 'Choose the file and directory for output'` (gif/flv/byzanz/ogg/ogv/webm)" \
	--icon=$ICON \
	--file-selection \
	--save)

if [ "$FAILS" ]; then
	echo `eval_gettext 'The output will be written to'`": $FAILS"
	rm "$FAILS"
else
	echo `eval_gettext 'Error:'` `eval_gettext 'No file selected for output'`"."
	exit 1
fi

DURATION=$(zenity \
	--title="`eval_gettext 'Chose the window to be recorded'`" \
	--icon=$ICON \
	--entry \
	--text="`eval_gettext 'The window will be recorded with'` „byzanz”.\n`eval_gettext 'After closing this window, You have to click on a window to be recorded'`.\n`eval_gettext 'Afterwards the record will start in'` $DELAY `eval_gettext 'seconds'`\n(`eval_gettext 'a sound notification will let you know when recording is about to start'`).\n\n`eval_gettext 'The record’s duration in seconds'`:" \
	--entry-text=$DURATION \
	--ok-label="`eval_gettext 'Record a window'`")
if [ $DURATION ]; then
	if [ $DURATION  -le "3" ]; then
		echo `eval_gettext 'Error:'` `eval_gettext 'Record duration  is set too short'`"."
		exit 1
	else
		TIMEOUT=$(($DELAY + $DURATION + $DURATION * 4))
		LOGS=$(xwininfo -frame)

		# The values are in the second column.
		read X < <(awk -F: '/Absolute upper-left X/{print $2}' <<< "$LOGS")
		read Y < <(awk -F: '/Absolute upper-left Y/{print $2}' <<< "$LOGS")
		read W < <(awk -F: '/Width/{print $2}' <<< "$LOGS")
		read H < <(awk -F: '/Height/{print $2}' <<< "$LOGS")
		
		komanda &
		zenity \
			--icon=$ICON \
			--text="`eval_gettext 'The duration of the record is'` $DURATION `eval_gettext 'seconds'`.\n`eval_gettext 'By closing this window the recording will be cancelled'`." \
			--timeout=$TIMEOUT \
			--progress \
			--percentage=100
		killall -e -w byzanz-record
		exit 13
	fi
else
	echo `eval_gettext 'Error:'` `eval_gettext 'Program aborted'`"."
	exit 1
fi

