#!/bin/bash
#(c) Copyright Barry Kauler, Nov. 2016. GPL v3 licence (refer: /usr/share/doc/legal).
#set the date and time from the Internet. called from quicksetup, also in menu.
#161104 first version. 
#161105 passed param, run without X. also called from /root/Startup/network_tray (v3.2.1)

export TEXTDOMAIN=qsync
export TEXTDOMAINDIR=/usr/share/locale
export OUTPUT_CHARSET=UTF-8
. gettext.sh

if pidof ntpdate;then exit; fi #precaution

NOX=0
[ ! $DISPLAY ] && NOX=1
PRE_ACTIVE_CHK="true"
if [ $1 ];then
 PRE_ACTIVE_CHK="$1" #true or false.
 NOX=1
fi

. /etc/clock #has HWCLOCKTIME=localtime or utc. ref: /usr/sbin/quicksetup.

#in all quirky builds, /etc/localtime is a symlink. some other distros have it as a file.
LTflag='ok'
[ ! -h /etc/localtime ] && LTflag='bad'
[ "`ls -l /etc/localtime | grep 'usr/share/zoneinfo'`" == "" ] && LTflag='bad'
if [ "$LTflag" != "ok" ];then
 [ $NOX -eq 0 ] && popup "level=top terminate=ok background='#ff8080'|<big>ERROR</big>, /etc/localtime is misconfigured. In Quirky, it must be a symlink to the appropriate file in /usr/share/zoneinfo. Aborting."
 exit
fi

if ! which ntpdate >/dev/null;then
 [ $NOX -eq 0 ] && popup "level=top terminate=ok background='#ff8080'|<big>ERROR</big>, the 'ntpdate' utility required by Qsync is missing. Aborting."
 exit
fi

mkdir -p /var/local/qsync
mkdir -p /tmp/qsync
TIMEZONE="$(readlink /etc/localtime | rev | cut -f 1 -d '/' | rev)" #ex: GMT-8
case "$TIMEZONE" in
 GMT-*) zTIMEZONE="$(echo -n "$TIMEZONE" | sed -e 's%GMT\-%GMT+%')" ;;
 GMT+*) zTIMEZONE="$(echo -n "$TIMEZONE" | sed -e 's%GMT\+%GMT-%')" ;;
 *) zTIMEZONE="$TIMEZONE" ;;
esac

SERVERLIST='Africa,africa.pool.ntp.org
Asia,asia.pool.ntp.org
Europe,europe.pool.ntp.org
North America,north-america.pool.ntp.org
South America,south-america.pool.ntp.org
Oceania,oceania.pool.ntp.org
UK,uk.pool.ntp.org'
echo "$SERVERLIST" > /tmp/qsync/serverlist
DEF_SERVER='North America'
if [ -f /var/local/qsync/def_server ];then
 zDEF_SERVER="$(cat /var/local/qsync/def_server)" #ex: north-america.pool.ntp.org
 DEF_SERVER="$(echo "$SERVERLIST" | grep ",${zDEF_SERVER}$" | cut -f 1 -d ',')"
fi

QSYNC_ACTIVE='false'
[ -f /var/local/qsync/qsync_active ] && QSYNC_ACTIVE="$(cat /var/local/qsync/qsync_active)"
if [ "$QSYNC_ACTIVE" == "false" ];then
 msgSTATUS="<span fgcolor='red'><big>$(gettext 'Qsync is currently disabled')</big></span>"
 DEF_ACTIVE_CHK='true' #coz want to enable it.
 msgCHK="$(gettext 'Tick to enable Qsync')"
else
 msgSTATUS="<span fgcolor='green'><big>$(gettext 'Qsync is currently enabled')</big></span>"
 DEF_ACTIVE_CHK='true'
 msgCHK="$(gettext 'Untick to disable Qsync')"
fi

export DLG_QSYNC="<window title=\"Qsync\" image-name=\"/usr/local/lib/X11/mini-icons/mini-clock.xpm\">
 <vbox>
  <text use-markup=\"true\"><label>\"<big><b>Set date and time from the Internet</b></big>\"</label></text>
  <text><label>Choose a NTP server, then click the OK button. Local time will be immediately synchronized to the correct time retrieved from the server, if there is an Internet connection. Sync will occur everytime in the future when a connection to the Internet is established.</label></text>
  
  <frame $(gettext 'Status')>
   <hbox>
    <text use-markup=\"true\"><label>\"${msgSTATUS}\"</label></text>
    <text><label>\"     \"</label></text>
    <vbox>
     <text><label>$(gettext 'Localtime set to:')</label></text>
     <text><label>${zTIMEZONE}</label></text>
    </vbox>
   </hbox>
  </frame>
  
  <frame $(gettext 'Setup')>
   <hbox>
    <vbox>
     <checkbox>
      <label>${msgCHK}</label>
      <variable>ACTIVE_CHK</variable>
      <default>${DEF_ACTIVE_CHK}</default>
     </checkbox>
    </vbox>
    <text><label>\"  \"</label></text>
    <vbox>
     <text><label>$(gettext 'NTP server:')</label></text>
     <comboboxtext>
      <input>cut -f 1 -d ',' /tmp/qsync/serverlist</input>
      <default>${DEF_SERVER}</default>
      <variable>NEW_SERVER</variable>
     </comboboxtext> 
    </vbox>
   </hbox>
  </frame>
  <hbox>
   <button ok></button>
   <button cancel></button>
  </hbox>
 </vbox>
</window>"

if [ $NOX -eq 0 ];then
 RETVAL="$(gtkdialog --program=DLG_QSYNC --center)"
 eval "$RETVAL"
 [ "$EXIT" != "OK" ] && exit
else
 ACTIVE_CHK="$PRE_ACTIVE_CHK" #passed in when called from network_tray.
 NEW_SERVER="$DEF_SERVER"
fi

echo -n "$ACTIVE_CHK" > /var/local/qsync/qsync_active
zNEW_SERVER="$(echo n "$SERVERLIST" | grep "^${NEW_SERVER}" | cut -f 2 -d ',')"
echo -n "$zNEW_SERVER" > /var/local/qsync/def_server

[ "$ACTIVE_CHK" == "false" ] && exit

# -s means log to syslog
ntpdate -s -b $zNEW_SERVER

if [ $? -eq 0 ];then
 #set this to prevent re-sync in current session...
 touch /tmp/qsync/have-run
 hwclock --systohc --${HWCLOCKTIME} #ref: /etc/clock
fi

#ntpdate will run at future internet connection if /var/local/qsync/qsync_active
#is "true". this will be done elsewhere, say in network_tray.
#...network_tray version 3.2.1 does this. network_tray will test exist /tmp/qsync/have-run
# every 4secs, not exist then will call qsync again, until ntpdate succeeds.
###END###
