#!/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.
#100525 hardware clock can be utc or localtime (thanks to pizzasgood).
#120202 rodin.s: internationalized.

export TEXTDOMAIN=set-time-for-puppy
export TEXTDOMAINDIR=/usr/share/locale
export OUTPUT_CHARSET=UTF-8
eval_gettext () {
  local myMESSAGE=$(gettext "$1")
  eval echo \"$myMESSAGE\"
}
export LANGORG=$LANG

. /etc/clock #100525

# Title to be used for all Xdialog boxes.
TITLE="$(gettext 'Set time tool')"

# Get the date (returned in DD/MM/YYYY format by Xdialog.
ENTEREDDATE=`Xdialog --stdout --title "$TITLE" --calendar "$(gettext 'Please set the date...')" 0 0 0 0 0`
if [ ! $? -eq 0 ]; then
   exit
fi

# Convert the date to the MM/DD/YYYY format needed by hwclock.
NEWDATE=`echo "$ENTEREDDATE" | awk --source 'BEGIN { FS="/" }' --source '{ print $2 "/" $1 "/" $3 }'`

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


# Set the hardware clock (RTC) and then the system clock

D=`echo $NEWDATE|sed "s/^...//" | sed "s/\/.*$//"`

M=`echo $NEWDATE|sed "s/\/.*$//"`
Y=`echo $NEWDATE|sed "s/^........//" | sed "s/ .*$//"`

H=`echo $NEWTIME|sed "s/^.* //" | sed "s/://g" | sed "s/..$//"`

DT=`echo $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 --${HWCLOCKTIME} #100525

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