#!/bin/sh
#
# ALU ICT8000 Upgrade init script -- start
#
# chkconfig: 345 90 01
#
# description:
#
#	This script update the crontab entry for user root to add a new entry
#	for the update process.
#
# Changelog:
#	- Fri Jan 7 2011 - Guillaume Catto <guillaume.catto@alcatel-lucent.com>
#	* Fix crms00284851: script creation
#
#       - Thu Jan 24 2013 - Jerry ZHOU <jerry.zhou@alcatel-lucent.com>
#       * PR261951: Reduce time of unavailability during boot time

unset start_time
polbin=/usr/sbin/chkupdate
cron_bin=/usr/sbin/cronupdt
crond=/usr/sbin/crond
startup_step=/tmp/step

# define_aliases
#---------------
# ARGS:
#   <none>  define macros that are used in the different functions
define_aliases() {
    local -
    set +x

    esc=$'\x1b'
    attr="$esc[7m"
    errorattr="$esc[31;7m"
    default="$esc[0m"
    ROOT=${ROOT:-}
    HOME_ADMIN_SYS="/home/admin"
    HOME_ADMIN_UPG="/system/home/admin"

    alias DEFAULT_DEBUG_OFF="local ldebug=1"
    alias defPS4="PS4=\${PS4_TPLT:-\"\$esc[\$((\$\$%6+31))m+(\$\$)\$FUNCTION:\$esc[0m \"}"
    alias IN_DEBUG="([[ -f $HOME_ADMIN_SYS/.debug.upgrade ]] || [[ -f $HOME_ADMIN_UPG/.debug.upgrade ]])"
    alias IN_NODEBUG="([[ -f $HOME_ADMIN_SYS/.nodebug.upgrade ]] || [[ -f $HOME_ADMIN_UPG/.nodebug.upgrade ]])"

#PR261951 jerryzh+
    if [[ -f $HOME_ADMIN_SYS/.debug.upgrade ]] || [[ -f $HOME_ADMIN_UPG/.debug.upgrade ]]; then
    	alias ENTER_FUNCTION="
    	[[ \${ldebug:-0} -eq 1 ]] && { local -; set +x; }
    	: \"---- ENTER_FUNCTION\"
    	: \"\$esc[4mfrom \${EF_function:-unknown()}\${default}\"
    	local OIFS=\$IFS IFS=\",\"
    	local EF_function=\"\$FUNCTION(\"\"\$*\"\")\"
    	IFS=\$OIFS
    	local _in_xflag=\${-//[^x]}
    	local PS4
    	defPS4
    	if IN_DEBUG; then
    	    local -; set -x
    	fi
    	if IN_NODEBUG; then
    	    local -; set +x
    	fi
    	(IFS=\",\"; : \"\${attr}START \$EF_function\${default}\")
    	: \"---- ENTER_FUNCTION END\"
    	"
# CRMS00312748 END
#   NOTE:
#       - RETURN macro in a short 'if' has to be like
#           [[ -f <file> ]] && { RETURN <ret_code>; }
#         The '{' and '; }' are VERY VERY important
#       - The ret_code has to be an immediate or a variable other than "$?".
#         "$?" is destroyed by the RETURN macro.
    	alias RETURN="
    	: \"\${attr}LEAVING \$EF_function\${default}\"
    	if [[ \"\$_in_xflag\" == \"\${-//[^x]}\" ]]; then
    	    [[ -n \"\$_in_xflag\" ]] && set -x || set +x
    	fi
    	return"
    else
        alias ENTER_FUNCTION=""
        alias RETURN="return"        
    fi
#PR261951 jerryzh-
}
define_aliases

# Support functions
add_time() {
	local tmp=0
	local spare_mins=0
	local spare_hours=0
	local FUNCTION=add_time
	ENTER_FUNCTION

	: Split parameter ${ADMCFG_UPDATE_TIME_START}
	local split_hours=${ADMCFG_UPDATE_TIME_START%%:*}
	local split_mins=${ADMCFG_UPDATE_TIME_START##*:}

	if [ ${shift_time} -gt 0 ]; then
		: Split parameter ${shift_time} 
		spare_mins=$(expr $shift_time % 60)
		spare_hours=$(expr $shift_time - $spare_mins)
		spare_hours=$(expr $spare_hours / 60)

		: Add both times together 
		split_mins=$(expr $split_mins + $spare_mins)
		if [ $split_mins -gt 59 ]; then
			tmp=$(expr $split_mins / 60)
			split_hours=$(expr $split_hours + $tmp)
			split_mins=$(expr $split_mins % 60)
		fi
		split_hours=$(expr $split_hours + $spare_hours)

		: Limit hours to 24
		split_hours=$(expr $split_hours % 24)
	fi

	: Generate new start time
	[ ${#split_mins} -lt 2 ] && split_mins="0${split_mins}"
	[ ${#split_hours} -lt 2 ] && split_hours="0${split_hours}"
	start_time="${split_hours}:${split_mins}"

	RETURN
}

generate_random_time() {
	local FUNCTION=generate_random_time
	ENTER_FUNCTION

	if [ $ADMCFG_UPDATE_TIME_DELTA -eq 0 ]; then
		shift_time=0
	else
		shift_time=$(expr $RANDOM % $ADMCFG_UPDATE_TIME_DELTA)
	fi

	RETURN
}

# Display some help about this script
usage() {
    echo "Usage: $0 {start|stop}"
}

start() {
    local current_step=$(cat ${startup_step})
    local FUNCTION=start
    ENTER_FUNCTION

	: Check steps ${current_step}/5
    [ ${current_step} -lt 5 ] && return 0

	: Modify the cron configuration with values
	ADMCFG_UPDATE_TIME_ENABLE=$(CLISettings get CURRENT "ADMCFG_UPDATE_TIME_ENABLE")
	ADMCFG_UPDATE_TIME_START=$(CLISettings get CURRENT "ADMCFG_UPDATE_TIME_START")
	ADMCFG_UPDATE_TIME_DELTA=$(CLISettings get CURRENT "ADMCFG_UPDATE_TIME_DELTA")

	logger -t upgrade -p local0.NOTICE "Modifying update cron configuration with values $ADMCFG_UPDATE_TIME_ENABLE, $ADMCFG_UPDATE_TIME_START and $ADMCFG_UPDATE_ENABLE"
	if [[ "${ADMCFG_UPDATE_TIME_ENABLE}" != "true" ]]; then
		: Removing cron entry for ${polbin}
		$cron_bin -r $polbin
		ret=$?
	else
		generate_random_time
		add_time
		: Adding cron entry for ${polbin} at ${start_time}
		$cron_bin -t $start_time $polbin
		ret=$?
	fi

	[[ $ret != 0 ]] && logger -s -t upgrade -p local0.ERR "$0 hook failed"
	RETURN $ret
}

# In case we want to stop/reboot
# cron entry should be removed
stop() {
	local FUNCTION=stop
	ENTER_FUNCTION

	$cron_bin -r $polbin
	RETURN $?
}

# See how we were called.
case "$1" in
    start)
		echo "entering $0 ..."
        start
		RETVAL=$?
		echo "leaving $0 ..."
        ;;
	reload)
		start
		RETVAL=$?
		;;
    stop)
		stop
		RETVAL=$?
        ;;
    *)
        usage
        RETVAL=$?
        ;;
esac


exit $RETVAL
