#!/bin/sh

set -e

if [ -r /etc/default/ntpdate ]; then
	. /etc/default/ntpdate
fi

if [ "$NTPDATE" = no -o "$NTPDATE" = "" ]; then
    exit 0
fi

if [ "$NTPDATE_USE_NTP_CONF" = yes ]; then
	for f in /var/lib/ntp/ntp.conf.dhcp /etc/ntp.conf /etc/openntpd/ntpd.conf; do
		if [ -r "$f" ]; then
			file=$f
			break
		fi
	done
	if [ -n "$file" ]; then
		NTPSERVERS=$(sed -rne 's/^(servers?|peer)[[:space:]]+(-[46][[:space:]]+)?([-_.:[:alnum:]]+).*$/\3/p' "$file" | grep -v '^127\.127\.') || [ $? -le 1 ]
	fi
elif [ -r /var/lib/ntpdate/default.dhcp ]; then
	. /var/lib/ntpdate/default.dhcp
fi

/usr/local/sbin/ntpdate $NTPOPTIONS "$@" $NTPSERVERS

hwclock --localtime --systohc
