#!/bin/bash

SUCCESS=0
FAILURE=1

source /etc/rc.d/rc.conf

set_adjtimex_parameters () {
    if [ -u /sbin/adjtimex -o -x /sbin/adjtimex ]
    then
        echo "Setting clock frequency and tick values from nonvolatile memory."
        if [ "$ADJTIMEX_TICK" -a "$ADJTIMEX_FREQ" ]
        then
            /sbin/adjtimex -t $ADJTIMEX_TICK -f $ADJTIMEX_FREQ
        else
            echo "Environment variables ADJTIMEX_TICK & ADJTIMEX_FREQ not found."
        fi
    else
        echo "adjtimex is not executable for this user"
    fi
}

set_sysclock_from_hwclock () {
    if [ -x /sbin/hwclock ]
    then
        echo "Setting system clock from hardware clock"
        /sbin/hwclock --hctosys
        /sbin/hwclock --show
    else
        echo "hwclock is not executable for this user -- not setting time"
        echo "Please set the system time using"
        echo "    date -s <yyyy-mm-dd HH:MM:SS>"
        echo "    /sbin/hwclock --systohc"
    fi
}

set_hwclock_from_sysclock () {
    if [ -x /sbin/hwclock ]
    then
        echo "Setting hardware clock from system clock"
        /sbin/hwclock --systohc
        /sbin/hwclock --show
    else
        echo "hwclock is not executable for this user"
    fi
}

test_connection_to_ntp_server () {
if [ "$NTP_SERVER" ]
then
    # 2013-09-03, mjs: resorted to killall construct below instead because ping and ntpclient on LRAUVs do not have a timeout arg (`ping -w 1000 -c 1 $NTP_SERVER` would be cleaner)
    ping -c 1 $NTP_SERVER &> /dev/null &
    sleep 1 # XXX this is the external timeout to the ping
    if eval "killall -0 ping &> /dev/null"
    then
        killall -9 ping 
        echo "could not ping ntp server"
        return $FAILURE
    else
        echo "ping returned from NTP server: $NTP_SERVER"
        return $SUCCESS
    fi
else
    echo "NTP_SERVER is not set (check /etc/rc.d/rc.conf)"
    return $FAILURE
fi
}

set_sysclock_from_ntp () {
if [ "$NTP_SERVER" ]
then
    /bin/ntpclient -s -i 15 -g 10000 -h $NTP_SERVER &
    sleep 1 # XXX this is the external timeout to the ntpclient request
    if eval "killall -0 ntpclient &> /dev/null"
    then
        killall -9 ntpclient 
        echo "request to NTP server took too long"
        return $FAILURE
    else
        echo "Time set from NTP server: $NTP_SERVER"
        return $SUCCESS
    fi
else
    echo "NTP_SERVER is not set (check /etc/rc.d/rc.conf)"
    return $FAILURE
fi
}

# now for the actual script...
if ! [ "$1" = "start" -o "$1" = "stop" -o "$1" = "restart" ]
then
    echo "please specify 'start', 'stop', or 'restart'"
fi

if [ "$1" = "stop" -o "$1" = "restart" ] 
then
    echo "settime stop or restart"
    set_hwclock_from_sysclock
fi

if [ "$1" = "start" -o "$1" = "restart" ]
then
    echo "settime start or restart"
    set_adjtimex_parameters
    if test_connection_to_ntp_server; then
        if set_sysclock_from_ntp; then
            set_hwclock_from_sysclock
            # TODO Add a line to log ntp and slew adjtimex, iff we can confirm that it exits gracefully when disconnected.
        else
            set_sysclock_from_hwclock
        fi
    else
        echo "no connection to NTP server, setting system clock from hardware clock"
        set_sysclock_from_hwclock
    fi
fi

