#!/bin/sh

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Get service config
if [ -f /etc/sysconfig/rdate ]; then
	. /etc/sysconfig/rdate
fi

# Check that networking is up.
if is_yes "${NETWORKING}" && is_yes "${SET_TIME}" && [ -n "${RDATE_SERVERS}" ]; then
	/usr/bin/rdate -s ${RDATE_SERVERS}
	if [ $? -eq 0 -a -x /sbin/hwclock ] && is_yes "${SYNC_HWCLOCK}"; then
		# Set the system clock.
		ARC=0
		SRM=0
		UTC=0
		
		if [ -f /etc/sysconfig/clock ]; then
			. /etc/sysconfig/clock
		
			# convert old style clock config to new values
			if [ "${CLOCKMODE}" = "GMT" ]; then
				UTC=true
			elif [ "${CLOCKMODE}" = "ARC" ]; then
				ARC=true
			fi
		fi
		
		if grep "system serial" /proc/cpuinfo | grep -q MILO ; then
			ARC=true
		fi
		
		CLOCKFLAGS="--systohc"
		
		if is_yes "$UTC" ; then
			CLOCKFLAGS="$CLOCKFLAGS --utc"
		else
			CLOCKFLAGS="$CLOCKFLAGS --localtime"
		fi
		
		if is_yes "$ARC" ; then
			CLOCKFLAGS="$CLOCKFLAGS -A"
		fi
		
		if is_yes "$SRM" ; then
			CLOCKFLAGS="$CLOCKFLAGS -S"
		fi
		
		/sbin/hwclock $CLOCKFLAGS
	fi
fi
