#! /bin/sh
#----------------------------------------------------------------------------
# rc920.chrony - configure and start chrony
#
# Creation:	2004-06-06  abe
# Last Update:  $Id: rc920.chrony 10529 2005-12-06 13:36:43Z abe $
#
#----------------------------------------------------------------------------

case $OPT_CHRONY in
yes)
    begin_script CHRONY "starting chrony"

    # disable rtc support if BIOS RTC is broken
    [ -f /var/run/broken_rtc ] || do_insmod rtc

    chrony_conf="/etc/chrony.conf"
    chrony_key="/etc/chrony.keys"
    chrony_log="/var/run"
    chrony_default_server="pool.ntp.org"
    chrony_serverlist="/etc/chrony_server.conf"

    [ -n "$CHRONY_LOG" ] && chrony_log=`echo $CHRONY_LOG | sed -e 's#/$##'`

    [ ! -d $chrony_log ] && mkdir -p $chrony_log


    # create file etc/chrony.keys

    if [ ! -f $chrony_key ]       	# already existing?
    then                                 # yes, don't overwrite
        cat <<-EOF > $chrony_key
	#----------------------------------------------------------------------
	# /etc/chrony.keys
	# Automatically created by rc920.chrony
	#----------------------------------------------------------------------
	1 dummy
EOF
    fi

    # create file etc/chrony_server.conf
    if [ ! -f $chrony_serverlist ]      # already existing?
    then                                # yes, don't overwrite
        [ ${CHRONY_TIMESERVER_N:=3} -eq 0 ] || {
            prefix=0
            for idx in `seq 1 $CHRONY_TIMESERVER_N`
            do
                eval chrony_server='$CHRONY_TIMESERVER_'$idx
                chrony_server=${chrony_server:-$prefix.$chrony_default_server}
                echo "server $chrony_server maxpoll 14 offline"
                serverlist="$serverlist$chrony_server "
                [ $prefix -lt 2 ] && prefix=`expr $prefix + 1` || prefix=0
            done
        } >> $chrony_serverlist
    fi

    # create file etc/chrony.conf
    if [ ! -f $chrony_conf ]             # already existing?
    then                                 # yes, don't overwrite
        cat <<-EOF > $chrony_conf
	#----------------------------------------------------------------------
	# /etc/chrony.conf
	# Automatically created by rc920.chrony
	#----------------------------------------------------------------------
	keyfile /etc/chrony.keys
	driftfile $chrony_log/chrony.drift
	rtcfile $chrony_log/chrony.rtc
	commandkey 1
	allow
	cmdallow localhost
	cmdallow 127.0.0.1
	maxupdateskew 100
	local
EOF
        [ "$CHRONY_BIOS_TIME" != local ] && echo rtconutc >> $chrony_conf
        cp $chrony_conf $chrony_conf.first
        echo "initstepslew 30 $serverlist" >> $chrony_conf.first
        cat $chrony_serverlist >> $chrony_conf.first
    fi
    # use initstepslew to correct time difference grater than 30sec. at once
    # only during first ip-up event (see chrony ip-up and ip-down scripts)
    # start chrony in first ip-up event or now if there is no ip_up_event
    case $ip_up_events in
    yes) chronyd -r ;;
    no)
        # for eth only routers use initstepslew as default
        # on eth only routers (=static ips, no ip-up events) add time server
        # list right away to chrony sources
        mv $chrony_conf.first $chrony_conf
        chronyd -r
        {
        sleep 5    # don't remove, chronyd isn't really up yet

        /usr/bin/chronyc >/dev/null <<-EOF
	password dummy
	online
EOF
        } &
    ;;
    esac

    case $CHRONY_TIMESERVICE in
        yes) srv868 ;;
    esac

    end_script
;;
esac
