#!/bin/sh

# ntpd options
# -n Don't fork. So returns can be logged.
# -g This option allows the time to be set to any value without restriction; however, this can happen only once.
# -x Normally, the time is slewed if the offset is less than the step threshold, which is 128 ms by default, and stepped if above the threshold.
#    This option sets the threshold to 600 s, which is well within the accuracy window to set the clock manually. 

. /usr/local/bin/telem/gps_functions

CONFIGFILE=/etc/ntp.conf
touch /var/local/telem/ntp_is_enabled
if isGPSttyEnabled; then
    CONFIGFILE=/etc/ntp-run.conf
    touch /var/local/telem/gps_is_enabled      
fi
killall check_ntp_status
/usr/local/bin/ntp/check_ntp_status &

while :; do
    logger "Starting NTP daemon"
    ntpd -n -g -x -c $CONFIGFILE
    hwclock -w -u
    logger "NTP daemon failed, restarting in 2 seconds"
    sleep 2
done

echo "Done"
