#!@l_prefix@/bin/openpkg rc
##
##  rc.ntp -- Run-Commands
##

%config
    ntp_enable="$openpkg_rc_def"
    ntp_mode="daemon"
    ntp_daemon_flags=""
    ntp_log_prolog="true"
    ntp_log_epilog="true"
    ntp_log_numfiles="10"
    ntp_log_minsize="1M"
    ntp_log_complevel="9"

%common
    ntp_pidfile=`grep "^pidfile" @l_prefix@/etc/ntp/ntp.conf | awk '{ printf("%s", $2); }'`
    ntp_signal () {
        [ -f $ntp_pidfile ] && kill -$1 `cat $ntp_pidfile`
    }
    ntp_once () {
        [ ".$1" != . ] && sleep $1
        egrep "^(logfile|pidfile|driftfile|statsdir|server)" \
            @l_prefix@/etc/ntp/ntp.conf \
            >@l_prefix@/var/ntp/ntp.conf.once
        @l_prefix@/sbin/ntpd -q -g \
            -c @l_prefix@/var/ntp/ntp.conf.once
        rm -f @l_prefix@/var/ntp/ntp.conf.once
    }

%status -u @l_susr@ -o
    ntp_usable="unknown"
    ntp_active="no"
    rcService ntp enable yes && \
    [ ".$ntp_mode" = ".daemon" ] && \
        ntp_signal 0 && ntp_active="yes"
    echo "ntp_enable=\"$ntp_enable\""
    echo "ntp_usable=\"$ntp_usable\""
    echo "ntp_active=\"$ntp_active\""

%start -p 200 -u @l_susr@
    rcService ntp enable yes || exit 0
    rcService ntp active yes && exit 0

    #   under both periodical and daemon mode, synchronize local machine
    #   once manually -- either to fill the gap between startup time and
    #   the first run of the cron jobs (periodical mode) or to perform
    #   a possible large time offset which ntpd does not perform itself
    #   (daemon mode).
    case "$ntp_mode" in
        once | \
        daemon | \
        quarterly | hourly | daily | weekly | monthly )
            ntp_once
            ;;
    esac

    #   run the NTP daemon for continued synchronization
    if [ ".$ntp_mode" = .daemon ]; then
        @l_prefix@/sbin/ntpd ${ntp_daemon_flags}
    fi

%stop -p 800 -u @l_susr@
    rcService ntp enable yes || exit 0
    rcService ntp active no  && exit 0
    ntp_signal TERM
    rm -f $ntp_pidfile 2>/dev/null || true

%restart -p 200 -u @l_susr@
    rcService ntp enable yes || exit 0
    rcService ntp active no  && exit 0
    rc ntp stop start

%quarterly -p 800 -u @l_susr@
    rcService ntp enable yes || exit 0
    [ ".$ntp_mode" = .quarterly ] && ntp_once 30
    exit 0

%hourly -p 800 -u @l_susr@
    rcService ntp enable yes || exit 0
    [ ".$ntp_mode" = .hourly ] && ntp_once 30
    exit 0

%daily -p 800 -u @l_susr@
    rcService ntp enable yes || exit 0
    [ ".$ntp_mode" = .daily ] && ntp_once 30
    shtool rotate -f \
        -n ${ntp_log_numfiles} -s ${ntp_log_minsize} -d \
        -z ${ntp_log_complevel} -m 644 -o @l_susr@ -g @l_mgrp@ \
        -P "${ntp_log_prolog}" \
        -E "${ntp_log_epilog}; rc ntp restart" \
        @l_prefix@/var/ntp/ntp.log
    exit 0

%weekly -p 800 -u @l_susr@
    rcService ntp enable yes || exit 0
    [ ".$ntp_mode" = .weekly ] && ntp_once 30
    exit 0

%monthly -p 800 -u @l_susr@
    rcService ntp enable yes || exit 0
    [ ".$ntp_mode" = .monthly ] && ntp_once 30
    exit 0

