#!/bin/bash

# This script can be used to set the linux system clock in one of two ways: ntp or man.
#
# If the ntp parameter is passed, the script expects a valid IP address for the NTP server 
#
# If the man parameter is passed, the script will update the linux system clock based on 
# the date and time provided
# 
# Usage
# 
# For NTP:
#   ntp <ip address>
#
# For MAN
#   man <dd> <month> <yyyy> <hh> <mm> <ss>
#
#    The month parameter must be:
#    JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC
#
# In addtion, the TIMEZONE can be set using this script as follows:
#
# timezone <system_timezone>
#
#   where system_timezone is one of the predefined timezones recognized by linux
#
#     examples:
#       America/New_York
#       Asia/Calcutta
#       Canada/Atlantic
#       Europe/London
#
# *IMPORTANT NOTE*
#  The only way the timezone parameter will "stick" is if you source this script:
#    source ./clock_set timezone America/New_York
#      or
#    . ./clock_set timezone America/New_York
#
# If dhcp_override is chosen, then the user must specifiy "enable" or "disable"
#   enable - causes NYQUIST to rely on DHCP server to define NTP servers and time 
#            services. The user will have no other control over system time
#            parameters.
#   disable - The user will be able to manually set the system time, or specify the
#             preferred NTP servers.
#
# If "current" is chosen, this script will return the current NTP server being used.
type=$1
paramnum="$#"

check_num_parameters()
{
        # Exit if number of parameters are incorrect
        expectedparamnum=$1
        if [ "$paramnum" -ne $expectedparamnum ]; then
                echo "Expected $expectedparamnum arguments - exiting"
                exit 1
        fi
}

if [ "$type" == "ntp" ]; then
        check_num_parameters 2
        sudo /etc/init.d/ntp stop
        sudo sntp --timeout=5 -s "$2" 2> /dev/null
        if [ $? -eq 0 ]; then
                sudo sed -i -e "s/^server.*/server $2 iburst/" /etc/ntp.conf
                echo Time set correctly from NTP server # "$2".
        elif [ $? -eq 1 ]; then
                echo SNTP Error -- Operation Failed.
        elif [ $? -eq 2 ]; then
                echo SNTP Parameter Error -- A parameter passed to this function is invalid.
        else
                echo SNTP Unknown Error
        fi
        sudo /etc/init.d/ntp start
elif [ "$type" == "man" ]; then
        check_num_parameters 7
        sudo date --set="$2 $3 $4 $5:$6:$7"
elif [ "$type" == "timezone" ]; then
        check_num_parameters 2
        sudo sed -i '1s|^.*$|'$2'|' /etc/timezone
	sudo ln -sfn /usr/share/zoneinfo/$2 /etc/localtime
	export TZ=$2
elif [ "$type" == "dhcp_override" ]; then
        check_num_parameters 2
        ntp_conf_dhcp_dir="/var/lib/ntp"
        ntp_exit_hook_dir="/etc/dhcp/dhclient-exit-hooks.d"
        ntp_conf_dhcp_file="$ntp_conf_dhcp_dir/ntp.conf.dhcp"
        ntp_conf_dhcp_backup="$ntp_conf_dhcp_file".backup
	ntp_exit_hook_file="$ntp_exit_hook_dir/ntp"
	ntp_exit_hook_backup="$ntp_exit_hook_file".backup
        #echo $ntp_conf_dhcp_file
	#echo $ntp_conf_dhcp_backup
	#echo $ntp_exit_hook_file
	#echo $ntp_exit_hook_backup
        dhclient_loc="/etc/dhcp/dhclient.conf"
        if [ "$2" == "enable" ]; then
                if grep -q ntp-servers $dhclient_loc; then
                        echo "NTP servers are already being configured via DHCP"
                else
                        echo "Adding ntp-servers to $dhclient_loc"
                        sudo sed -i 's/request /request ntp-servers, /g' $dhclient_loc  
                fi
                if [ -f "$ntp_conf_dhcp_backup" ]; then
			echo "Restoring $ntp_conf_dhcp_backup to $ntp_conf_dhcp_file"
			sudo mv $ntp_conf_dhcp_backup $ntp_conf_dhcp_file
                else
			if [ -f "$ntp_conf_dhcp_file" ]; then
	                       	echo "$ntp_conf_dhcp_file is already present"
			else
				echo "$ntp_conf_dhcp_file does not exist"
			fi
                fi

		if [ -f "$ntp_exit_hook_backup" ]; then
                        echo "Restoring $ntp_exit_hook_backup to $ntp_exit_hook_file"
                        sudo mv $ntp_exit_hook_backup $ntp_exit_hook_file
                else
                        if [ -f "$ntp_exit_hook_file" ]; then
                                echo "$ntp_exit_hook_file is already present"
                        else
                                echo "$ntp_exit_hook_file does not exist"
                        fi
                fi

		sudo dhclient eth0

        elif [ "$2" == "disable" ]; then
                sudo sed -i 's/, ntp-servers;/;/g;s/ntp-servers, //g' $dhclient_loc
                echo "Removing ntp-servers from $dhclient_loc"
		if [ -f "$ntp_conf_dhcp_file" ]; then
                        echo "Renaming $ntp_conf_dhcp_file to $ntp_conf_dhcp_backup"
                        sudo mv $ntp_conf_dhcp_file $ntp_conf_dhcp_backup
                else
                       echo "$ntp_conf_dhcp_file does not exist"
                fi


		if [ -f "$ntp_exit_hook_file" ]; then
                        echo "Renaming $ntp_exit_hook_file to $ntp_exit_hook_backup"
                        sudo mv $ntp_exit_hook_file $ntp_exit_hook_backup
                else
                       echo "$ntp_exit_hook_file does not exist"
                fi

		sudo dhclient eth0
        	sudo /etc/init.d/ntp restart
        fi
elif [ "$type" == "current" ]; then
	temp_ntp_query_file=/tmp/tempntp.log
	ntpq -pn > $temp_ntp_query_file
	echo -n "Current NTP Server: "
	if [ "$(grep -cim1 "^*" /tmp/tempntp.log)" == 1 ]; then
		cat $temp_ntp_query_file | grep '^*' | sed 's/*//' | awk '{ print $1 }'
	else
		cat $temp_ntp_query_file | sed -n '3,3'p | awk '{ print $1 }'
	fi
	rm -f $temp_ntp_query_file
else
        echo First argument must be ntp, man, timezone, dhcp_override, or current
fi

