# drtc_functions
#	This file contains functions to be used by most or all
#	shell scripts in the /opt/nec/drtc.
#
# COPYRIGHT (C) NEC CORPORATION 2009, 2010
# Rev.7

MODEFILE=/etc/adjtime
DRTCFILE=/proc/driver/rtc
#DRTCFILE=./dbg_rtc;	 					#debug
#LOGFILE=./dbg_drtc.log; 					#debug
#LOGFILE=/opt/nec/drtc/dbg_drtc.log;				#debug
#CONF_FILE=hasdrtc.conf;					#debug
CONF_FILE=/etc/hasdrtc.conf
PROGRAM=/opt/nec/hasdrtc/correctdrtc
_TRUE=0
_FALSE=1

print_log () {
	if [ "$1" = DBG ]; then return; fi
	MESSAGE2="[$$] $2 exec_count($count) $UTC date($CSYSYMD $CSYSHMS) drtc($DRTCYMD $DRTCHMS)"
	/sbin/initlog -n $MESSAGE1 -s "$MESSAGE2"
#	echo "$MESSAGE1 $MESSAGE2" >> $LOGFILE;			#debug
}

NUM_MINUS=1
NUM_ZERO=2
NUM_PLUS=3
NUM_ERR=4
_chk_numeric () {
	expr 0 + $1 >/dev/null 2>&1
	if (($? < 2)); then
		if (($1 > 0)); then
			return $NUM_PLUS
		elif (($1 < 0)); then
			return $NUM_MINUS
		elif (($1 == 0)); then
			return $NUM_ZERO
		fi
	fi
	return $NUM_ERR
}

get_param () {
        if [ ! -e $CONF_FILE ]; then return; fi
	local _STR=`/usr/bin/perl -lnae \
		'if (/^'$1'/) {$T=$_;} END {print $T;}' $CONF_FILE \
		| cut -d"=" -f2 | sed "s/^ *//g"` > /dev/null 2>&1
	if [ ! $? ];  then return; fi

	eval "VAL=$_STR"			
	_chk_numeric $VAL
	local RESULT=$?
	case $2 in
	-ZERO_PLUS )
		if [ $RESULT = $NUM_PLUS -o $RESULT = $NUM_ZERO ]; then eval "$1=$VAL"; fi;;

	-PLUS      )
		if [ $RESULT = $NUM_PLUS ]; then eval "$1=$VAL"; fi;;
	esac
}

get_systime () {
        UTC=""
        if /bin/grep ^UTC$ $MODEFILE > /dev/null 2>&1; then UTC="--utc"; fi
	local _TIME=`date $UTC '+%Y-%m-%d %H:%M:%S %s'`
#	local _TIME=`cat dbg_systime`; 				#debug
	CYEAR=${_TIME:0:4}
	CMONTH=${_TIME:5:2}
	CDATE=${_TIME:8:2}
	CSYSYMD=${_TIME:0:10}
	CSYSHMS=${_TIME:11:8}
	CALLSEC=${_TIME:20}
}

diff_seconds () {
        TALLSEC=`date -d "$CYEAR/$CMONTH/$CDATE 23:59:59" $UTC '+%s'`
        DIFFSECONDS=$(($TALLSEC - $CALLSEC + 1))
}

get_drtctime () {
	local _DTIME=`/usr/bin/perl -lnae 'chomp; if (/rtc_date/) {$date=@F[2];} \
	    if (/rtc_time/) {$time=@F[2]} END{printf("%s %s", $date, $time);}' $DRTCFILE`
        DRTCYMD=`echo $_DTIME | cut -d" " -f1 | sed "s/ //g"`
        DRTCHMS=`echo $_DTIME | cut -d" " -f2`
}

drtc_exe () {
	local NR_RETRY=$2
	local SLEEP_RETRY=$3
	count=0
	while ((count < NR_RETRY));do
		get_systime
		get_drtctime
		if [ "$CSYSYMD" = "$DRTCYMD" ]; then
			print_log INF "drtc OK."
			return
		fi
		print_log DBG "drtc($1)"
		if [ "$1" = "-WRITE" ]; then 
			/sbin/hwclock --systohc
			if ((count == 0)); then print_log INF "write drtc."; fi

		else
			 echo -n "."
		fi
		sleep $SLEEP_RETRY
		(( count += 1 ))
	done
	print_log ERR "drtc check."
}

sleep_cnt () {
        count=$2
        while ((count >= 0)); do
		echo -ne "\r${1}          \b\b\b\b\b\b\b\b\b\b${count}s."
		sleep 1
                ((count -= 1))
        done
}

function mkcrontab () {
	echo "# COPYRIGHT (C) NEC CORPORATION 2010"
	
	if /bin/grep ^UTC$ $MODEFILE > /dev/null 2> /dev/null
	then
		launch=`date --utc -d '2012/2/29 00:00:00' +'%s'`
		/usr/bin/perl -e '@t=localtime('$launch'); \
		printf("0 %2d * * * root '${PROGRAM}'\n", @t[2])'
	else
		echo "0 0 * * * root ${PROGRAM}"
	fi
}


function chk_timeout () {
	[ -f /boot/grub/grub.conf ] || return 0

	GRUB_TIMEOUT=0
	GRUB_TIMEOUT=`grep -e "^ *timeout=" /boot/grub/grub.conf | head -n 1 | sed 's/timeout=\([0-9]*\).*$/\1/'`
	DEF_TIMEOUT=$(( $GRUB_TIMEOUT + 900 ))
	if [ $DELAY_REBOOT -ge $DEF_TIMEOUT ];
	then
		return 0;
	fi
	/sbin/initlog -n hasdrtc -s "DELAY_REBOOT is too short."
	return 1
}

function chk_cron () {
	/sbin/service crond status > /dev/null 2>&1 && return 0
	/sbin/initlog -n hasdrtc -s "crond does not work."
	return 1
}

function chk_cron_boot () {
	CHKCFG=`LANG=C /sbin/chkconfig --list crond | sed -e "s/^.*3:\([a-z]\+\).*5:\([a-z]\+\).*$/\1\2/"`
	[ $CHKCFG = "onon" ] && return 0
	/sbin/initlog -n hasdrtc -s "crond is not started automatically."
	return 1
}

function chk_crontab () {
	TMPFILE=`mktemp`
	mkcrontab > $TMPFILE
	diff $TMPFILE /etc/cron.d/correctrtc.cron > /dev/null 2>&1
	RETVAL=$?
	rm -f $TMPFILE
	if [ $RETVAL -ne 0 ];then
		/sbin/initlog -n hasdrtc -s "correctrtc.cron is wrong."
	fi
	return $RETVAL
}


function chk_package () {
	rpm -V gemini_hasdrtc-1.00.00-7 > /dev/null 2>&1 && return 0
	/sbin/initlog -n hasdrtc -s "The package of gemini_hasdrtc is modified."
	return 1
}

function check_config () {
	RETVAL=0
	case $1 in
	"boot")
		if ! chk_cron_boot;then
			RETVAL=1
		fi
		if ! chk_timeout;then
			RETVAL=1
		fi
		if ! chk_package;then
			RETVAL=1
		fi
		return $RETVAL
		;;
	"cron")
		if ! chk_cron;then
			RETVAL=1
		fi
		if ! chk_cron_boot;then 
			RETVAL=1
		fi
		if ! chk_crontab;then 
			RETVAL=1
		fi
		if ! chk_timeout;then 
			RETVAL=1
		fi
		if ! chk_package;then 
			RETVAL=1
		fi
		;;
	esac
	return 1
}

function log_chkresult () {
	if [ $1 -eq 0 ]
	then
		/sbin/initlog -n hasdrtc -s "RTC Adjuster works normaly."
	else
		/sbin/initlog -n hasdrtc -s "Configuration of RTC Adjuster is incorrect."
	fi
}

