#! /bin/sh

# Copyright (C) 2001 Software in the Public Interest
#
# Licensed under the GPL v2
#

show_continents() {

cat <<EOF

Please enter the number of the geographic area in which you live:


        1) Africa			7) Australia

        2) America			8) Europe

        3) US time zones		9) Indian Ocean

        4) Canada time zones		10) Pacific Ocean

        5) Asia				11) Use System V style time zones

        6) Atlantic Ocean		12) None of the above


Then you will be shown a list of cities which represent the time zone
in which they are located. You should choose a city in your time zone.

EOF

}

zone_info()
{
        TZBase=$(LC_ALL=C TZ=UTC0 date)
        UTdate=$(TZ=UTC0 date -d "$TZBase")
        TZdate=$(TZ="$timezone" date -d "$TZBase")
        extra_info="
Local time is now:      $TZdate.
Universal Time is now:  $UTdate."
}

TIMEZONES=$(tempfile -p time) || TIMEZONES=/etc/timezone.$$

if [ -f /etc/timezone ] || [ -f /etc/localtime ] ; then
        current_timezone="Unknown"
        if [ -L /etc/localtime ] ; then
                current_timezone=$(readlink /etc/localtime | sed 's%^/usr/share/zoneinfo/%%')
        elif [ -f /etc/timezone ] && [ -f /etc/localtime ]; then
                current_timezone=$(cat /etc/timezone)
        fi
        
        echo "Your current time zone is set to $current_timezone"
        echo -n "Do you want to change that? [n]: "
        read ans
        if [ "x$ans" = "x" ] || [ "$ans" = "n" ] || [ "$ans" = "no" ]; then
                echo "Your time zone will not be changed"
                exit 0
        fi
fi

( cd /usr/share/zoneinfo ; find . -type f | sed -e 's/^.\///' | sort > $TIMEZONES )
valid=no
while [ $valid = no ]; do
        show_continents
        echo -n "Number: " ; read area
        case $area in
                1) continent=Africa ; valid=yes ;;
                2) continent=America ; valid=yes ;;
                3) continent=US ; valid=yes ;;
                4) continent=Canada ; valid=yes ;;
                5) continent=Asia ; valid=yes ;;
                6) continent=Atlantic ; valid=yes ;;
                7) continent=Australia ; valid=yes ;;
                8) continent=Europe ; valid=yes ;;
                9) continent=Indian ; valid=yes ;;
                10) continent=Pacific ; valid=yes ;;
                11) continent=SystemV ; valid=yes ;;
                12) continent=Etc ; valid=yes ;;
        esac
done

if [ -x /usr/bin/fmt ]; then fmt_bin=/usr/bin/fmt
else fmt_bin=/bin/more ; fi

valid=no
zone=""

while [ $valid = no ]; do
        if [ -n "$zone" ]; then
                # We accept the zone if either it is an unambiguous prefix, or
                # it is a complete zone name.
                number=`grep -i -c "^$continent/$zone" $TIMEZONES`
                if [ $number -eq 1 ]; then
                        timezone=`grep -i "^$continent/$zone" $TIMEZONES`
                else
                        timezone=`grep -i -s "^$continent/$zone"'$' $TIMEZONES`
                fi

                if [ -n "$timezone" ]; then
                        zone_info
                        echo "Your default time zone is set to '$timezone'.$extra_info"
                        valid=yes
                        break
                fi
        fi
        echo
        grep -i "^$continent/$zone" $TIMEZONES | cut -d/ -f2- | $fmt_bin
        echo
        echo "Please enter the name of one of these cities or zones"
        echo "You just need to type enough letters to resolve ambiguities"
        echo "Press Enter to view all of them again"
        echo -n "Name: [$zone] " ; read zone
done

umask 022
echo $timezone > /etc/timezone 
rm -f /etc/localtime && cp -f /usr/share/zoneinfo/$timezone /etc/localtime
trap 'rm -f $TIMEZONES' 0 1 2 3 13 15
