#!/bin/sh

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

}

TIMEZONES=$(tempfile) || TIMEZONES=/etc/timezone.$$

if [ -f /etc/timezone ]; then
	echo "Your current time zone is set to `cat /etc/timezone`"
	echo -n "Do you want to change that? [n]: "
	read ans
	if [ "x$ans" = "x" -o "$ans" = "n" -o "$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
name=""

while [ $valid = no ]; do
	if [ -n "$name" ]; then
		number=`grep -i -c "^$continent/$name" $TIMEZONES`
		if [ $number -eq 1 ]; then
			name=`grep -i "^$continent/$name" $TIMEZONES`
			echo "Your default time zone is set to $name"
			valid=yes
			break
		fi
	fi
	echo
	grep -i "^$continent/$name" $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: [$name] " ; read name
done

echo $name > /etc/timezone
rm -f /etc/localtime ; ln -s /usr/share/zoneinfo/$name /etc/localtime
rm -f $TIMEZONES
