#!/bin/bash
#pClock
#Copyright 2011,2012,2014,2015,2016,2017
#Sigmund Berglund - Valåmoen, Norway.
# SFR/jamesbond: modifications/fixes for Fatdog64

#------------------------------
#This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation version 2.

#This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. <http://www.gnu.org/licenses/>.
#------------------------------


export TEXTDOMAIN=pclock #for gettext

export APPDIR=`dirname $0`
[ $APPDIR = '.' ] && export APPDIR=`pwd`
#export WORKDIR=$HOME/.pclock/tmp
export WORKDIR="/tmp/pclock_${USER}_${$}"
mkdir -p "${HOME}/.pclock"	# for permanent settings

trap 'sleep 3; rm -rf "$WORKDIR"; exit' 0 INT HUP TERM	# some delay needed for all subprocesses to finish

if [ ! -d $WORKDIR ]; then
	mkdir -p $WORKDIR
	echo true > $WORKDIR/HUNDREDTHS
	echo '/usr/share/audio/beep_high.wav' > $WORKDIR/COUNTDOWN_ACTION
	echo '/usr/share/audio/2barks.au' > $WORKDIR/ALARM
fi

#configs
TMP=`grep VERSION $HOME/.pclock/pclockrc 2> /dev/null` #check current version
if [ ! -f $HOME/.pclock/pclockrc ] || [ `echo $TMP | sed -e s/^.*\=//g | tr -d "."` != 082 > /dev/null 2>&1 ]; then
	#guess correct fontsize
	TMP=`xrdb -query | grep dpi | awk '{print $2}'` #check global fontsize
	FONTSIZE=$((4100/$TMP))
	#---
	echo 'export VERSION=0.8.2
export FONTSIZE='$FONTSIZE'
export COLOR1="black"
export COLOR2="gold"' > $HOME/.pclock/pclockrc
	chmod 722 $HOME/.pclock/pclockrc
fi
. $HOME/.pclock/pclockrc

#initials
rm -f $WORKDIR/end_while #start progessbar loop
echo -n > $WORKDIR/end_while_fullscreen #stop fullscreen loop
date +%s%N > $WORKDIR/seconds_start #save the time in nanoseconds (from 1970) to tempfile
echo -n > $WORKDIR/resultlist
echo analog > $WORKDIR/clock_mode
echo "
$(gettext 'pClock uses the program pSchedule 
to keep track of alarming. Click the 
Define-Alarm button and you will 
get new options to define time and 
action.

As a Command you can use, for example:
/usr/bin/aplay /usr/share/audio/2barks.au')" > $WORKDIR/alarm_txt

export ZONEINFO="/usr/share/zoneinfo"

MYGMT=$(readlink /etc/localtime)
if [ -z "$MYGMT" ]; then
	MYGMT="GMT"
else
	MYGMT=${MYGMT#${ZONEINFO}/} # jamesbond
fi
echo -n "$MYGMT" > "${WORKDIR}/timezone"



NOTEBOOK_TAB=0
echo 0 > $WORKDIR/pclock-tab
NOTEBOOK_TOOLS_TAB=0
echo 0 > $WORKDIR/pclock-tools_tab
echo 0 > $WORKDIR/COUNTDOWN
echo '' > $WORKDIR/COUNTDOWN_LOOPS
echo -n > $WORKDIR/timer

#gtk-theme
echo 'style "monoBig" { font_name="Mono bold '$FONTSIZE'" base[NORMAL]="'$COLOR1'" text[NORMAL]="'$COLOR2'" }
widget "*timer" style "monoBig"' > $WORKDIR/gtkrc
export GTK2_RC_FILES=$WORKDIR/gtkrc:~/.gtkrc-2.0 #include theme stuff with system themes



###############################################
#                                             #
#              F U N C T I O N S              #
#                                             #
###############################################

# SFR: moved here from a separate file (/usr/lib/gtkdialog/svg_analogclock)
svg_analogclock() {
	SIZE="$1"; [ ! "$SIZE" ] && SIZE=220
	GMT="$2"; [ ! "$GMT" ] && GMT=$TZ # jamesbond

	GMT="$(echo "$GMT" | tr '+-' '-+')"	# need to swap signs in GMT strings

	# jamesbond
	local hms=$(TZ=$GMT date +%l-%M-%S)
	SEC=${hms##*-}; SEC=${SEC#0}
	hms=${hms%-*}
	MIN=${hms##*-}; MIN=${MIN#0}
	HOUR=${hms%-*}

	SEC_ANGLE="`echo "$SEC * 6" | bc`"
	MIN_ANGLE="`echo "$MIN * 6" | bc`"
	HOUR_ANGLE=$(( HOUR * 30 + MIN_ANGLE/12 ))
	
	COLOR1="$3"; [ ! "$COLOR1" ] && COLOR1='#E6F1DD'
	COLOR2="$4"
	[ "$COLOR2" ] && COLOR='url(#RG1)' || COLOR="$COLOR1"
	
	echo '<?xml version="1.0" encoding="utf-8"?>
	<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="'$SIZE'" height="'$SIZE'" viewBox="0 0 512 512">
		<title>SVG analog clock</title>
	
		<defs>
		<radialGradient
			id="RG1" cx="400" cy="400" r="250"
			gradientUnits="userSpaceOnUse">
			<stop style="stop-color:'$COLOR2';stop-opacity:1;" offset="1"/>
			<stop style="stop-color:'$COLOR1';stop-opacity:1;" offset="0.2"/>
		</radialGradient>
		</defs>
	
		<circle fill="#777777" cx="256" cy="256" r="248" />
		<circle fill="#222222" cx="256" cy="256" r="245" />
		<circle fill="'$COLOR'" cx="256" cy="256" r="215.04" />
	
		<path fill="none" stroke="#000000" stroke-width="0.5" stroke-miterlimit="10" d="M173.3,437.8l-2.04,4.5 M211.9,455.8 l1.1-4.8 M191.2,450.1l1.6-4.7 M137.3,416.6l-3,4 M89.4,374.9l4.1-2.9 M102.7,391.6l3.7-3.3 M121.2,403.3l-3.4,3.7 M233,459.4l0.6-4.9 M68.3,337.7l4.6-2 M60.7,317.6l4.8-1.5 M390.6,108.6l3.5-3.9 M405.4,123.5l3.9-3.5 M422.6,136.7l-4.3,3 M439,176l4.7-2.1 M446.4,195.6l4.9-1.6 M320.8,61.5l-1.7,5 M300.1,55.9l-1.1,5.1 M374.5,95.3l3.1-4.2 M279,52.3 l-0.6,5.3 M316.3,446.4l1.5,4.7 M340.7,69.4l-2.2,4.8 M454.5,278.2l5.1,0.6 M416.7,374.5 l4.1,3 M372.2,418.4l2.9,4.1 M403.5,390.6l3.7,3.4 M388.5,405.4l3.3,3.7 M335.9,439 l2,4.5 M454.7,236.5l5.1-0.5 M456,300l-4.9-1.1 M450.3,320.6l-4.8-1.6 M442.5,340.6l-4.6-2.1 M456.6,214.8l-5,1 M66.6,192.7l-4.9-1.6 M52.5,232.9l5.1,0.6 M55.4,296.9l4.9-1 M52.1,275.7l5.1-0.5 M56,211.7l5,1.1 M195.8,65.6l-1.6-5 M216,60.3l-1-5.1 M176.2,72.9l-2.1-4.8 M236.2,52l0.5,5.3 M91.2,134.2l4.2,3.1 M136.9,89.2l3.1,4.3 M69.6,171.1l4.7,2.1 M108.7,121.2l-3.9-3.5 M120.2,102.6l3.5,3.9 M296.1,451.6l1,4.8 M275.8,459.7l-0.5-4.9" />
		<path d="M256,56.3l-2.5-5.1l5.1,0L256,56.3z M250.3,460.8l5.1,0l-2.5-5.1L250.3,460.8z M361.9,80.7l-4.4-2.6l-0.4,5.7L361.9,80.7z M435.4,157.1l-2.5-4.5l-3.2,4.8L435.4,157.1z M460.8,261.7l0-5.1l-5.1,2.5L460.8,261.7z M431.4,361.8l2.6-4.4l-5.7-0.4L431.4,361.8z M354.9,435.4l4.5-2.5l-4.8-3.2L354.9,435.4z M157.3,76.5l-4.5,2.5l4.8,3.2L157.3,76.5z M80.8,149.9l-2.6,4.4l5.7,0.4L80.8,149.9z M51.2,253.4l-0,5.1l5.1-2.5L51.2,253.4z M76.5,354.7l2.5,4.5l3.2-4.8L76.5,354.7z M150,431.3l4.4,2.6l0.4-5.7L150,431.3z" />
		<circle fill="none" stroke="#000" stroke-width="0.3" cx="256" cy="256" r="205.2" />
		<circle fill="none" stroke="#000" stroke-width="0.3" cx="256" cy="256" r="199.5" />
	
		<path d="M350.4,87v47.9c0,0.7,0.2,1.3,0.5,1.7c0.5,0.5,1,0.9,1.7,0.8h4.7v2.9h-17.3v-2.9h4c0.8,0,1.4-0.3,1.8-0.9c0.4-0.6,0.6-1.5,0.6-2.5v-36.2c0-0.7-0.2-1.4-0.6-1.9 c-0.4-0.5-0.8-0.8-1.4-0.8h-5.3v-2.6c2.5-0.5,4.5-1.2,6.1-2.1c1.6-0.9,2.9-2,4.1-3.4 L350.4,87L350.4,87z"/>
		<path d="M403.9,194.3c-0.7,1.5,2.9,2.3,10.9,2.3c1.2,0,2-0.5,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.7c0-2.6,0.4-5,1.3-7.1c0.8-2.1,2.1-3.9,3.8-5.3c1.7-1.4,3.5-2.1,5.5-2.1c1.9,0,3.7,0.7,5.4,2c1.7,1.3,2.9,3.1,3.7,5.2c0.8,2.2,1.2,4.5,1.2,7.1c0,1.8-0.2,3.6-0.6,5.4c-0.4,1.8-1,3.5-1.8,5.1c-1.4,2.7-2.8,5.1-4.2,7l-6.3,8.5C266.8,109.1,265.6,110.9,264.9,112.4z"/>
	
		<polygon id="clockhand_hour" points="252.5,156 252.5,256 252.5,286 259.5,286 259.5,256 259.5,156" transform="rotate('$HOUR_ANGLE',256,256)"/>
		<polygon id="clockhand_min" points="253.5,115 253.5,256 253.5,301 258.5,301 258.5,256 258.5,115" transform="rotate('$MIN_ANGLE',256,256)"/>
		<polygon id="clockhand_sec" points="258,271 257,271 257,256 257,126 255,126 255,256 255,271 254,271 252,321 260,321" transform="rotate('$SEC_ANGLE',256,256)"/>
		<circle fill="#000000" cx="256" cy="256" r="8" />
	
	</svg>'
}
export -f svg_analogclock


analogclock(){
	#clock size
	if [ ! -f $WORKDIR/end_while_fullscreen ]; then
		SCREEN_HEIGHT=`xwininfo -root | grep -m 1 '\geometry' | cut -f4 -d ' ' | cut -f2 -d 'x' | cut -f1 -d '+'`
		SIZE=$(($SCREEN_HEIGHT-100))
		OUTFILE=$WORKDIR/pclock-analog_big.svg
	else
		SIZE=300
		OUTFILE=$WORKDIR/pclock-analog.svg
	fi
	#/usr/lib/gtkdialog/svg_analogclock "$SIZE" "$1" > $OUTFILE
	svg_analogclock "$SIZE" "$1" > $OUTFILE
}


func_time(){ #called as input for the entry
	export GTK2_RC_FILES=/root/.gtkrc-2.0 #reset gtk
	case "`cat $WORKDIR/clock_mode`" in
	null)
		echo '00:00:00' > $WORKDIR/timer
		echo; echo 100 #update clock
		sleep 1
		;;
	analog)
		echo; echo 100 #update clock
		sleep 0.95
		;;
	clock12)
		NANOSEC=`date +%N | head -c 2`
		echo $NANOSEC > $WORKDIR/NANOSEC #for fullscreen
		if [ $NANOSEC -lt 25 ]; then
			date +%I:%M:%S > $WORKDIR/timer
			echo; echo 100 #update clock
			sleep 0.6 #no use in looping until we are closer to whole second
		fi
		;;
	clock24)
		NANOSEC=`date +%N | head -c 2`
		echo $NANOSEC > $WORKDIR/NANOSEC #for fullscreen
		if [ $NANOSEC -lt 25 ]; then
			date +%H:%M:%S > $WORKDIR/timer
			echo; echo 100 #update clock
			sleep 0.6 #no use in looping until we are closer to whole second
		fi
		;;
	stopwatch)
		date +%s%N > $WORKDIR/seconds_now
		SEC_START=$(<$WORKDIR/seconds_start)
		echo "$(($(<$WORKDIR/seconds_now)-$SEC_START))" | tail -c 10 | head -c 2 > $WORKDIR/NANOSEC #this file is used by fullscreen
		NANOSEC=($(<$WORKDIR/NANOSEC))
		if [ $NANOSEC -lt 25 ]; then
			TIME_SEC=`echo "($(<$WORKDIR/seconds_now) - $SEC_START) / 1000000000" | bc 2> /dev/null`
			printf "%02d:%02d:%02d" $((TIME_SEC/3600)) $((TIME_SEC%3600/60)) $((TIME_SEC%60)) > $WORKDIR/timer
			echo; echo 100 #update clock
			sleep 0.6 #no use in looping until we are closer to whole second
		fi
		;;
	countdown_start)
		COUNTDOWN_SEC="`cat $WORKDIR/COUNTDOWN`"
		printf "%02d:%02d:%02d" $((COUNTDOWN_SEC/3600)) $((COUNTDOWN_SEC%3600/60)) $((COUNTDOWN_SEC%60)) > $WORKDIR/timer
		echo; echo 100 #update clock
		sleep 1
		;;
	countdown)
		date +%s%N > $WORKDIR/seconds_now
		SEC_NOW=$(($(<$WORKDIR/seconds_now)-1000000000))
		SEC_START=$(<$WORKDIR/seconds_start)
		NANOSEC=`echo "$(($SEC_START-$SEC_NOW))" | tail -c 10 | head -c 2`
		if [ $NANOSEC -gt 75 ]; then
			TIME_SEC=`echo "($SEC_NOW - $SEC_START) / 1000000000" | bc 2> /dev/null`
			COUNTDOWN_SEC="$((($SEC_START-$SEC_NOW)/1000000000))"
			printf "%02d:%02d:%02d" $((COUNTDOWN_SEC/3600)) $((COUNTDOWN_SEC%3600/60)) $((COUNTDOWN_SEC%60)) > $WORKDIR/timer
			echo; echo 100 #update clock
			echo $COUNTDOWN_SEC > $WORKDIR/COUNTDOWN
			if [ $COUNTDOWN_SEC = 0 ]; then
				TMP="$(<$WORKDIR/COUNTDOWN_ACTION)"
				if [ -f "$TMP" ]; then #file is defined --> try to play it 
					if [ "`echo "$TMP" | grep -E "\.wav$|\.au$"`" ]; then
						aplay "$TMP" &
					else
						ffmpeg -i "$TMP" -f au - | aplay &
					fi
				else
					chmod 722 $WORKDIR/COUNTDOWN_ACTION
					$WORKDIR/COUNTDOWN_ACTION &
				fi
				TMP="$(<$WORKDIR/COUNTDOWN_START)"
				echo $(($(date +%s)+$TMP))$(date +%N) > $WORKDIR/seconds_start
				#---
				TMP="$(<$WORKDIR/COUNTDOWN_LOOPS)"
				echo $(($TMP+1)) > $WORKDIR/COUNTDOWN_LOOPS

			fi
			sleep 0.6 #no use in looping until we are closer to whole second
		fi
		;;
	esac
}

timestamp(){
	date +%s%N > $WORKDIR/seconds_now
	SEC_SINCE_START="$((($(<$WORKDIR/seconds_now)-$(<$WORKDIR/seconds_start))/1000000000))"
	NANOSEC=`echo "$(($(<$WORKDIR/seconds_now)-$(<$WORKDIR/seconds_start)))" | tail -c 10 | head -c 2`
	TIME=`printf "%02d:%02d:%02d" $((SEC_SINCE_START/3600)) $((SEC_SINCE_START%3600/60)) $((SEC_SINCE_START%60))`
	[ -f $WORKDIR/HUNDREDTHS ] && TIME=${TIME}.$NANOSEC
	#laptime
	TIME_PREV=`tail -n1 $WORKDIR/resultlist | cut -d'|' -f3` #previous timestamp
	if [ $TIME_PREV = 00:00:00 ]; then
		LAPTIME=$TIME
	else
		HOUR=`echo $TIME_PREV | cut -d: -f1`
		MIN=`echo $TIME_PREV | cut -d: -f2`
		SEC=`echo $TIME_PREV | cut -d: -f3 | cut -d. -f1`
		NSEC=`echo $TIME_PREV | cut -d. -f2`
		if [ ! -f $WORKDIR/HUNDREDTHS ]; then NSEC=0; NANOSEC=0; fi
		LAPTIME_PREV_SEC=`echo "($HOUR*3600)+($MIN*60)+($SEC)" | bc`
		LAPTIME_SEC=`echo ${SEC_SINCE_START}.${NANOSEC} - ${LAPTIME_PREV_SEC}.${NSEC} | bc | cut -d. -f1`
		LAPTIME_NSEC=`echo ${SEC_SINCE_START}.${NANOSEC} - ${LAPTIME_PREV_SEC}.${NSEC} | bc | cut -d. -f2`
		LAPTIME=`printf "%02d:%02d:%02d" $((LAPTIME_SEC/3600)) $((LAPTIME_SEC%3600/60)) $((LAPTIME_SEC%60))`
		[ -f $WORKDIR/HUNDREDTHS ] && LAPTIME=${LAPTIME}.$LAPTIME_NSEC
	fi
	#---
	TMP=`tail -n1 $WORKDIR/resultlist | cut -d'|' -f1`
	if [ ! "$TMP" ]; then LAPNR=1; else LAPNR=$((($TMP)+1)); fi
	echo "$LAPNR|$LAPTIME|$TIME" >> $WORKDIR/resultlist
}

fullscreen (){
	rm -f $WORKDIR/end_while_fullscreen #activate loop
	#geometry
	HEIGHT=`xwininfo -root | grep -m 1 '\geometry' | cut -f4 -d ' ' | cut -f2 -d 'x' | cut -f1 -d '+'`
	WIDTH=`xwininfo -root | grep -m 1 '\geometry' | cut -f4 -d ' ' | cut -f1 -d 'x'`
	#gtk-theme
	echo 'style "monohuge" { font_name="Mono bold '`echo "( $WIDTH * $FONTSIZE ) / 280" | bc`'" bg[NORMAL]="'$COLOR1'" base[NORMAL]="'$COLOR1'" fg[NORMAL]="'$COLOR2'" text[NORMAL]="'$COLOR2'" }
	widget "*" style "monohuge"' > $WORKDIR/gtkrc2
	export GTK2_RC_FILES=$WORKDIR/gtkrc2:/root/.gtkrc-2.0 #include theme stuff with system themes
	#gui  	
	if [ "$RADIOBUTTON_ANALOG_CLOCK" = "true" ]; then
		export curtain='<window title="pClock '$LOC_FULLSCREEN'" width-request="'$WIDTH'" height-request="'$HEIGHT'" decorated="false">
		<vbox>
		 <hbox width-request="1">
		  <button relief="2" can-focus="no"><input file stock="gtk-close"></input><action>exit:EXIT</action></button>
		  <timer visible="false">
	       <action>analogclock $GMT</action>
		   <action>refresh:PIX_ANALOG_CLOCK</action>
		  </timer>
		 </hbox>
		 <pixmap space-expand="false" space-fill="false"><variable>PIX_ANALOG_CLOCK</variable><input file>'$WORKDIR'/pclock-analog_big.svg</input></pixmap>
		</vbox>
		<action signal="hide">exit:EXIT</action>
		</window>'
	else
		export curtain='<window title="pClock '$LOC_FULLSCREEN'" width-request="'$WIDTH'" height-request="'$HEIGHT'" decorated="false">
		<vbox>
		 <hbox width-request="1">
		  <button relief="2" can-focus="no"><input file stock="gtk-close"></input><action>exit:EXIT</action></button>
		  <vbox height-request="1">
		   <progressbar width-request="1" height-request="1" >
		    <input>while [ ! -f '$WORKDIR'/end_while_fullscreen ]; do if [ `cat '$WORKDIR'/NANOSEC` -lt 25 ]; then echo; echo 100; sleep 0.4; fi; sleep 0.05; done</input>
		    <action>refresh:TIMER</action>
		   </progressbar>
		  </vbox>
		 </hbox>
		 <entry can-focus="no" width-request="'$WIDTH'" height-request="'$((HEIGHT/3))'">
		  <variable>TIMER</variable>
		  <input>cat '$WORKDIR'/timer</input>
		  <action signal="button-press-event">exit:EXIT</action>
		 </entry>
		</vbox>
		<action signal="hide">exit:EXIT</action>
		</window>'
	fi
	gtkdialog -p curtain --center
	echo end > $WORKDIR/end_while_fullscreen
	export GTK2_RC_FILES=$WORKDIR/gtkrc:/root/.gtkrc-2.0 #reset gtk-theme
}

preferences(){
	export GTK2_RC_FILES=/root/.gtkrc-2.0 #reset gtk
	export pClock_pref='
	<window title="pClock - '$(gettext "Preferences")'">
	 <vbox>
	  <hbox>
	   <text><label>'$(gettext "Font size")'</label></text>
	   <entry><variable>FONTSIZE</variable><default>'$FONTSIZE'</default></entry>
	  </hbox>
	  <hbox>
	   <text><label>'$(gettext "Background Colour")'</label></text>
	   <entry><variable>COLOR1</variable><default>'$COLOR1'</default></entry>
	  </hbox>
	  <hbox>
	   <text><label>'$(gettext "Foreground Colour")'</label></text>
	   <entry><variable>COLOR2</variable><default>'$COLOR2'</default></entry>
	  </hbox>
	  <hbox>
	   <text use-markup="true"><label>"<b>'$(gettext "Restart pClock to activate changes")'</b>    "</label></text>
	   <button cancel></button>
	   <button ok>
	    <action>echo -e "export VERSION=$VERSION\nexport FONTSIZE=$FONTSIZE\nexport COLOR1=\"$COLOR1\"\nexport COLOR2=\"$COLOR2\"" > '$HOME'/.pclock/pclockrc</action>
	    <action>EXIT:exit</action>
	    </button>
	  </hbox>
	 </vbox>
	</window>'
	gtkdialog -p pClock_pref
}

system_time (){
	export GTK2_RC_FILES=/root/.gtkrc-2.0 #reset gtk
	set-time-for-puppy &
}

alarm(){
	export GTK2_RC_FILES=/root/.gtkrc-2.0 #reset gtk
#	pschedule -s -e "pmusic -B '$ALARM'" "pClock Alarm" &
	#if type pmusic > /dev/null 2>&1; then
	#	pschedule -s "pmusic -B '$ALARM'" "pClock Alarm" &
	#else
	#	pschedule -s "defaultaudioplayer '$ALARM'" "pClock Alarm" &
	#fi
	
	pschedule &	# Fatdog
	
}

read_dir() {
	local CURDIR

	if [ ! -d "$1" ]; then
		echo -n "${1#${ZONEINFO}/}" > "${WORKDIR}/timezone"
		return
	fi

	if [ "${1##*/}" = ".." ]; then
		CURDIR="$(echo ${1%/*})"
		CURDIR="${CURDIR%/*}"
	else
		CURDIR="$(echo "${1}")"
	fi

	if [ "$CURDIR" != "$ZONEINFO" ]; then
		echo 'gtk-go-up|..' > ${WORKDIR}/tzlist
	else
		echo -n > ${WORKDIR}/tzlist
	fi

	if [ -d "$CURDIR" ]; then
		{ find -L "$CURDIR" -mindepth 1 -maxdepth 1 -type d -not -path '*\/.*' -printf "gtk-directory|%P\n";
		  find -L "$CURDIR" -mindepth 1 -maxdepth 1 ! -type d -not -path '*\/.*' -not -iname "*.tab" -printf "gtk-new|%P\n"; } | \
		  sed "s/$/|/" | \
		  sort -fV >> $WORKDIR/tzlist
    
		echo "${CURDIR}" > ${WORKDIR}/currentdir
	fi
}

read_dir "$ZONEINFO"	# init

export -f analogclock func_time timestamp fullscreen preferences system_time alarm read_dir


###############################################
#                                             #
#                  A B O U T                  #
#                                             #
###############################################

export about="
<window title=\"pClock - about\">
 <notebook labels=\"About|License\">
  <frame>
   <text><label>\"\"</label></text>
   <progressbar>
    <input>echo 0; echo \"pClock $VERSION\"</input>
   </progressbar>
   <text use-markup=\"true\"><label>\"<b>Sigmund Berglund, Copyright 2011-2017</b>\"</label></text>
   <text><label>Fixes/mods for Fatdog by JakeSFR & jamesbond</label></text>
   <text height-request=\"30\"><label>\"\"</label></text>
   <button>
    <label>\"http://www.murga-linux.com/puppy/viewtopic.php?p=493137\"</label>
    <action>defaulthtmlviewer http://www.murga-linux.com/puppy/viewtopic.php?p=493137#493137 &</action>
   </button>
  </frame>
  <frame>
   <text use-markup=\"true\"><label>\"pClock is released under the <b>GNU General Public License</b> (GPL). You have the right to use and modify this software in any way you like, so long as any derivative works remain under a GPL license.\"</label></text>
   <text use-markup=\"true\"><label>\"This program is distributed in the hope that it will be useful, but <b><span color='"'red'"'>WITHOUT ANY WARRANTY</span></b>. See the GNU General Public License homepage for more details.\"</label></text>
   <hbox>
    <button>
     <label>\"http://www.gnu.org/licenses/\"</label>
     <action>defaulthtmlviewer http:\/\/www.gnu.org\/licenses\/</action>
    </button>
   </hbox>
  </frame>
 </notebook>
 <variable>about</variable>
</window>"



###############################################
#                                             #
#               M A I N   G U I               #
#                                             #
###############################################

export pClock='
<window title="pClock">
<vbox>
  <menubar>
   <menu>
    <menuitem stock="gtk-preferences">
     <action>preferences</action>
    </menuitem>
    <menuitem label="'$(gettext "Set system time")'">
     <action>system_time &</action>
    </menuitem>
    <menuitem label="'$(gettext "Set system time zone")'">
     <action>fatdog-set-timezone.sh &</action>
    </menuitem>
    <menuitem label="'$(gettext "Set per-user time zone")'">
     <action>fatdog-set-timezone.sh user &</action>
    </menuitem>
    <menuitemseparator></menuitemseparator>
    <menuitem  stock="gtk-quit" >
     <action>exit:EXIT</action>
    </menuitem>
    <label>'$(gettext "File")'</label>
   </menu>

   <menu>
    <menuitem  stock="gtk-fullscreen">
     <action>fullscreen &</action>
    </menuitem>
    <label>'$(gettext "View")'</label>
   </menu>
   <menu>
    <menuitem  stock="gtk-about">
     <action>launch:about</action>
    </menuitem>
    <label>'$(gettext "Help")'</label>
   </menu>
  </menubar>
  
  <hbox homogeneous="true">
   <hbox>
    <radiobutton label=" '$(gettext "Clock")' " draw_indicator="false">
     <action>echo analog > '$WORKDIR'/clock_mode</action>
     <variable>RADIOBUTTON_ANALOG_CLOCK</variable>
     <action>echo 0 > '$WORKDIR'/pclock-tab</action>
     <action>refresh:NOTEBOOK</action>
     <action>refresh:NOTEBOOK_TOOLS</action>
    </radiobutton>
    <radiobutton label=" '$(gettext "Stopwatch")' " draw_indicator="false">
     <action>echo null > '$WORKDIR'/clock_mode</action>
     <action>echo 1 > '$WORKDIR'/pclock-tools_tab</action>
     <action>echo 1 > '$WORKDIR'/pclock-tab</action>
     <action>refresh:NOTEBOOK</action>
     <action>refresh:NOTEBOOK_TOOLS</action>
    </radiobutton>
    <radiobutton label=" '$(gettext "Countdown")' " draw_indicator="false">
     <action>echo countdown_start > '$WORKDIR'/clock_mode</action>
     <action>echo 2 > '$WORKDIR'/pclock-tools_tab</action>
     <action>echo 1 > '$WORKDIR'/pclock-tab</action>
     <action>disable:BUTTON_COUNTDOWN_START</action>
     <action>disable:BUTTON_COUNTDOWN_STOP</action>
     <action>disable:BUTTON_COUNTDOWN_PAUSE</action>
     <action>refresh:NOTEBOOK</action>
     <action>refresh:NOTEBOOK_TOOLS</action>
    </radiobutton>
    <radiobutton label=" '$(gettext "Alarm")' " draw_indicator="false">
     <action>echo clock24 > '$WORKDIR'/clock_mode</action>
     <action>echo 3 > '$WORKDIR'/pclock-tools_tab</action>
     <action>echo 1 > '$WORKDIR'/pclock-tab</action>
     <action>refresh:NOTEBOOK</action>
     <action>refresh:NOTEBOOK_TOOLS</action>
    </radiobutton>
##    <radiobutton label=" '$(gettext "System")' " draw_indicator="false">
##     <action>echo 0 > '$WORKDIR'/pclock-tools_tab</action>
##     <action>echo 1 > '$WORKDIR'/pclock-tab</action>
##     <action>refresh:NOTEBOOK</action>
##     <action>refresh:NOTEBOOK_TOOLS</action>
##    </radiobutton>
   </hbox>
  </hbox>
  <hseparator></hseparator>
  <hbox width-request="1">
   <progressbar width-request="1" height-request="1" >
    <input>while [ ! -f '$WORKDIR'/end_while ]; do func_time; sleep 0.05; done</input>
    <action>refresh:TIMER</action>
    <action>analogclock $GMT</action>
    <action>refresh:PIX_ANALOG_CLOCK</action>
   </progressbar>
  </hbox>
  <notebook show-tabs="false" show-border="false" page="'$NOTEBOOK_TAB'" space-expand="true" space-fill="true">

   <vbox space-fill="true" space-expand="true">
  
    <vbox>
	  <text>
	    <variable>GMT</variable>
	    <input file>'${WORKDIR}/timezone'</input>
	  </text>

      <text><label>""</label></text>
      <pixmap space-expand="false" space-fill="false"><variable>PIX_ANALOG_CLOCK</variable><input file>'$WORKDIR'/pclock-analog.svg</input></pixmap>

      <text space-fill="true" space-expand="true"><label>""</label></text>

	  <button height-request="50" space-fill="false" space-expand="false">
	    <label>'$( gettext "Change timezone")'</label>
	    <action>hide:CLOCKVIEW</action>
	    <action>show:TZVIEW</action>
	  </button>
	  
	  <variable>CLOCKVIEW</variable>
	</vbox>

	<vbox visible="false">
		<text><label>'$(gettext "Press Enter or double-click on the desired timezone:")'</label></text>

		<tree block-function-signals="true" headers-visible="false" selection-mode="1" enable-search="false">
			<variable>TIMEZONE</variable>
			<input file stock-column="0">'$WORKDIR/tzlist'</input>
			<action>read_dir "$(cat $WORKDIR/currentdir)/${TIMEZONE}"</action>
			<action>refresh:GMT</action>
			<action condition="command_is_true( CURDIR=$(cat '$WORKDIR/currentdir'); [ ${CURDIR}/${TIMEZONE} = '${ZONEINFO}'/${GMT} ] && echo true )">hide:TZVIEW</action>
			<action condition="command_is_true( CURDIR=$(cat '$WORKDIR/currentdir'); [ ${CURDIR}/${TIMEZONE} = '${ZONEINFO}'/${GMT} ] && echo true )">show:CLOCKVIEW</action>
			<action condition="command_is_true( CURDIR=$(cat '$WORKDIR/currentdir'); [ ${CURDIR}/${TIMEZONE} = '${ZONEINFO}'/${GMT} ] && echo true )">read_dir '"$ZONEINFO"'</action>
			<action>clear:TIMEZONE</action>
			<action>refresh:TIMEZONE</action>
		</tree>
		
		<button cancel>
			<action>hide:TZVIEW</action>
			<action>show:CLOCKVIEW</action>
			<action>read_dir '"$ZONEINFO"'</action>
			<action>clear:TIMEZONE</action>
			<action>refresh:TIMEZONE</action>
		</button>

		<variable>TZVIEW</variable>
	</vbox>

   </vbox>

   <vbox>
    <hbox homogeneous="true">
     <hbox>
      <entry name="timer" width-request="300" height-request="60" editable="false" xalign="0">
       <variable>TIMER</variable>
       <input>cat '$WORKDIR'/timer</input>
      </entry>
     </hbox>
    </hbox>

    <notebook  show-tabs="false" show-border="false" page="'$NOTEBOOK_TOOLS_TAB'" space-expand="true" space-fill="true">
     <frame>
      <hbox>
       <button>
        <label>12 '$(gettext "Hours")'</label>
        <action>echo clock12 > '$WORKDIR'/clock_mode</action>
       </button>
       <button>
        <label>24 '$(gettext "Hours")'</label>
        <action>echo clock24 > '$WORKDIR'/clock_mode</action>
       </button>
      </hbox>
      <hbox>
       <button>
        <label>'$(gettext "Set system time")'</label>
        <action>system_time &</action>
       </button>
       <button>
        <label>'$(gettext "Set system time zone")'</label>
        <action>fatdog-set-timezone.sh &</action>
       </button>
       <button>
        <label>'$(gettext "Set per-user time zone")'</label>
        <action>fatdog-set-timezone.sh user &</action>
       </button>
      </hbox>
     </frame>

     <vbox>
      <hbox>
       <button space-expand="false" space-fill="false">
        <label>" '$(gettext "Start")' "</label>
        <action>date +%s%N > '$WORKDIR'/seconds_start</action>
        <action>echo "||00:00:00" > '$WORKDIR'/resultlist</action>
        <action>echo stopwatch > '$WORKDIR'/clock_mode</action>
        <action>echo 00:00:00 > '$WORKDIR'/timer</action>
        <action>refresh:TIMER</action>
        <action>refresh:RESULTLIST</action>
        <action>enable:BUTTON_LAP</action>
        <action>enable:BUTTON_SPLIT</action>
       </button>
       <text space-expand="true" space-fill="true"><label>""</label></text>
       <checkbox>
        <label>'$(gettext "Count hundredths")'</label>
        <default>true</default>
        <action>if true echo true > '$WORKDIR'/HUNDREDTHS</action>
        <action>if false rm '$WORKDIR'/HUNDREDTHS</action>
       </checkbox>
       <button>
        <variable>BUTTON_SPLIT</variable>
        <label>" '$(gettext "Split")' "</label>
        <visible>disabled</visible>
        <action>timestamp</action>
        <action>date +%s%N > '$WORKDIR'/seconds_start</action>
        <action>echo "||00:00:00" >> '$WORKDIR'/resultlist</action>
        <action>echo 00:00:00 > '$WORKDIR'/timer</action>
        <action>refresh:RESULTLIST</action>
        <action>refresh:TIMER</action>
       </button>
       <button>
        <variable>BUTTON_LAP</variable>
        <label>" '$(gettext "Lap")' "</label>
        <visible>disabled</visible>
        <action>timestamp</action>
        <action>refresh:RESULTLIST</action>
       </button>
      </hbox>
      <tree>
       <variable>RESULTLIST</variable>
       <height>250</height><width>100</width>
       <label>"'$(gettext "Lap")'|'$(gettext "Laptime")'     |'$(gettext "Time")'"</label>
       <input>tac '$WORKDIR'/resultlist</input>
      </tree>
     </vbox>
   
     <vbox>
      <text height-request="10"><label>""</label></text>
      <hbox homogeneous="true">
      <hbox>
      <text width-request="50"><label>""</label></text>
      <button height-request="50">
       <label>"  '$(gettext "Start")'  "</label>
       <variable>BUTTON_COUNTDOWN_START</variable>
       <action>echo countdown > '$WORKDIR'/clock_mode</action>
       <action>echo "" > '$WORKDIR'/COUNTDOWN_LOOPS</action>
       <action>TMP="`cat '$WORKDIR'/COUNTDOWN`"; echo $(($(date +%s)+$TMP))$(date +%N) > '$WORKDIR'/seconds_start; echo $TMP > '$WORKDIR'/COUNTDOWN_START</action>
       <action>echo $COUNTDOWN_ACTION > /'$WORKDIR'/COUNTDOWN_ACTION</action>
       <action>enable:BUTTON_COUNTDOWN_STOP</action>
       <action>enable:BUTTON_COUNTDOWN_PAUSE</action>
       <action>disable:BUTTON_COUNTDOWN_START</action>
       <action>refresh:COUNTDOWN_LOOPS</action>
      </button>
      <checkbox label="  '$(gettext "Pause")'  " draw_indicator="false" height-request="50">
       <variable>BUTTON_COUNTDOWN_PAUSE</variable>
       <action>if true echo countdown_start > '$WORKDIR'/clock_mode</action>
       <action>if false echo countdown > '$WORKDIR'/clock_mode</action>
       <action>TMP="`cat '$WORKDIR'/COUNTDOWN`"; echo $(($(date +%s)+$TMP))$(date +%N) > '$WORKDIR'/seconds_start</action>
       <action>if true disable:BUTTON_COUNTDOWN_STOP</action>
       <action>if false enable:BUTTON_COUNTDOWN_STOP</action>
      </checkbox>
      <button label="  '$(gettext "Stop")'  " height-request="50">
       <variable>BUTTON_COUNTDOWN_STOP</variable>
       <action>echo 0 > '$WORKDIR'/COUNTDOWN</action>
       <action>echo countdown_start > '$WORKDIR'/clock_mode</action>
       <action>disable:BUTTON_COUNTDOWN_START</action>
       <action>disable:BUTTON_COUNTDOWN_PAUSE</action>
       <action>disable:BUTTON_COUNTDOWN_STOP</action>
      </button>
      <text width-request="50"><variable>COUNTDOWN_LOOPS</variable><input>cat '$WORKDIR'/COUNTDOWN_LOOPS</input></text>
      </hbox>
      </hbox>
      <text height-request="30"><label>""</label></text>








      <hbox homogeneous="true">
      <hbox>
       <vbox>
        <button label="+10">
         <action>TMP="`cat '$WORKDIR'/COUNTDOWN`"; echo $TMP + 36000 | bc > '$WORKDIR'/COUNTDOWN</action>
         <action>COUNTDOWN_SEC="`cat '$WORKDIR'/COUNTDOWN`"; printf "%02d:%02d:%02d" $((COUNTDOWN_SEC/3600)) $((COUNTDOWN_SEC%3600/60)) $((COUNTDOWN_SEC%60)) > '$WORKDIR'/timer</action>
         <action>enable:BUTTON_COUNTDOWN_START</action>
         <action>refresh:TIMER</action>
        </button>
        <button label="+1">
         <action>TMP="`cat '$WORKDIR'/COUNTDOWN`"; echo $TMP + 3600 | bc > '$WORKDIR'/COUNTDOWN</action>
         <action>COUNTDOWN_SEC="`cat '$WORKDIR'/COUNTDOWN`"; printf "%02d:%02d:%02d" $((COUNTDOWN_SEC/3600)) $((COUNTDOWN_SEC%3600/60)) $((COUNTDOWN_SEC%60)) > '$WORKDIR'/timer</action>
         <action>enable:BUTTON_COUNTDOWN_START</action>
         <action>refresh:TIMER</action>
        </button>
        <text use-markup="true"><label>"<b>'$(gettext "Hour")'</b>"</label></text>
        <button label="-1">
         <action>TMP="`cat '$WORKDIR'/COUNTDOWN`"; if [ $TMP -ge 3600 ]; then echo $TMP - 3600 | bc > '$WORKDIR'/COUNTDOWN; fi</action>
         <action>COUNTDOWN_SEC="`cat '$WORKDIR'/COUNTDOWN`"; printf "%02d:%02d:%02d" $((COUNTDOWN_SEC/3600)) $((COUNTDOWN_SEC%3600/60)) $((COUNTDOWN_SEC%60)) > '$WORKDIR'/timer</action>
         <action>refresh:TIMER</action>
        </button>
        <button label="-10">
         <action>TMP="`cat '$WORKDIR'/COUNTDOWN`"; if [ $TMP -ge 36000 ]; then echo $TMP - 36000 | bc > '$WORKDIR'/COUNTDOWN; fi</action>
         <action>COUNTDOWN_SEC="`cat '$WORKDIR'/COUNTDOWN`"; printf "%02d:%02d:%02d" $((COUNTDOWN_SEC/3600)) $((COUNTDOWN_SEC%3600/60)) $((COUNTDOWN_SEC%60)) > '$WORKDIR'/timer</action>
         <action>refresh:TIMER</action>
        </button>
       </vbox>
       <vbox>
        <button label="+10">
         <action>TMP="`cat '$WORKDIR'/COUNTDOWN`"; echo $TMP + 600 | bc > '$WORKDIR'/COUNTDOWN</action>
         <action>COUNTDOWN_SEC="`cat '$WORKDIR'/COUNTDOWN`"; printf "%02d:%02d:%02d" $((COUNTDOWN_SEC/3600)) $((COUNTDOWN_SEC%3600/60)) $((COUNTDOWN_SEC%60)) > '$WORKDIR'/timer</action>
         <action>enable:BUTTON_COUNTDOWN_START</action>
         <action>refresh:TIMER</action>
        </button>
        <button label="+1">
         <action>TMP="`cat '$WORKDIR'/COUNTDOWN`"; echo $TMP + 60 | bc > '$WORKDIR'/COUNTDOWN</action>
         <action>COUNTDOWN_SEC="`cat '$WORKDIR'/COUNTDOWN`"; printf "%02d:%02d:%02d" $((COUNTDOWN_SEC/3600)) $((COUNTDOWN_SEC%3600/60)) $((COUNTDOWN_SEC%60)) > '$WORKDIR'/timer</action>
         <action>enable:BUTTON_COUNTDOWN_START</action>
         <action>refresh:TIMER</action>
        </button>
        <text use-markup="true"><label>"<b>'$(gettext "Min")'</b>"</label></text>
        <button label="-1">
         <action>TMP="`cat '$WORKDIR'/COUNTDOWN`"; if [ $TMP -ge 60 ]; then echo $TMP - 60 | bc > '$WORKDIR'/COUNTDOWN; fi</action>
         <action>COUNTDOWN_SEC="`cat '$WORKDIR'/COUNTDOWN`"; printf "%02d:%02d:%02d" $((COUNTDOWN_SEC/3600)) $((COUNTDOWN_SEC%3600/60)) $((COUNTDOWN_SEC%60)) > '$WORKDIR'/timer</action>
         <action>refresh:TIMER</action>
        </button>
        <button label="-10">
         <action>TMP="`cat '$WORKDIR'/COUNTDOWN`"; if [ $TMP -ge 600 ]; then echo $TMP - 600 | bc > '$WORKDIR'/COUNTDOWN; fi</action>
         <action>COUNTDOWN_SEC="`cat '$WORKDIR'/COUNTDOWN`"; printf "%02d:%02d:%02d" $((COUNTDOWN_SEC/3600)) $((COUNTDOWN_SEC%3600/60)) $((COUNTDOWN_SEC%60)) > '$WORKDIR'/timer</action>
         <action>refresh:TIMER</action>
        </button>
       </vbox>
       <vbox>
        <button label="+10">
         <action>TMP="`cat '$WORKDIR'/COUNTDOWN`"; echo $TMP + 10 | bc > '$WORKDIR'/COUNTDOWN</action>
         <action>COUNTDOWN_SEC="`cat '$WORKDIR'/COUNTDOWN`"; printf "%02d:%02d:%02d" $((COUNTDOWN_SEC/3600)) $((COUNTDOWN_SEC%3600/60)) $((COUNTDOWN_SEC%60)) > '$WORKDIR'/timer</action>
         <action>enable:BUTTON_COUNTDOWN_START</action>
         <action>refresh:TIMER</action>
        </button>
        <button label="+1">
         <action>TMP="`cat '$WORKDIR'/COUNTDOWN`"; echo $TMP + 1 | bc > '$WORKDIR'/COUNTDOWN</action>
         <action>COUNTDOWN_SEC="`cat '$WORKDIR'/COUNTDOWN`"; printf "%02d:%02d:%02d" $((COUNTDOWN_SEC/3600)) $((COUNTDOWN_SEC%3600/60)) $((COUNTDOWN_SEC%60)) > '$WORKDIR'/timer</action>
         <action>enable:BUTTON_COUNTDOWN_START</action>
         <action>refresh:TIMER</action>
        </button>
        <text use-markup="true"><label>"<b>'$(gettext "Sec")'</b>"</label></text>
        <button label="-1">
         <action>TMP="`cat '$WORKDIR'/COUNTDOWN`"; if [ $TMP -ge 1 ]; then echo $TMP - 1 | bc > '$WORKDIR'/COUNTDOWN; fi</action>
         <action>COUNTDOWN_SEC="`cat '$WORKDIR'/COUNTDOWN`"; printf "%02d:%02d:%02d" $((COUNTDOWN_SEC/3600)) $((COUNTDOWN_SEC%3600/60)) $((COUNTDOWN_SEC%60)) > '$WORKDIR'/timer</action>
         <action>refresh:TIMER</action>
        </button>
        <button label="-10">
         <action>TMP="`cat '$WORKDIR'/COUNTDOWN`"; if [ $TMP -ge 10 ]; then echo $TMP - 10 | bc > '$WORKDIR'/COUNTDOWN; fi</action>
         <action>COUNTDOWN_SEC="`cat '$WORKDIR'/COUNTDOWN`"; printf "%02d:%02d:%02d" $((COUNTDOWN_SEC/3600)) $((COUNTDOWN_SEC%3600/60)) $((COUNTDOWN_SEC%60)) > '$WORKDIR'/timer</action>
         <action>refresh:TIMER</action>
        </button>
       </vbox>
      </hbox>
      </hbox>









      <vbox space-expand="true" space-fill="true"><text><label>""</label></text></vbox>
      <vbox space-expand="false" space-fill="false" tooltip-text="'$(gettext "You can either play an audio-file
or execute a defined command.")'">
       <text><label>'$(gettext "Sound/Action at zero")'</label></text>
       <hbox>
        <entry accept="file" fs-title="pClock">
         <variable>COUNTDOWN_ACTION</variable>
         <input>cat '$WORKDIR'/COUNTDOWN_ACTION</input>
        </entry>
        <button>
         <input file stock="gtk-open"></input>
         <action type="fileselect">COUNTDOWN_ACTION</action>
        </button>
       </hbox>
      </vbox>
     </vbox>
     <vbox>
      <edit editable="false" left-margin="20"><input file>'$WORKDIR'/alarm_txt</input></edit>
      <button height-request="50">
       <label>'$(gettext "Define Alarm")'</label>
       <action>alarm</action>
       <action>echo $ALARM > '$WORKDIR'/ALARM</action>
      </button>
##      <hbox>
##       <text><label>'$(gettext "Audio")'</label></text>
##       <entry accept="file">
##        <variable>ALARM</variable>
##        <input>cat '$WORKDIR'/ALARM</input>
##       </entry>
##       <button>
##        <input file stock="gtk-open"></input>
##        <action type="fileselect">ALARM</action>
##       </button>
##      </hbox>
     </vbox>
     <input file>'$WORKDIR'/pclock-tools_tab</input>
     <variable>NOTEBOOK_TOOLS</variable>
    </notebook>
   </vbox>
   <input file>'$WORKDIR'/pclock-tab</input>
   <variable>NOTEBOOK</variable>
  </notebook>
 </vbox>
 <action signal="hide">echo end > '$WORKDIR'/end_while</action>
 <action signal="hide">exit:EXIT</action>
</window>'

echo "$pClock" | sed 's/##.*//' > $WORKDIR/pclock_xml #I use double hash (##) for comments. --> as #FF0000 is valid xml-code

gtkdialog -f $WORKDIR/pclock_xml
echo end > $WORKDIR/end_while
