#!/bin/bash
#PupClockset 2.3.1
#Tint2Clockset module
#Copyright GPL3, http://www.gnu.org/licenses/
#Roger Grider (radky)

WORKDIR="/usr/local/PupClockset"

TIME1_FORMAT=`grep -a TIME1_FORMAT= $WORKDIR/tint2clock-variables.rc | cut -d "=" -f 2 | sed 's/\"//g'`
TIME2_FORMAT=`grep -a TIME2_FORMAT= $WORKDIR/tint2clock-variables.rc | cut -d "=" -f 2 | sed 's/\"//g'`
TIME1_FONTNAME=`grep -a TIME1_FONTNAME= $WORKDIR/tint2clock-variables.rc | cut -d "=" -f 2 | sed 's/\"//g'`
TIME2_FONTNAME=`grep -a TIME2_FONTNAME= $WORKDIR/tint2clock-variables.rc | cut -d "=" -f 2 | sed 's/\"//g'`
TIME1_FONTSIZE=`grep -a TIME1_FONTSIZE= $WORKDIR/tint2clock-variables.rc | cut -d "=" -f 2 | sed 's/\"//g'`
TIME2_FONTSIZE=`grep -a TIME2_FONTSIZE= $WORKDIR/tint2clock-variables.rc | cut -d "=" -f 2 | sed 's/\"//g'`
TIME1_FONTWEIGHT=`grep -a TIME1_FONTWEIGHT= $WORKDIR/tint2clock-variables.rc | cut -d "=" -f 2 | sed 's/\"//g'`
TIME1_FONTSTYLE=`grep -a TIME1_FONTSTYLE= $WORKDIR/tint2clock-variables.rc | cut -d "=" -f 2 | sed 's/\"//g'`
TINT2_FONTCOLOR=`grep -a TINT2_FONTCOLOR= $WORKDIR/tint2clock-variables.rc | cut -d "=" -f 2 | sed 's/\"//g'`
TINT2_FONTOPACITY=`grep -a TINT2_FONTOPACITY= $WORKDIR/tint2clock-variables.rc | cut -d "=" -f 2 | sed 's/\"//g'`

CLOCK_TOOLTIP=`grep -a CLOCK_TOOLTIP= $WORKDIR/tint2clock-variables.rc | cut -d "=" -f 2 | sed 's/\"//g'`
CLOCK_PADDINGX=`grep -a CLOCK_PADDINGX= $WORKDIR/tint2clock-variables.rc | cut -d "=" -f 2 | sed 's/\"//g'`
CLOCK_PADDINGY=`grep -a CLOCK_PADDINGY= $WORKDIR/tint2clock-variables.rc | cut -d "=" -f 2 | sed 's/\"//g'`
CLOCK_BACKGROUND_ID=`grep -a CLOCK_BACKGROUND_ID= $WORKDIR/tint2clock-variables.rc | cut -d "=" -f 2 | sed 's/\"//g'`
LEFTCLICK=`grep -a LEFTCLICK= $WORKDIR/tint2clock-variables.rc | cut -d "=" -f 2 | sed 's/\"//g'`
RIGHTCLICK=`grep -a RIGHTCLICK= $WORKDIR/tint2clock-variables.rc | cut -d "=" -f 2 | sed 's/\"//g'`
TIMEZONE1=`grep -a TIMEZONE1= $WORKDIR/tint2clock-variables.rc | cut -d "=" -f 2 | sed 's/\"//g' | sed 's/Local_Timezone//g'`
TIMEZONE2=`grep -a TIMEZONE2= $WORKDIR/tint2clock-variables.rc | cut -d "=" -f 2 | sed 's/\"//g' | sed 's/Local_Timezone//g'`
TZ1_NAME=`grep -a TIMEZONE1= $WORKDIR/tint2clock-variables.rc | cut -d "=" -f 2 | sed -e 's/\://g' -e 's/\"//g' -e 's/Local_Timezone//g'`
TZ2_NAME=`grep -a TIMEZONE2= $WORKDIR/tint2clock-variables.rc | cut -d "=" -f 2 | sed -e 's/\://g' -e 's/\"//g' -e 's/Local_Timezone//g'`

if   [ "$TIME1_FORMAT" = "Hide Both Clocks" ]; then T1_FORMAT=""""
elif [ "$TIME1_FORMAT" = "03:30 PM" ]; then T1_FORMAT="%l:%M %p  $TZ1_NAME"
elif [ "$TIME1_FORMAT" = "03:30:00 PM" ]; then T1_FORMAT="%I:%M:%S %p  $TZ1_NAME"
elif [ "$TIME1_FORMAT" = "Sun 03:30 PM" ]; then T1_FORMAT="%a %l:%M %p  $TZ1_NAME"
elif [ "$TIME1_FORMAT" = "Sun 25 Dec 03:30 PM" ]; then T1_FORMAT="%a %d %b %l:%M %p  $TZ1_NAME"
elif [ "$TIME1_FORMAT" = "15:30" ]; then T1_FORMAT="%H:%M  $TZ1_NAME"
elif [ "$TIME1_FORMAT" = "15:30:00" ]; then T1_FORMAT="%H:%M:%S  $TZ1_NAME"
elif [ "$TIME1_FORMAT" = "Sun 15:30" ]; then T1_FORMAT="%a %H:%M  $TZ1_NAME"
elif [ "$TIME1_FORMAT" = "Sun 25 Dec 15:30" ]; then T1_FORMAT="%a %d %b %H:%M  $TZ1_NAME"
elif [ "$TIME1_FORMAT" = "Sun 25" ]; then T1_FORMAT="%a %d"
fi

if   [ "$TIME2_FORMAT" = "Hide Lower Clock" ]; then T2_FORMAT=""""
elif [ "$TIME2_FORMAT" = "12-hr clock" ]; then T2_FORMAT="%l:%M %p  $TZ2_NAME"
elif [ "$TIME2_FORMAT" = "24-hr clock" ]; then T2_FORMAT="%H:%M  $TZ2_NAME"
elif [ "$TIME2_FORMAT" = "25 Dec" ]; then T2_FORMAT="%d %b  $TZ2_NAME"
elif [ "$TIME2_FORMAT" = "25 Dec 2000" ]; then T2_FORMAT="%d %b %Y  $TZ2_NAME"
elif [ "$TIME2_FORMAT" = "25 December 2000" ]; then T2_FORMAT="%d %B %Y  $TZ2_NAME"
elif [ "$TIME2_FORMAT" = "Dec 25" ]; then T2_FORMAT="%b %d  $TZ2_NAME"
elif [ "$TIME2_FORMAT" = "Dec 25 2000" ]; then T2_FORMAT="%b %d %Y  $TZ2_NAME"
elif [ "$TIME2_FORMAT" = "December 25 2000" ]; then T2_FORMAT="%B %d %Y  $TZ2_NAME"
elif [ "$TIME2_FORMAT" = "Sun 25 December" ]; then T2_FORMAT="%a %d %B  $TZ2_NAME"
elif [ "$TIME2_FORMAT" = "Sunday 25 December" ]; then T2_FORMAT="%A %d %B  $TZ2_NAME"
elif [ "$TIME2_FORMAT" = "Sun 25" ]; then T2_FORMAT="%a %d"
fi

if   [ "$CLOCK_TOOLTIP" = "Hide Tooltip" ]; then CLK_TOOLTIP=""""
elif [ "$CLOCK_TOOLTIP" = "25 Dec 2000" ]; then CLK_TOOLTIP="%d %b %Y"
elif [ "$CLOCK_TOOLTIP" = "25 December 2000" ]; then CLK_TOOLTIP="%d %B %Y"
elif [ "$CLOCK_TOOLTIP" = "Sun 25 Dec" ]; then CLK_TOOLTIP="%a %d %b"
elif [ "$CLOCK_TOOLTIP" = "Sun 25 Dec 2000" ]; then CLK_TOOLTIP="%a %d %b %Y"
elif [ "$CLOCK_TOOLTIP" = "Sun 25 Dec 2000  15:30" ]; then CLK_TOOLTIP="%a %d %b %Y  %H:%M"
elif [ "$CLOCK_TOOLTIP" = "Sunday 25 December" ]; then CLK_TOOLTIP="%A %d %B"
elif [ "$CLOCK_TOOLTIP" = "Sunday 25 December 2000" ]; then CLK_TOOLTIP="%A %d %B %Y"
elif [ "$CLOCK_TOOLTIP" = "Sunday 25 Dec 2000  15:30" ]; then CLK_TOOLTIP="%A %d %B %Y  %H:%M"
fi

if [ "`cat $HOME/.config/tint2/tint2rc | grep 'tooltip_font_color'`" = "" ]; then echo -e "\n# Tooltip\ntooltip = 0\ntooltip_padding = 2 2\ntooltip_show_timeout = 0.7\ntooltip_hide_timeout = 0.3\ntooltip_background_id = 1\ntooltip_font_color = #OOOOOO 80\ntooltip_font = sans 10\n" >> $HOME/.config/tint2/tint2rc & fi

case "$1" in

-tint2clockset)
	grep -Eiv 'clock|time' $HOME/.config/tint2/tint2rc > /tmp/tmpfile ; mv -f /tmp/tmpfile $HOME/.config/tint2/tint2rc
	echo -e "# Clock\ntime1_format = "$T1_FORMAT"\ntime1_font = "$TIME1_FONTNAME" "$TIME1_FONTWEIGHT" "$TIME1_FONTSTYLE" "$TIME1_FONTSIZE"\ntime2_format = "$T2_FORMAT"\ntime2_font = "$TIME2_FONTNAME" "$TIME2_FONTSIZE"\nclock_font_color = "$TINT2_FONTCOLOR" "$TINT2_FONTOPACITY"\nclock_tooltip = "$CLK_TOOLTIP"\nclock_padding = "$CLOCK_PADDINGX" "$CLOCK_PADDINGY"\nclock_background_id = "$CLOCK_BACKGROUND_ID"\nclock_lclick_command = "$LEFTCLICK"\nclock_rclick_command = "$RIGHTCLICK"\ntime1_timezone = "$TIMEZONE1"\ntime2_timezone = "$TIMEZONE2"" >> $HOME/.config/tint2/tint2rc
	sed -i 's/^.*tooltip_font_color.*$/tooltip_font_color = '"$TINT2_FONTCOLOR"' '"$TINT2_FONTOPACITY"'/' $HOME/.config/tint2/tint2rc
	killall -SIGUSR1 tint2
	cp -f $WORKDIR/tint2clock-variables.rc $WORKDIR/tint2clock-variables2.rc
	cp -f $WORKDIR/tint2clock-variables.rc $WORKDIR/tint2bak.rc
	$WORKDIR/Tint2Clockset
	;;

-time1_fontname)
	echo "`fc-list : family | sort | uniq`" | awk '{print "<item>"$1" "$2" "$3" "$4"</item>"}' | sed -e 's/ //g;s/\\//g' > /tmp/time1clockfontlist_1.tmp
	grep -a TIME1_FONTNAME= $WORKDIR/tint2clock-variables2.rc | cut -d "=" -f 2 | sed 's/\"//g' | awk '{print "<item>"$1"</item>"}' > /tmp/time1clockfontlist_2.tmp
	awk 'BEGIN{print ""}1' /tmp/time1clockfontlist_1.tmp > /tmp/time1clockfontlist_3.tmp
	sed '1r /tmp/time1clockfontlist_2.tmp' < /tmp/time1clockfontlist_3.tmp | sed '1d' > /tmp/tmpfile ; mv -f /tmp/tmpfile /tmp/PCS-time1fontlist.txt
	rm -f /tmp/time1clockfontlist_*.tmp
	;;

-time2_fontname)
	echo "`fc-list : family | sort | uniq`" | awk '{print "<item>"$1" "$2" "$3" "$4"</item>"}' | sed -e 's/ //g;s/\\//g' > /tmp/time2clockfontlist_1.tmp
	grep -a TIME2_FONTNAME= $WORKDIR/tint2clock-variables2.rc | cut -d "=" -f 2 | sed 's/\"//g' | awk '{print "<item>"$1"</item>"}' > /tmp/time2clockfontlist_2.tmp
	awk 'BEGIN{print ""}1' /tmp/time2clockfontlist_1.tmp > /tmp/time2clockfontlist_3.tmp
	sed '1r /tmp/time2clockfontlist_2.tmp' < /tmp/time2clockfontlist_3.tmp | sed '1d' > /tmp/tmpfile ; mv -f /tmp/tmpfile /tmp/PCS-time2fontlist.txt
	rm -f /tmp/time2clockfontlist_*.tmp
	;;

-tint2_fontcolor)
	[ ! -s /tmp/tint2clockcolor.tmp ] && grep -a TINT2_FONTCOLOR= $WORKDIR/tint2clock-variables.rc | cut -d "=" -f 2  | sed 's/\"//g'  > /tmp/tint2clockcolor.tmp
	;;

-tint2_restore)
	sleep 1
	CURRENTFONTCOLOR=`grep -a TINT2_FONTCOLOR= $WORKDIR/tint2bak.rc | cut -d "=" -f 2  | sed 's/\"//g'`
	echo "$CURRENTFONTCOLOR" > /tmp/tint2clockcolor.tmp
	grep -Ea 'CLOCK|CLICK|TIME|TINT2' $WORKDIR/tint2bak.rc | grep -v 'ALL=' > $WORKDIR/tint2clock-variables.rc
	echo 'EXIT="quit_now"' >> $WORKDIR/tint2clock-variables.rc
	cp -f $WORKDIR/tint2clock-variables.rc $WORKDIR/tint2clock-variables2.rc
	;;

-zoneinfo)
	TZ1=`grep -a TIMEZONE1= $WORKDIR/tint2clock-variables2.rc | cut -d "=" -f 2 | sed 's/\"//g' | awk '{print "<item>"$1"</item>"}'`
	TZ2=`grep -a TIMEZONE2= $WORKDIR/tint2clock-variables2.rc | cut -d "=" -f 2 | sed 's/\"//g' | awk '{print "<item>"$1"</item>"}'`
	TZ_DIR="/usr/share/zoneinfo"
	 ls $TZ_DIR/*/* | cut -d "/" -f 5,6 | sort | sed 's/^/:/' | awk '{print "<item>"$1"</item>"}' > /tmp/zoneinfo.tmp
	 echo "$TZ1" > /tmp/zone1info.tmp
	 [ ! "$TZ1" = "<item>Local_Timezone</item>" ] && echo "<item>Local_Timezone</item>" >> /tmp/zone1info.tmp
	 echo "`cat /tmp/zoneinfo.tmp`" >> /tmp/zone1info.tmp
	 echo "$TZ2" > /tmp/zone2info.tmp
	 [ ! "$TZ2" = "<item>Local_Timezone</item>" ] && echo "<item>Local_Timezone</item>" >> /tmp/zone2info.tmp
	 echo "`cat /tmp/zoneinfo.tmp`" >> /tmp/zone2info.tmp
	;;

esac
