:
#	@(#) asktime.sh 23.4 91/11/21 
#
#	Copyright (C) 1986-1991 The Santa Cruz Operation, Inc.
#		All Rights Reserved.
#	The information in this file is provided for the exclusive use of
#	the licensees of The Santa Cruz Operation, Inc.  Such users have the
#	right to use, modify, and incorporate this code into other products
#	for purposes authorized by the license agreement provided they include
#	this notice and the associated copyright notice with any such product.
#	The information in this file is provided "AS IS" without warranty.
#
#
#***	asktime -- prompt user for date and time, set things accordingly
#
#	If a real-time clock exists, it is used as the reference,
#	otherwise the system clock is used.
#
#	We (silently) force the real-time clock and the system clock
#	to agree.  This is done even if the user does not change the
#	things from the reference.
#
#	The method is basically as follows (unfortunately shell script
#	limitations make the actual implementation rather convoluted):
#
#	'real-time clock'	means clock/calendar unaffected by power-downs,
#				etc.
#	'system clock'		means clock kept track of by kernel, initialized
#				at boot time from date on super-block.
#
#	ref=`setclock`			reference comes from real-time clock
#	date $ref > /dev/null		sync system clock to real-time
#	read new			user override
#	date $new			...
#	setclock `date` > /dev/null	sync real-time clock to override
#
PATH=/bin:/usr/bin
DEFAULT=/etc/default/boot


cleanup() {
	[ $OASH ] && {
		oadeinit
		clear
	}	
}

# find out if this script is run via oash
eval oainit 2>/dev/null && OASH=1 || OASH=

[ $OASH ] && {
	werase
	wrefresh
	dateline $0
	# open main window
	wmopen 1 21 80 4 0

}


# If autobooting then timeout after 30 seconds
[ "$1" = -a ] && {
	timeout=`sed -n "/^TIMEOUT=[0-9]*$/s/TIMEOUT=//p" $DEFAULT`
	[ "$timeout" ] || timeout=30
}
rtc=no

# If we have a real-time clock, synchronize system clock to it.
cat /dev/clock > /dev/null 2>&1 && {
	rtc=yes
	date `/etc/setclock` > /dev/null
}

# Start up a timeout daemon

[ "$timeout" ] && {
	[ $timeout -lt 2 ] && timeout=2
	(sleep $timeout; kill -14 $$ > /dev/null 2>&1 ; echo "") &
}
[ $OASH ] && {
   while :
   do
	wmopen 2 5 60 10 10
	wmtitle "Configure Current Time"
	echo "\nCurrent System Time is `date`"
	echo "Enter new time ([yymmdd]hhmm): \c"
   	read date
	[ "$date" ] && {
   		date=`/lib/cvtdate $date 2>/dev/null` && werase && \
			echo `date $date` && sleep 1 && break
		error "Error: Invalid time. Please try again"
		wmove 1
		werase 2
		wmclose 2
	} || break
   done
   

} || {

   echo "\nCurrent System Time is `date`\nEnter new time ([yymmdd]hhmm): \c"
   while	read date
	[ "$date" ]
   do	date=`/lib/cvtdate $date` && date $date && break
	echo "Try again: \c"
   done

}

[ "$rtc" = yes ] && /etc/setclock `date +\%m\%d\%H\%M\%y` > /dev/null

cleanup
# Kill daemon before we exit, exec eliminates ``killed'' error message
[ "$timeout" ] && exec kill $! > /dev/null 2>&1
