#!/bin/sh
#
# sntpd
#
# start the sntp daemon

echo entering $0 ...

start() {
  if [ -f /usr/sbin/sntpd.sh ]; then
     if [ -f /etc/init.d/rc.config ]; then
        . /etc/init.d/rc.config
        # get from /etc/function
        readConfig
     fi
     if [ -z $ENETCFG_SNTP ]; then
        echo "$0: ENETCFG_SNTP is not defined "
        exit 1
     fi
     if [ $ENETCFG_SNTP =  "0.0.0.0" ]; then
        echo "$0: ENETCFG_SNTP has not a valid value: $ENETCFG_SNTP"
        exit 1
     fi
     echo "$0: Starting SNTP with server $ENETCFG_SNTP"
     # update current server settings
     CLISettings set CURRENT ENETCFG_SNTP $ENETCFG_SNTP
     CLISettings set CURRENT ENETCFG_SNTP_REFRESH_PERIOD $ENETCFG_SNTP_REFRESH_PERIOD
     # start daemon
     ( trap "" HUP && exec sntpd.sh $ENETCFG_SNTP $ENETCFG_SNTP_REFRESH_PERIOD) &
  else
     echo "$0: no sntpd.sh script found"
     exit 1
  fi
}

stop() {
  echo "$0: Stopping SNTP"
  /etc/cron.hourly/updateTimestamp
  killall -q sntpd.sh
  killall -q sntp
  if [ -f /var/run/sntpd_wait.pid ]; then
     kill -9 `cat /var/run/sntpd_wait.pid`
     rm -f /var/run/sntpd_wait.pid
  fi
}

restart() {
  stop
  start
}

case "$1" in
   start|"")
      start
      ;;
   stop)
      stop
      ;;
   restart)
      restart
      ;;
   *)
      echo "Usage $0 {start|stop|restart}" >&2
      exit 1
      ;;
esac

echo leaving $0 ...

