#!/bin/sh
#(c) Copyright Barry Kauler 2007 www.puppylinux.com
#2007 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
#utility to select the timezone.
#Note, as Puppy will mostly be on PCs coexisting with Windows,
#hardware clock (CMOS) is set to local time (see /etc/rc.d/rc.local0)
#BK oct2007: updated for 3.02
#w019 april 2009: cli mode so can run from /etc/rc.d/rc.update
#100525 hardware clock can be utc or localtime (thanks to pizzasgood).
#100526 screen out UTC timezone, causes confusion (separate script to choose localtime or UTC).

export LANG=C
. /etc/clock #100525

XSTATUS="no"
[ -z $DISPLAY ] || XSTATUS="yes"
[ $1 ] && XSTATUS="no"

if [ -e /etc/localtime ];then
 CZONE="`readlink /etc/localtime`"
 [ "$CZONE" = "" ] && rm -f /etc/localtime
fi

if [ -e /etc/localtime ];then
 CZONE="`readlink /etc/localtime`"
 DEFTAG="`readlink /etc/localtime | sed -e 's%/usr/share/zoneinfo/%%'`"
else
 CZONE='/usr/share/zoneinfo/Etc/GMT-8' #these have opposite sign.
 DEFTAG='Etc/GMT-8'
fi
if [ "`echo -n "$DEFTAG" | grep 'GMT' | grep '\+'`" = "" ];then
 DEFTAG="`echo -n "$DEFTAG" | sed -e 's%Etc/%%' | tr "\-" "\+"`"
else
 DEFTAG="`echo -n "$DEFTAG" | sed -e 's%Etc/%%' | tr "\+" "\-"`"
fi

#100526 screen out UTC timezone, causes confusion...
ZONEINFO="`find /usr/share/zoneinfo -type f | grep -v '\.tab$' | grep -v 'UTC' | sed -e 's%/usr/share/zoneinfo/%%' | sed -e 's%Etc/%%' | tr ' ' '_' | sort | tr '\n' ' '`"

ZONECHOICES=""
for ONEZONE in $ZONEINFO
do
 ONEDESCR="."
 [ "$ONEZONE" = "GMT" ] && continue
 [ "$ONEZONE" = "GMT-0" ] && continue
 case $ONEZONE in
  GMT+0)     ONEDESCR='(London, Dublin, Edinburgh, Lisbon, Reykjavik, Casablanca)';;
  GMT-1)     ONEDESCR='(Azores, Cape Verdes)';;
  GMT+1)     ONEDESCR='(Paris, Berlin, Amsterdam, Brussels, Madrid, Stockholm Oslo)';;
  GMT-2)     ONEDESCR='(mid-Atlantic)';;
  GMT+2)     ONEDESCR='(Athens, Helsinki, Istanbul, Jerusalem, Harare)';;
  GMT-3)     ONEDESCR='(Brasilia, Buenos Aires, Georgetown)';;
  GMT+3)     ONEDESCR='(Kuwait, Nairobi, Riyadh, Moscow)';;
  GMT-4)     ONEDESCR='(Caracas, La Paz, Canada)';;
  GMT+4)     ONEDESCR='(Abu Dhabi, Muscat, Tblisi, Volgograd, Kabul)';;
  GMT-5)     ONEDESCR='(Bogota, Lima, New York)';;
  GMT+5)     ONEDESCR='(Islamabad, Karachi)';;
  GMT+5:30)  ONEDESCR='(India)';;
  GMT-6)     ONEDESCR='(Mexico City, Saskatchewan)';;
  GMT+6)     ONEDESCR='(Almaty, Dhaka)';;
  GMT+6:30)  ONEDESCR='(Cocos Islands)';;
  GMT-7)     ONEDESCR='(Alberta, Montana, Arizona)';;
  GMT+7)     ONEDESCR='(Bangkok, Jakarta)';;
  GMT-8)     ONEDESCR='(Los Angeles)';;
  GMT+8)     ONEDESCR='(Perth, Singapore, Hongkong)';;
  GMT-9)     ONEDESCR='(Alaska)';;
  GMT+9)     ONEDESCR='(Tokyo)';;
  GMT+9:30)  ONEDESCR='(Darwin, Adelaide)';;
  GMT-10)    ONEDESCR='(Alaska, Hawaii)';;
  GMT+10)    ONEDESCR='(Guam)';;
  GMT+10:30) ONEDESCR='(Lord Howe Island)';;
  GMT-11)    ONEDESCR='(Samoa)';;
  GMT+11)    ONEDESCR='(Magadan, Soloman Is.)';;
  GMT-12)    ONEDESCR='(Eniwetok)';;
  GMT+12)    ONEDESCR='(Wellington, Fiji, Marshall Islands)';;
  GMT+13)    ONEDESCR='(Rawaki Islands)';;
  GMT+14)    ONEDESCR='(Line Islands)';;
 esac
 ZONECHOICES="${ZONECHOICES}${ONEZONE} \"${ONEDESCR}\" "
done

if [ "$XSTATUS" = "yes" ];then
 ZONEDLG="Xdialog --stdout --title \"Puppy timezone selector\" --default-item $DEFTAG --menubox \"Please choose your timezone.\\nIf a city/region/country in your timezone is not listed, choose a GMT<number>\" 0 0 0 $ZONECHOICES"
 eval $ZONEDLG >/tmp/zoneretval
else
 ZONEDLG="dialog --aspect 10 --title \"Puppy timezone selector\" --default-item $DEFTAG --menu \"Please choose your timezone. If a city/region/country in your timezone is not listed, choose a GMT<number>\" 0 0 0 ${ZONECHOICES}"
 eval $ZONEDLG 2>/tmp/zoneretval
fi

[ $? -ne 0 ] && exit

ZONERETVAL="`cat /tmp/zoneretval`"

if [ "`echo -n "$ZONERETVAL" | grep 'GMT' | grep '\+'`" = "" ];then
 ZONERETVAL="`echo -n "$ZONERETVAL" | tr "\-" "\+"`"
else
 ZONERETVAL="`echo -n "$ZONERETVAL" | tr "\+" "\-"`"
fi

#validity check...
[ "`echo "$ZONEINFO" | grep "$ZONERETVAL"`" = "" ] && exit

[ "`echo "$ZONERETVAL" | grep 'GMT'`" != "" ] && ZONERETVAL="Etc/$ZONERETVAL"

rm -f /etc/localtime
ln -s /usr/share/zoneinfo/$ZONERETVAL /etc/localtime

rm -f /etc/TZ #don't think need this anymore. also removed from /etc/profile.
#.../etc/profile now reads /etc/localtime and exports TZ variable.

#need to set Linux system time/date, from hardware clock...
#hwclock --hctosys --localtime
hwclock --hctosys --${HWCLOCKTIME} #100525
#...--hctosys reads cmos clock to system, referencing /usr/share/zoneinfo/localtime
#...--localtime means that cmos clock is set to local-time.

###END###
