#!/bin/bash
# set sfwbar clock format
# based on code by 01micko

export TEXTDOMAIN=sfwbar-clockset
export OUTPUT_CHARSET=UTF-8
. gettext.sh

CONFIG=$HOME/.config/sfwbar/clock.widget
ICONDIR=/usr/share/pixmaps/puppy

# set icon-name
[ ! -f /usr/share/icons/hicolor/48x48/apps/clock_digital.svg ] && \
ln -sf $ICONDIR/clock_digital.svg /usr/share/icons/hicolor/48x48/apps && gtk-update-icon-cache -f -i /usr/share/icons/hicolor 2>/dev/null

# find current clock font size
DEFSIZE=$(cat $HOME/.config/sfwbar/clock.widget | grep -o 'font:.*pt' | cut -d ' ' -f2)
[ -z "$DEFSIZE" ] && DEFSIZE=10pt

# find current clock font color
DEFCOLOR=$(cat $HOME/.config/sfwbar/clock.widget | grep -oP 'color:\s*\K[^;]+')
[ "$DEFCOLOR" = "dimgray" ] && DEFCOLOR=darkgray
[ -z "$DEFCOLOR" ] && DEFCOLOR=black

# find current clock time
CURCLOCK=$(grep 'value' $CONFIG | cut -d '"' -f2- | cut -d '"' -f1)
H24=`echo $CURCLOCK|grep -E 'H|R|T'` #20 Oct 2015
[ "$H24" ] && HC24=true VIS1=disabled || HC24=false VIS1=enabled
I24=`echo $CURCLOCK|grep -E 'I|l|r'` #20 Oct 2015
[ "$I24" ] && IC24=true || IC24=false
S24=`echo $CURCLOCK|grep S`
[ "$S24" ] && SC24=true || SC24=false
p24=`echo $CURCLOCK|grep p`
[ "$p24" ] && pC24=true || pC24=false
a24=`echo $CURCLOCK|grep a` #30 Mar 2021
[ "$a24" ] && aC24=true || aC24=false
d24=`echo $CURCLOCK|grep d` #30 Mar 2021
[ "$d24" ] && dC24=true VIS2=enabled || dC24=false VIS2=disabled
y24=`echo $CURCLOCK|grep Y` #30 Mar 2021
[ "$y24" ] && yC24=true || yC24=false

# apply new clock font size
new_size_func(){
	if [ "$DEFSIZE" != "$NEWSIZE" ]; then
		sed -i "s/font:.*pt/font: $NEWSIZE/" $CONFIG
	fi
}

# apply new clock font color
new_color_func(){
	if [ "$DEFCOLOR" != "$NEWCOLOR" ]; then
		[ "$NEWCOLOR" = "darkgray" ] && NEWCOLOR=dimgray
		sed -i "s/color:.*;/color: $NEWCOLOR;/" $CONFIG
	fi
}

# apply new clock time
new_time_func(){
	
	[ "$r0" = "true" ] && NCLOCK=%H:%M
	[ "$r1" = "true" ] && NCLOCK=%l:%M
	[ "$c0" = "true" ] && AMPM=:%p
	[ "$c1" = "true" ] && SEC=:%S
	[ "$d0" = "true" ] && DAY="%a "
	[ "$d1" = "true" ] && DATE="%d %b "
	[ "$y0" = "true" ] && YEAR="%Y "
	NEWCLOCK=${DAY}${DATE}${YEAR}${NCLOCK}${SEC}${AMPM}
	sed -i "s|$CURCLOCK|$NEWCLOCK|" $CONFIG
	new_size_func
	new_color_func
}

export new_size_func new_color_func new_time_func

# set buttons for external applications
[ "`which osmo 2>/dev/null`" ] && calendar="osmo --calendar" || calendar=minixcal

[ "`which set-time-for-puppy 2>/dev/null`" ] && timeset='
		<button use-underline="true" tooltip-text=" '"$(gettext 'Set date and time')"' ">
			<input file>'$ICONDIR'/date_time.svg</input><height>24</height><width>24</width>
			<action>set-time-for-puppy &</action>
		</button>'

[ "`which timezone-set 2>/dev/null`" ] && timezone='
		<button use-underline="true" tooltip-text=" '"$(gettext 'Set timezone')"' ">
			<input file>'$ICONDIR'/country_timezone.svg</input><height>24</height><width>24</width>
			<action>timezone-set &</action>
		</button>'

[ "`which psync 2>/dev/null`" ] && psync_cmd=psync
[ "$psync_cmd" ] && timesync='
		<button use-underline="true" tooltip-text=" '"$(gettext 'Sync with time server')"' ">
			<input file>'$ICONDIR'/clock_countdown.svg</input><height>24</height><width>24</width>
			<action>'${psync_cmd}' &</action>
		</button>'

MSG_1="<b><span size='"'x-large'"'>$(gettext "SFWbar Clock")</span></b>"
MSG_2="<b>$(gettext "Set clock format")</b>"

# main dialog
export GUI='<window title="'"$(gettext 'Taskbar Clock')"'" icon-name="clock_digital" window-position="1" resizable="false" decorated="true">
<hbox>
	<vbox>
	'"`/usr/lib/gtkdialog/xml_info fixed clock_digital.svg 60 "$MSG_1" "$MSG_2"`"'
	<hbox>
		<vbox width-request="126">
		<text space-expand="true" space-fill="false"><label>" "</label></text>
		<text space-expand="true" space-fill="false"><label>" "</label></text>
		<hbox homogeneous="true">
		<vbox>
			<radiobutton>
				<variable>r0</variable>
				<default>'$HC24'</default>
				<label>'$(gettext "24 h")'</label>
				<action>if true disable:c0</action>
				<action>if true clear:c0</action>
			</radiobutton>
			<radiobutton>
				<variable>r1</variable>
				<default>'$IC24'</default>
				<label>'$(gettext "12 h")'</label>
				<action>if true enable:c0</action>
			</radiobutton>
		</vbox>
		</hbox>
		<text space-expand="true" space-fill="false"><label>" "</label></text>
		<text space-expand="true" space-fill="false"><label>" "</label></text>
		</vbox>

		<vbox margin="5" width-request="126">
			<checkbox>
				<variable>c0</variable>
				<default>'$pC24'</default>
				<label>'$(gettext "am / pm")'</label>
				<visible>'$VIS1'</visible>
			</checkbox>
			<checkbox>
				<variable>c1</variable>
				<default>'$SC24'</default>
				<label>'$(gettext "seconds")'</label>
			</checkbox>
			<checkbox>
				<variable>d0</variable>
				<default>'$aC24'</default>
				<label>'$(gettext "day")'</label>
			</checkbox>
			<checkbox>
				<variable>d1</variable>
				<default>'$dC24'</default>
				<label>'$(gettext "date")'</label>
				<action>if true enable:y0</action>
				<action>if false disable:y0</action>
				<action>if false clear:y0</action>
			</checkbox>
			<checkbox>
				<variable>y0</variable>
				<default>'$yC24'</default>
				<label>'$(gettext "year")'</label>
				<visible>'$VIS2'</visible>
			</checkbox>
		</vbox>
	</hbox>

	<hseparator space-expand="false" space-fill="false"></hseparator>

	<hbox space-expand="true" space-fill="true" homogeneous="true">
		<comboboxtext tooltip-text=" '"$(gettext 'Clock font size')"' ">
			<default>'$DEFSIZE'</default>
			<item>10pt</item>
			<item>11pt</item>
			<item>12pt</item>
			<item>13pt</item>
			<item>14pt</item>
			<variable>NEWSIZE</variable>
		</comboboxtext>
		<comboboxtext tooltip-text=" '"$(gettext 'Clock font color')"' ">
			<default>'$DEFCOLOR'</default>
			<item>black</item>
			<item>darkgray</item>
			<item>gray</item>
			<item>lightgray</item>
			<item>white</item>
			<variable>NEWCOLOR</variable>
		</comboboxtext>
		<button use-underline="true" tooltip-text=" '"$(gettext 'Set clock format')"' ">
			<label>'$(gettext "_Set")'</label>
			<input file>'$ICONDIR'/apply.svg</input><height>24</height><width>24</width>
			<action>exit:OK</action>
		</button>
	</hbox>
	<hbox space-expand="true" space-fill="true">
		<button has-focus="true" use-underline="true" tooltip-text=" '"$(gettext 'Calendar')"' ">
			<input file>'$ICONDIR'/calendar.svg</input><height>25</height><width>25</width>
			<action>'${calendar}' &</action>
		</button>
		'$timeset'
		'$timezone'
		'$timesync'
	</hbox>
	</vbox>
</hbox>
<action signal="key-press-event" condition="command_is_true([ $KEY_SYM = Escape ] && echo true )">exit:EXIT</action>
</window>'
eval `gtkdialog -p GUI --styles=/tmp/gtkrc_xml_info.css`
case $EXIT in
OK|Ok) new_time_func
killall -SIGHUP sfwbar &
exit ;;
*)exit ;;
esac
