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

export TEXTDOMAIN=pupclockset
export OUTPUT_CHARSET=UTF-8

WORKDIR="/usr/local/PupClockset"

#define gtkdialog
if [ "`which gtkdialog4`" ]; then
 GTKDIALOG=gtkdialog4
elif [ "`which gtkdialog3`" ]; then
 GTKDIALOG=gtkdialog3
else
 GTKDIALOG=gtkdialog
fi

#auto-adjust header text color for light or dark GTK themes
if [ "`grep 'Stardust_dark' $HOME/.gtkrc-2.0 2>/dev/null`" ]; then #gold text for unique Stardust dark-grey-orange themes
  COLOR=#D7B740
else #light-blue text for common dark themes, otherwise medium blue
  [ "`grep -Ei 'black|dark|night' $HOME/.gtkrc-2.0 2>/dev/null`" ] && COLOR=#84aad9 || COLOR=#3272C0
fi
export COLOR

#define special Sans font for PupClockset GUI
echo "style \"specialframe\"
{
  font_name=\"Sans bold 10\"
#  fg[NORMAL]=\"grey40\"
}
widget \"*.GtkFrame.GtkLabel\" style \"specialframe\"
class \"*.GtkFrame.GtkLabel\" style \"specialframe\"" > "$WORKDIR/gtkrc_special"

export GTK2_RC_FILES="$WORKDIR/gtkrc_special:${HOME}/.gtkrc-2.0"

#define title-bar icon
ln -snf $WORKDIR/icons/clockformat48.png /usr/share/icons/hicolor/48x48/apps && gtk-update-icon-cache -f -i /usr/share/icons/hicolor 2>/dev/null

#define current global screen dpi
CURRDPI=`cat $HOME/.Xresources | grep "^Xft\\.dpi:" | tr '\t' ' ' | tr -s ' ' | cut -f 2 -d ' '`
[ "$CURRDPI" = "" ] && CURRDPI="96"

#define default configuration path for Tint2 panel
TINT2="$HOME/.config/tint2/tint2rc"

#if no Tint2 clock configuration file, then build file
[ ! -s $WORKDIR/tint2clock-variables.rc ] && echo -e -n 'CLOCK_BACKGROUND_ID="0"\nCLOCK_PADDINGX="4"\nCLOCK_PADDINGY="0"\nCLOCK_TOOLTIP="25 Dec 2000"\nLEFTCLICK="PupClockset"\nRIGHTCLICK="osmo"\nTIME1_FONTNAME="DejaVuSans"\nTIME1_FONTSIZE="12"\nTIME1_FONTSTYLE="normal"\nTIME1_FONTWEIGHT="normal"\nTIME1_FORMAT="15:30"\nTIME2_FONTNAME="DejaVuSans"\nTIME2_FONTSIZE="8"\nTIME2_FORMAT="Hide Lower Clock"\nTIMEZONE1=""\nTIMEZONE2=""\nTINT2_COLORBUTTON="#eeeeee"\nTINT2_FONTCOLOR="#eeeeee"\nTINT2_FONTOPACITY="75"\n' > $WORKDIR/tint2clock-variables.rc
. $WORKDIR/tint2clock-variables.rc

#if no tint2clock-variables2.rc, then create file
if [ ! -f $WORKDIR/tint2clock-variables2.rc ]; then cp -f $WORKDIR/tint2clock-variables.rc $WORKDIR/tint2clock-variables2.rc & fi

#if no tint2rc backup, then create file
if [ ! -f $TINT2-bak ]; then cp -f $TINT2 $TINT2-bak & fi

#if tint2rc is null, then restore from tint2rc-bak
if [ "`grep clock $TINT2`" = "" ]; then cp -f $TINT2-bak $TINT2 & fi

#define button width-request for GUI tray
if  	[ $CURRDPI = "150" ]; then TRAY_BUTTON_WIDTH=$"90"
elif	[ $CURRDPI = "144" ]; then TRAY_BUTTON_WIDTH=$"88"
elif	[ $CURRDPI = "138" ]; then TRAY_BUTTON_WIDTH=$"86"
elif	[ $CURRDPI = "132" ]; then TRAY_BUTTON_WIDTH=$"84"
elif	[ $CURRDPI = "126" ]; then TRAY_BUTTON_WIDTH=$"82"
elif	[ $CURRDPI = "120" ]; then TRAY_BUTTON_WIDTH=$"78"
elif	[ $CURRDPI = "114" ]; then TRAY_BUTTON_WIDTH=$"76"
elif	[ $CURRDPI = "108" ]; then TRAY_BUTTON_WIDTH=$"74"
elif	[ $CURRDPI = "102" ]; then TRAY_BUTTON_WIDTH=$"70"
elif	[ $CURRDPI = "96" ]; then TRAY_BUTTON_WIDTH=$"68"
elif	[ $CURRDPI = "90" ]; then TRAY_BUTTON_WIDTH=$"66"
elif	[ $CURRDPI = "84" ]; then TRAY_BUTTON_WIDTH=$"64"
elif	[ $CURRDPI = "78" ]; then TRAY_BUTTON_WIDTH=$"62"
elif	[ $CURRDPI = "72" ]; then TRAY_BUTTON_WIDTH=$"60"
fi

#define combobox width-request
if  	[ $CURRDPI = "108" ]; then COMBOBOX_WIDTH=$"315"
elif	[ $CURRDPI = "102" ]; then COMBOBOX_WIDTH=$"305"
elif	[ $CURRDPI = "96" ]; then COMBOBOX_WIDTH=$"285"
elif	[ $CURRDPI = "90" ]; then COMBOBOX_WIDTH=$"280"
elif	[ $CURRDPI = "84" ]; then COMBOBOX_WIDTH=$"265"
elif	[ $CURRDPI = "78" ]; then COMBOBOX_WIDTH=$"245"
elif	[ $CURRDPI = "72" ]; then COMBOBOX_WIDTH=$"240"
fi

#define current font name and color
. $WORKDIR/tint2_func -time1_fontname
. $WORKDIR/tint2_func -time2_fontname
. $WORKDIR/tint2_func -tint2_fontcolor

#define timezone information
. $WORKDIR/tint2_func -zoneinfo

#define $TIME1_FORMAT_ITEMS for time1_format combobox
TIME1_FORMAT_ITEMS="<item>$TIME1_FORMAT</item>"
for I in "Hide Both Clocks" "03:30 PM" "03:30:00 PM" "Sun 03:30 PM" "Sun 25 Dec 03:30 PM" "15:30" "15:30:00" "Sun 15:30" "Sun 25 Dec 15:30" "Sun 25"; do TIME1_FORMAT_ITEMS=`echo "$TIME1_FORMAT_ITEMS<item>$I</item>"`; done

#define $TIME1_FONTNAME_ITEMS for time1_fontname combobox
TIME1_FONTNAME_ITEMS=`cat /tmp/PCS-time1fontlist.txt`

#define $TIME1_FONTSIZE_ITEMS for time1_fontsize combobox
TIME1_FONTSIZE_ITEMS="<item>$TIME1_FONTSIZE</item>"
for I in 6 7 8 9 10 11 12 13 14 15 16 17 18 20 22 24; do TIME1_FONTSIZE_ITEMS=`echo "$TIME1_FONTSIZE_ITEMS<item>$I</item>"`; done

#define $TIME1_FONTWEIGHT_ITEMS for time1_fontweight combobox
TIME1_FONTWEIGHT_ITEMS="<item>$(gettext 'normal')</item> <item>$(gettext 'bold')</item>"

#define $TIME1_FONTSTYLE_ITEMS for time1_fontstyle combobox
TIME1_FONTSTYLE_ITEMS="<item>$(gettext 'normal')</item> <item>$(gettext 'italic')</item>"

#define $TIME2_FORMAT_ITEMS for time2_format combobox
TIME2_FORMAT_ITEMS="<item>$TIME2_FORMAT</item>"
for I in "Hide Lower Clock" "12-hr clock" "24-hr clock" "25 Dec 2000" "25 December 2000" "Dec 25 2000" "December 25 2000" "Sun 25 December" "Sunday 25 December" "Sun 25"; do TIME2_FORMAT_ITEMS=`echo "$TIME2_FORMAT_ITEMS<item>$I</item>"`; done

#define $TIME2_FONTNAME_ITEMS for time2_fontname combobox
TIME2_FONTNAME_ITEMS=`cat /tmp/PCS-time2fontlist.txt`

#define $TIME2_FONTSIZE_ITEMS for time2_fontsize combobox
TIME2_FONTSIZE_ITEMS="<item>$TIME2_FONTSIZE</item>"
for I in 6 7 8 9 10 11 12 13 14 15 16 17 18 20 22 24; do TIME2_FONTSIZE_ITEMS=`echo "$TIME2_FONTSIZE_ITEMS<item>$I</item>"`; done

#define $TINT2_FONTOPACITY_ITEMS for tint2_fontopacity combobox
TINT2_FONTOPACITY_ITEMS="<item>$TINT2_FONTOPACITY</item>"
for I in 100 95 90 85 80 75 70 65 60 55 50 45 40 35 30 25 20 15 10 5 0; do TINT2_FONTOPACITY_ITEMS=`echo "$TINT2_FONTOPACITY_ITEMS<item>$I</item>"`; done

#define $CLOCK_TOOLTIP_ITEMS for clock_tooltip combobox
CLOCK_TOOLTIP_ITEMS="<item>$CLOCK_TOOLTIP</item>"
for I in "Hide Tooltip" "25 Dec 2000" "25 December 2000" "Sun 25 Dec" "Sun 25 Dec 2000" "Sun 25 Dec 2000  15:30" "Sunday 25 December" "Sunday 25 December 2000" "Sunday 25 Dec 2000  15:30"; do CLOCK_TOOLTIP_ITEMS=`echo "$CLOCK_TOOLTIP_ITEMS<item>$I</item>"`; done

#define $CLOCK_PADDINGX_ITEMS for clock_padding_x combobox
CLOCK_PADDINGX_ITEMS="<item>$CLOCK_PADDINGX</item>"
for I in 0 1 2 3 4 5 6 7 8 9 10; do CLOCK_PADDINGX_ITEMS=`echo "$CLOCK_PADDINGX_ITEMS<item>$I</item>"`; done

#define $CLOCK_PADDINGY_ITEMS for clock_padding_y combobox
CLOCK_PADDINGY_ITEMS="<item>$CLOCK_PADDINGY</item>"
for I in 0 1 2 3 4 5 6 7 8 9 10; do CLOCK_PADDINGY_ITEMS=`echo "$CLOCK_PADDINGY_ITEMS<item>$I</item>"`; done

#define $CLOCK_BACKGROUND_ID_ITEMS for clock_background_id combobox
CLOCK_BACKGROUND_ID_ITEMS="<item>0</item> <item>1</item> <item>2</item> <item>3</item>"

#define $TIMEZONE1_ITEMS for timezone1 combobox
TIMEZONE1_ITEMS=`cat /tmp/zone1info.tmp`

#define $TIMEZONE2_ITEMS for timezone2 combobox
TIMEZONE2_ITEMS=`cat /tmp/zone2info.tmp`

#define entry input for Tint2 clock settings
LEFTCLICK=`cat $WORKDIR/tint2clock-variables2.rc | grep -a LEFTCLICK= | cut -d "=" -f 2  | sed 's/\"//g'`
RIGHTCLICK=`cat $WORKDIR/tint2clock-variables2.rc | grep -a RIGHTCLICK= | cut -d "=" -f 2  | sed 's/\"//g'`
TINT2_FONTOPACITY=`cat $WORKDIR/tint2clock-variables2.rc | grep -a TINT2_FONTOPACITY= | cut -d "=" -f 2  | sed 's/\"//g'`

#define color selector for Tint2 clock font
	COLORSEL="
	<entry tooltip-text=\" $(gettext 'Click the color selector button') \">
	<input>cat /tmp/tint2clockcolor.tmp</input>
	<variable>TINT2_FONTCOLOR</variable>
	</entry>

	<colorbutton title=\"$(gettext 'PupClockset Color Selector')\" tooltip-text=\" $(gettext 'Color selector') \">
	<input file>/tmp/tint2clockcolor.tmp</input>
	<variable>TINT2_COLORBUTTON</variable>
	<action>echo "'"$TINT2_COLORBUTTON"'" > /tmp/tint2clockcolor.tmp</action>
	<action>refresh:TINT2_FONTCOLOR</action>
	</colorbutton>"

#Main Dialog
export TINT2CLOCKFORMAT_DIALOG="<window title=\"PCS\" icon-name=\"clockformat48\" resizable=\"false\" window-position=\"1\">
 <vbox>
  <text use-markup=\"true\"><label>\"<b><span size='"'large'"'>Pup</span><span size='"'large'"' color='"$COLOR"'>Clockset</span></b>\"</label></text>
  <notebook page=\"0\" labels=\" $(gettext 'Tint2 Clock Display | Tint2 Clock Settings | Help') \">
  <vbox>
  <vbox>
  <frame $(gettext 'Clock 1')>
   <hbox>
    <text><label>$(gettext 'Time Format:')</label></text>
    <comboboxentry wrap-width=\"1\" width-request=\""$COMBOBOX_WIDTH"\" tooltip-text=\" $(gettext 'Time/Date format of upper clock') \">
     <variable>TIME1_FORMAT</variable>
     <default>$TIME1_FORMAT</default>
     $TIME1_FORMAT_ITEMS
    </comboboxentry>
   </hbox>

   <hbox>
    <text><label>$(gettext 'Font Name:')</label></text>
    <comboboxentry wrap-width=\"1\" width-request=\""$COMBOBOX_WIDTH"\" tooltip-text=\" $(gettext 'Font of upper clock') \">
     <variable>TIME1_FONTNAME</variable>
     <default>$TIME1_FONTNAME</default>
     $TIME1_FONTNAME_ITEMS
    </comboboxentry>
   </hbox>

   <hbox>
    <text><label>$(gettext 'Font Size:')</label></text>
    <comboboxentry wrap-width=\"1\" width-request=\""$COMBOBOX_WIDTH"\" tooltip-text=\" $(gettext 'Font size of upper clock') \">
     <variable>TIME1_FONTSIZE</variable>
     <default>$TIME1_FONTSIZE</default>
     $TIME1_FONTSIZE_ITEMS
    </comboboxentry>
   </hbox>

   <hbox>
    <text><label>$(gettext 'Font Weight:')</label></text>
    <comboboxentry wrap-width=\"1\" width-request=\""$COMBOBOX_WIDTH"\" tooltip-text=\" $(gettext 'Font weight of upper clock') \">
     <variable>TIME1_FONTWEIGHT</variable>
     <default>$TIME1_FONTWEIGHT</default>
     $TIME1_FONTWEIGHT_ITEMS
    </comboboxentry>
   </hbox>

   <hbox>
    <text><label>$(gettext 'Font Style:')</label></text>
    <comboboxentry wrap-width=\"1\" width-request=\""$COMBOBOX_WIDTH"\" tooltip-text=\" $(gettext 'Font style of upper clock') \">
     <variable>TIME1_FONTSTYLE</variable>
     <default>$TIME1_FONTSTYLE</default>
     $TIME1_FONTSTYLE_ITEMS
    </comboboxentry>
   </hbox>
  </frame>

  <frame $(gettext 'Clock 2')>
   <hbox>
    <text><label>$(gettext 'Time Format:')</label></text>
    <comboboxentry wrap-width=\"1\" width-request=\""$COMBOBOX_WIDTH"\" tooltip-text=\" $(gettext 'Time/Date format of lower clock') \">
     <variable>TIME2_FORMAT</variable>
     <default>$TIME2_FORMAT</default>
     $TIME2_FORMAT_ITEMS
    </comboboxentry>
   </hbox>

   <hbox>
    <text><label>$(gettext 'Font Name:')</label></text>
    <comboboxentry wrap-width=\"1\" width-request=\""$COMBOBOX_WIDTH"\" tooltip-text=\" $(gettext 'Font of lower clock') \">
     <variable>TIME2_FONTNAME</variable>
     <default>$TIME2_FONTNAME</default>
     $TIME2_FONTNAME_ITEMS
    </comboboxentry>
   </hbox>

   <hbox>
    <text><label>$(gettext 'Font Size:')</label></text>
    <comboboxentry wrap-width=\"1\" width-request=\""$COMBOBOX_WIDTH"\" tooltip-text=\" $(gettext 'Font size of lower clock') \">
     <variable>TIME2_FONTSIZE</variable>
     <default>$TIME2_FONTSIZE</default>
     $TIME2_FONTSIZE_ITEMS
    </comboboxentry>
   </hbox>
  </frame>

  <frame $(gettext 'Font Color & Opacity')>
   <hbox>
    $COLORSEL

    <comboboxentry wrap-width=\"1\" width-request=\"75\" tooltip-text=\" $(gettext 'Font opacity') \">
     <variable>TINT2_FONTOPACITY</variable>
     <default>$TINT2_FONTOPACITY</default>
     $TINT2_FONTOPACITY_ITEMS
    </comboboxentry>
   </hbox>
  </frame>
  </vbox>
  </vbox>


  <vbox>
  <frame $(gettext 'Settings')>
  <vbox>
   <hbox>
    <text><label>$(gettext 'Date Tooltip:')</label></text>
    <comboboxentry wrap-width=\"1\" width-request=\""$COMBOBOX_WIDTH"\" tooltip-text=\" $(gettext 'Date format of clock tooltip 
 (see the Tint2 Wiki for help)') \">
     <variable>CLOCK_TOOLTIP</variable>
     <default>$CLOCK_TOOLTIP</default>
     $CLOCK_TOOLTIP_ITEMS
    </comboboxentry>
   </hbox>

   <hbox>
    <text><label>$(gettext 'Padding (x):')</label></text>
    <comboboxentry wrap-width=\"1\" width-request=\""$COMBOBOX_WIDTH"\" tooltip-text=\" $(gettext 'Pad free space of right and left clock borders') \">
     <variable>CLOCK_PADDINGX</variable>
     <default>$CLOCK_PADDINGX</default>
     $CLOCK_PADDINGX_ITEMS
    </comboboxentry>
   </hbox>

   <hbox>
    <text><label>$(gettext 'Padding (y):')</label></text>
    <comboboxentry wrap-width=\"1\" width-request=\""$COMBOBOX_WIDTH"\" tooltip-text=\" $(gettext 'Pad free space of upper and lower clock borders') \">
     <variable>CLOCK_PADDINGY</variable>
     <default>$CLOCK_PADDINGY</default>
     $CLOCK_PADDINGY_ITEMS
    </comboboxentry>
   </hbox>

   <hbox>
    <text><label>$(gettext 'Background:')</label></text>
    <comboboxentry wrap-width=\"1\" width-request=\""$COMBOBOX_WIDTH"\" tooltip-text=\" $(gettext 'The clock_background_id values are unique to 
 the tint2rc file of the active Tint2 panel theme.') \">
     <variable>CLOCK_BACKGROUND_ID</variable>
     <default>$CLOCK_BACKGROUND_ID</default>
     $CLOCK_BACKGROUND_ID_ITEMS
    </comboboxentry>
   </hbox>

   <hbox>
    <text><label>$(gettext 'TZ (Clock 1):')</label></text>
    <comboboxentry width-request=\""$COMBOBOX_WIDTH"\" tooltip-text=\" $(gettext 'Select timezone for Clock 1') \">
     <variable>TIMEZONE1</variable>
     <default>"'"'$TIMEZONE1'"'"</default>
     $TIMEZONE1_ITEMS
    </comboboxentry>
   </hbox>

   <hbox>
    <text><label>$(gettext 'TZ (Clock 2):')</label></text>
    <comboboxentry width-request=\""$COMBOBOX_WIDTH"\" tooltip-text=\" $(gettext 'Select timezone for Clock 2') \">
     <variable>TIMEZONE2</variable>
     <default>"'"'$TIMEZONE2'"'"</default>
     $TIMEZONE2_ITEMS
    </comboboxentry>
   </hbox>

   <hbox>
    <text><label>$(gettext 'Lt-Click Cmd:')</label></text>
    <entry tooltip-text=\" $(gettext 'Left-click the clock to launch this program') \">
     <variable>LEFTCLICK</variable>
     <input>echo "$LEFTCLICK"</input>
    </entry>
   </hbox>

   <hbox>
    <text><label>$(gettext 'Rt-Click Cmd:')</label></text>
    <entry tooltip-text=\" $(gettext 'Right-click the clock to launch this program') \">
     <variable>RIGHTCLICK</variable>
     <input>echo "$RIGHTCLICK"</input>
    </entry>
   </hbox>
    <text><label>\"\"</label></text>
   <hbox homogeneous=\"true\">
    <button tooltip-text=\" $(gettext 'Open strftime manual in default browser') \">
       <action>defaultbrowser http://www.kernel.org/doc/man-pages/online/pages/man3/strftime.3.html &</action>
     <label>\"  strftime  \"</label>
    </button>
    <button tooltip-text=\" $(gettext 'Open Tint2 Wiki in default browser') \">
     <label>\"     $(gettext 'Wiki')     \"</label>
       <action>defaultbrowser http://code.google.com/p/tint2/wiki/Configure &</action>
    </button>
   </hbox>
  </vbox>
  </frame>
  </vbox>


   <vbox>
    <frame $(gettext 'Ver. 2.3.1')>
     <text><label>\"


  $(gettext 'A digital-tray clock will display the 
  time and date in multiple formats 
  based on standard, locale-specific 
  strftime conversion specifications. 
  PupClockset enables quick access 
  to eight common displays of time 
  and date in 12 and 24 hr formats. 
  See the help file for strftime data.

       Roger D. Grider, 2015GPL3')\"</label></text>
  <text><label>\"\"</label></text>
  <hbox homogeneous=\"true\">
    <button tooltip-text=\" $(gettext 'Open help file in default browser') \">
     <label>\" $(gettext 'Help') \"</label>
       <action>defaultbrowser /usr/local/PupClockset/Help.html &</action>
    </button>
  </hbox>
    </frame>
   </vbox>
  </notebook>

  <hbox homogeneous=\"true\">
    <button width-request=\""$TRAY_BUTTON_WIDTH"\" tooltip-text=\" $(gettext 'Apply selected format') \">
     <label>\"$(gettext 'Set')\"</label>
      <input file stock=\"gtk-ok\"></input>
       <action>. $WORKDIR/tint2_func -tint2clockset &</action>
       <action type=\"exit\">quit_now</action>
    </button>

    <button width-request=\""$TRAY_BUTTON_WIDTH"\" tooltip-text=\" $(gettext 'Calendar and Organizer') \">
     <label>\"Cal\"</label>
       <action>[ "`which osmo`" ] && killall osmo 2>/dev/null ; osmo 2>/dev/null &</action>
       <action>[ ! "`which osmo`" ] && [ "`which orage`" ] && orage &</action>
    </button>

    <button width-request=\""$TRAY_BUTTON_WIDTH"\" tooltip-text=\" $(gettext 'Set time and date') \">
     <label>\"$(gettext 'Time')\"</label>
       <action>set-time-for-puppy &</action>
    </button>

    <button width-request=\""$TRAY_BUTTON_WIDTH"\" tooltip-text=\" $(gettext 'Sync clock with time servers') \">
     <label>\"Sync\"</label>
       <action>. $WORKDIR/func -sync_clock &</action>
    </button>

    <button width-request=\""$TRAY_BUTTON_WIDTH"\" tooltip-text=\" $(gettext 'Close PupClockset') \">
     <label>\"$(gettext 'Exit')\"</label>
      <input file stock=\"gtk-close\"></input>
       <action>. $WORKDIR/tint2_func -tint2_restore &</action>
       <action type=\"exit\">quit_now</action>
    </button>
  </hbox>
 </vbox>
</window>"

$GTKDIALOG --program=TINT2CLOCKFORMAT_DIALOG > $WORKDIR/tint2clock-variables.rc

unset TINT2CLOCKFORMAT_DIALOG

exit 0
