#!/bin/bash
#
# License: Copyright 2009 SpinetiX S.A. This file is licensed
#          under the terms of the GNU General Public License version 2.
#          This program is licensed "as is" without any warranty of any
#          kind, whether express or implied.
#
# Copyright 1999-2003 MontaVista Software, Inc.
# Copyright 2002, 2003, 2004 Sony Corporation
# Copyright 2002, 2003, 2004 Matsushita Electric Industrial Co., Ltd.
#
### BEGIN INIT INFO
# Required-Start:
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 5
# Default-Stop:
# Short-Description: Fixes NTP drift file
# Description: Fixes NTP drift file it it was generated by off-frequency kernel 
### END INIT INFO
# chkconfig: S 80 0

# Init script information
INIT_NAME=ntp-drift-fixup
DESC=""

DRIFTFILE=/var/lib/spx-ntp/drift

case "$1" in
    start)

        echo -n "Fixing NTP drift value... "

	if [ -s "$DRIFTFILE" ]; then
	    drift="$(< "$DRIFTFILE" )"
	    idrift="${drift%%.*}"
	    if [ "$idrift" -lt -80 ]; then
		drift=$(( idrift + 115 ))
		echo "$drift" > "$DRIFTFILE"
		echo "done, new drift is $drift"
	    else
		echo "done, no correction necessary"
	    fi
	else
	    echo "done, no drift file"
	fi

	# Be sure the new value is on stable storage
	sync

	# We just need to run once at boot
	initdconfig --del "$INIT_NAME"

    ;;
    stop)
    ;;
esac


