#!/bin/sh
# $Id: timezones,v 1.19 2005/10/10 10:54:31 zerg Exp $


PROG="${0##*/}" #program name

. /etc/init.d/functions

print_version()
{
	cat <<EOF
$PROG version 0.1

Written by Anton Farygin

Copyright (C) 2005 ALT Linux
EOF
	exit
}

print_usage()
{
	[ "$1" = 0 ] || exec >&2
	cat <<EOF
Usage: $PROG [options] 

module for clock setup

Valid options are:
  -h, --help	display help screen
  -v, --version	display version information
  -l, --list	get list of available timezones
  -t, --type	check 
  -r, --read	read current timezone
  -w, --write	write current timezone

Report bugs to <rider@altlinux.org>
EOF
	[ -n "$1" ] && exit "$1" || exit
}

ZONETABFILE=/usr/share/zoneinfo/zone.tab
ISOTABFILE=/usr/share/zoneinfo/iso3166.tab
NTPDATE=/usr/sbin/ntpdate
NTPD=/usr/sbin/ntpd

TEMP=`getopt -n $PROG -o h,v,l:,d:,r:,w:,t: -l help,version,list:,delete:,read:,write:,type: -- "$@"` || print_usage
eval set -- "$TEMP"

while :; do
	case "$1" in
		-h|--help) print_usage 0
			;;
		-v|--version) print_version
			;;
		-l|--list)
			shift;dir=$1;
			if [ "$dir" == "/" ];then
				echo "current r"
				echo "ntp r"
				echo "ntpserver_test r"
				echo "ntpservers d"
				echo "zonelist d"
			elif [ "$dir" == "ntpservers" ]; then
			    for s in "ntp.ru" "fartein.ifi.uio.no" "clock.nc.fukuoka-u.ac.jp" \
				    "ntp.adelaide.edu.au" "ntp.cs.strath.ac.uk" "ntp1.cmc.ec.gc.ca" \
				    "pool.ntp.org"
			    do
				echo "$s r"
			    done
			elif [ "$dir" == "zonelist" ];then
			    TMPISO=`mktemp /tmp/isotab.XXXXXXXXX`
			    grep -v "^#" $ISOTABFILE|sort -u >$TMPISO
			    grep -v "^#" $ZONETABFILE|sort|join -1 1 -2 1 $TMPISO - -o "1.2,2.3" -t"	"|sed "s,\t.*/,/,;s,$, r,"|sort -r
			    rm -f $TMPISO
			fi
			;;
		-d|--delete)
			shift;name=$1;
			;;
		-t|--type)
			shift;file=$1;
			if [ "$file" = "current" ];then
				echo "current r"
			elif [ "$file" = "ntp" ];then
				echo "ntp r"
			elif [ "$file" = "ntpserver_test" ];then
				echo "ntpserver_test r"
			elif [ "$file" = "ntpservers" ];then
				echo "ntpservers d"
			elif [ "$file" = "zonelist" ];then
				echo "zonelist d"
			fi
			#TODO: also checking for each zone
			;;
		-r|--read)
			shift;name=$1;
			if [ "$name" == "current" ];then
			    SourceIfNotEmpty /etc/sysconfig/clock
			    [ -z "$ZONE" ] && ZONE="Europe/Moscow"
			    REGION=${ZONE#*/}
			    COUNTRY_CODE=$(grep "^[^#].*$ZONE" $ZONETABFILE|cut -f1 -d'	')
			    COUNTRY_NAME=$(grep "^$COUNTRY_CODE" $ISOTABFILE|cut -f2 -d'	')
			    echo "ZONE:$COUNTRY_NAME/$REGION"
			    echo -n "UTC:"
			    is_no $UTC
			    echo $?
			elif [ "$name" == "ntp" ];then
			    SERVER=`grep -e "^server\s*.*" /etc/ntp.conf 2>/dev/null| sed -e "s|.*server\s*\(.*\)$|\1|" -e "s|\s*minpoll.*$||" -e "s|\s*maxpoll.*$||"| head -n 1`
			    [ -n "$SERVER" ] || SERVER="ntp.ru"
			    echo "SERVER:$SERVER"
			    echo -n "NTP:"
			    LC_ALL=C $(! chkconfig --list ntpd 2>/dev/null| grep -q ':on')
			    echo $?
			    echo -n "PACKAGES_INSTALLED:"
			    if [ -x "$NTPD" -a -x "$NTPDATE" ]; then
				echo "1"
			    else
				echo "0"
			    fi
			elif echo "$name"| grep -q '^ntpserver_test/'; then
			    SERVER=`echo "$name" |sed 's|^ntpserver_test/||'`
			    if [ -n "$SERVER" ]; then
				NTPD_RUNNING=""
				service ntpd status >/dev/null 2>&1 && NTPD_RUNNING="yes"
				if [ "$NTPD_RUNNING" == "yes" ]; then
				    service ntpd condstop
				fi
				echo -n "RESULT:"
				! $NTPDATE -t 5 "$SERVER" >/dev/null 2>&1
				echo $?
				if [ "$NTPD_RUNNING" == "yes" ]; then
				    service ntpd start
				fi
			    else
				echo "RESULT:0"
			    fi
			fi
			;;
		-w|--write)
			shift;name=$1;
			if [ "$name" == "current" ];then
				while read l; do
					option=${l%%:*}
					value=${l#*:}
					case $option in
						UTC)
						     	subst "s,.*UTC=.*$,UTC=$value," /etc/sysconfig/clock
							;;
						ZONE)
							COUNTRY=$(grep "${value%%/*}$" $ISOTABFILE|cut -f1 -d'	')
							if [ -n "$COUNTRY" ]; then
								ZONE=$(grep "^$COUNTRY.*${value#*/}	" $ZONETABFILE|cut -f3 -d'	')
						    		if [ -f "/usr/share/zoneinfo/$ZONE" ];then
									subst "s,.*ZONE=.*$,ZONE=\"$ZONE\"," /etc/sysconfig/clock
									cat "/usr/share/zoneinfo/$ZONE" >/etc/localtime
						    		fi
							fi
							;;
						*)
							;;
					esac
				done
			elif [ "$name" == "ntp" ]; then
			    while read l; do
				option=${l%%:*}
				value=${l#*:}
				case $option in
				    NTP)
					if [ "$value" == "yes" ]; then
					    chkconfig ntpd on
					    if service ntpd status; then
						service ntpd restart
					    else
						service ntpd start
					    fi
					else
					    chkconfig ntpd off
					    service ntpd stop
					fi
					;;
				    SERVER)
					if [ -f /etc/ntp.conf ]; then
					    subst "s|^\s*server\s*.*$||" /etc/ntp.conf
					    subst "s|^restrict\s*[[:digit:]]*\.[[:digit:]]*\.[[:digit:]]*\.[[:digit:]]*$||" /etc/ntp.conf
					    echo "server $value minpoll 10 maxpoll 14" >> /etc/ntp.conf
					    echo "restrict 127.0.0.1" >>/etc/ntp.conf
					    echo "restrict $value" >>/etc/ntp.conf
					fi
					if [ -f /etc/ntp/step-tickers ]; then
					    subst "s|^\s*[[:digit:]]*\.[[:digit:]]*\.[[:digit:]]*\.[[:digit:]]*$||" /etc/ntp/step-tickers
					    echo "$value" >> /etc/ntp/step-tickers
					fi
					;;
				    *)
					;;
				esac
			    done
			fi
			;;
		--) shift; break
			;;
		*) Fatal "unrecognized option: $1"
			;;
	esac
	shift
done
