#!/bin/bash

# This script can be used to set the linux system clock in one of two ways: ntp or dhcp.
#
# If the ntp parameter is passed, the script expects a valid IP address for the NTP server 
#
# If the dhcp parameter is passed, NTP server will be requested using DHCP option 42
# 
# Usage
# 
# For NTP:
#   ntp <ip address>
#
#
# If dhcp is chosen, then NYQUIST will to rely on the DHCP server to define NTP servers 
#            and time services. The user will have no other control over system time
#            parameters.
#
type=$1
paramnum="$#"

INTERFACE_FILE=/etc/network/interfaces
NET_INTERFACE_TYPE=`sed -n -e "s/^\s*iface.*\(dhcp\|static\)/\1/p" $INTERFACE_FILE`

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
}

RestartNetwork() 
{
	service networking restart
	echo "Restarting Network Service"
}
        
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"

echo "Network Interface Type: $NET_INTERFACE_TYPE"

if [ "$type" == "ntp" ] || [ "$type" == "nyquist" ]; 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

	if grep -q ntp-servers $dhclient_loc; then
		echo "NTP servers are already being configured via DHCP, removing"
		sudo sed -i 's/, ntp-servers;/;/g;s/ntp-servers, //g' $dhclient_loc
		echo "  Removing ntp-servers from $dhclient_loc"

		# Only restart network if we have to make a change
		RestartNetwork
	else
		echo "NTP Servers are not currently set using DHCP, no need to restart dhclient"
	fi
		
	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 /etc/init.d/ntp restart
elif [ "$type" == "dhcp" ]; then
	if [ "$NET_INTERFACE_TYPE" == "static" ]; then
		echo "Error - attempting to use DHCP NTP configuration for statically configured IP"
		exit 1
        fi
	echo "Configuring DHCP Option 42"
	if grep -q ntp-servers $dhclient_loc; then
		echo "NTP servers are already being configured via DHCP, no need to restart dhclient"
	else
		echo "Adding ntp-servers to $dhclient_loc"
		sudo sed -i 's/request /request ntp-servers, /g' $dhclient_loc 

		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

		sync
		sleep 2
		reboot
	fi
fi


