#!/bin/sh
set -e
# postinst for chrony John Hasler jhasler@debian.org 2000-2005
# Any possessor of a copy of this program may treat it as if it
# were in the public domain.  I waive all rights.

install-info --quiet --description="Set your clock from the Net" \
	     --section General "General Commands" \
             /usr/share/info/chrony.info

if [ -x /usr/bin/update-menus ] ; then 
    /usr/bin/update-menus 
fi

# If chrony.keys is the dist file generate a random passord and put it there.

if [ "`cat /etc/chrony/chrony.keys`" = "1 password_here" ] ; then
    PASSWORD=`head -c 8 /dev/urandom | tr '\0-\377' 'a-zA-Z0-9a-zA-Z0-9a-zA-Z0-9a-zA-Z0-9@@@@####'`
    echo "1 $PASSWORD" > /etc/chrony/chrony.keys
    MAILPASSWORD="The password for chronyc is in /etc/chrony/chrony.keys."
fi

update-rc.d chrony defaults 83 >/dev/null

# If chrony.conf is the dist file figure out whether or not we are on UTC
# and put an appropriate line in chrony.conf.

if [ -f /etc/default/rcS -a -f /etc/chrony/chrony.conf ] ; then 
    if tail -1 /etc/chrony/chrony.conf | grep -q '^# POSTINSTMARKER$' ; then
	. /etc/default/rcS
	case "$UTC" in
	    no|"") echo "# rtconutc" >> /etc/chrony/chrony.conf
                   MAILUTC="Chrony has been configured to assume that your real-time clock is on local time.
If this is not correct edit /etc/chrony/chrony.conf.  The comments explain
what to do."
		   ;;
	    yes)   echo "rtconutc" >> /etc/chrony/chrony.conf
                   MAILUTC="Chrony has been configured to assume that your real-time clock is on UTC time.
If this is not correct edit /etc/chrony/chrony.conf.  The comments explain
what to do."
		   ;;
	    *)     echo "# Can't tell how your clock is set: assuming local time." >> /etc/chrony/chrony.conf
                   MAILUTC="Can't tell how your clock is set: assuming local time.
If this is not correct edit /etc/chrony/chrony.conf.  The comments explain
what to do."
		   ;;
	esac
    fi
fi

invoke-rc.d chrony start

if [ \( "$MAILPASSWORD" -o "$MAILUTC" \) -a \( -x /usr/bin/mail \) ] ; then
    /usr/bin/mail -s "Chrony" root <<EOF

$MAILPASSWORD

$MAILUTC

In the chrony.conf included in this package:

Chrony has been configured on the assumption that you are using either a dialup
connection or a PPPoE DSL connection.  It will be brought online when PPP 
comes up and offline when it goes down.  If this is not correct you should edit
/etc/chrony/chrony.conf.  The comments explain what to do.  For more 
information on configuring Chrony use the command  'info chrony'.

You can also change the default time servers in  /etc/chrony/chrony.conf.

Updating of the real-time clock has been enabled but some systems that use
either the genrtc driver or have HPET hardware clocks have problems.  To
disable real-time clock updating edit /etc/chrony/chrony.conf.  The
comments explain what to do.

EOF

fi

exit 0
