#!/bin/sh
#2007 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
# A small script used to set the time under Linux with hwclock...
# MU: updated for puppy 1.0.7.
# jamesbond 2012 - add gtksu & other fixes
# 131112 L18L internationalisation

# std localisation stanza
export TEXTDOMAIN=fatdog
. gettext.sh
# performance tweak - use "C" if there is no localisation
! [ -e $TEXTDOMAINDIR/${LANG%.*}/LC_MESSAGES/$TEXTDOMAIN.mo ] &&
! [ -e $TEXTDOMAINDIR/${LANG%_*}/LC_MESSAGES/$TEXTDOMAIN.mo ] && LANG=C

# Title to be used for all Xdialog boxes.
APPTITLE="$(gettext 'Set System Time')"
HWCLOCKPARM="--localtime" # default
HWCLOCKCONF=/etc/hwclock.conf
[ -e $HWCLOCKCONF ] && . $HWCLOCKCONF

[ $(id -u) -ne 0 ] && exec gtksu "$APPTITLE" "$0" 

# Get the date (returned in DD/MM/YYYY format by Xdialog)
! NEWDATE=`Xdialog --stdout --title "$APPTITLE" --calendar "$(gettext 'Please set the date...')" 0 0 0 0 0` && exit

# Get the time in HH:MM:SS format.
if ! NEWTIME=`Xdialog --stdout --title "$APPTITLE" --timebox "$(gettext 'Please set the time...')" 0 0`; then
   Xdialog --title "$APPTITLE" --msgbox "$(gettext 'Aborted.')" 0 0
   exit
fi

# Set system time, then use system time to set the hardware clock (RTC)
OIFS=$IFS; IFS=/; set -- $NEWDATE
D=$1 M=$2 Y=$3

IFS=:; set -- $NEWTIME; IFS=$OIFS
H=$1$2
DT=$M$D$H$Y

date $DT
Xdialog --title "$(gettext 'info')" --msgbox "$(gettext 'Your screen might turn black now for some seconds, just wait...')" 0 0

hwclock --systohc $HWCLOCKPARM
THEDATE=`date`
Xdialog --title "$(gettext 'info')" --msgbox "$(eval_gettext 'Finished. Time was set to $THEDATE')" 0 0
