#!/bin/sh

#
# ALU ICT8000 Upgrade init script -- start
#
# chkconfig: 345 80 --
# description:  This is the starting step of the ALU Upgrade application for the ICT8000 phones.
#
# Changelog:
#	- Thu March 11 2010 - Guillaume CATTO <guillaume.catto@alcatel-lucent.com>
#	* Fix crms00219008: upgrade process reboots the phone if no upgrade URL has been set
#
#	- Fri March 12 2010 - Guillaume CATTO <guillaume.catto@alactel-lucent.com>
#	* Fix crms00219242: in upgrade mode, we cannot trust parameters in CURRENT settings
#
#	- Fri March 19 2010 - Guillaume CATTO <guillaume.catto@alcatel-lucent.com>
#	* Fix crms00221058: remove old profile's checksum when coming back from the upgrade partition
#	*					so that the upgrade can be restarted and finishes successfully
#
#	- Mon April 12 2010 - Guillaume Catto <guillaume.catto@alcatel-lucent.com>
#	* Fix crmsxxxxxxxx: add the support of signed RPMs, key.xml and signed index files
#
#	- Mon April 20 2010 - Thomas CREMEL <thomas.cremel@alcatel-lucent.com>
#	* Fix crmsxxxxxxxx: add the support for kernel option : ictouch = restore >> the upgrade partition has an issue
#
#	- Fri April 23 2010 - Guillaume Catto <guillaume.catto@alcatel-lucent.com>
#	* Fix crms00219212: use dwl command to force an upgrade to a specific version
#
#   - Wed May 18 2010 - Guillaume Catto <guillaume.catto@alcatel-lucent.com>
#   * Export a new function to cleanup upgrade directory
#
#   - Thu May 19 2010 - Guillaume Catto <guillaume.catto@alcatel-lucent.com>
#   * Modify rules to detect if the upgrade is runing and whether it is locked or not
#
#   - Wed Jun 16 2010 - Guillaume Catto <guillaume.catto@alcatel-lucent.com>
#   * Fix crms00238705: do not delete .serversip.bak configuration file while doing the cleanup
#
#   - Wed Jun 23 2010 - Guillaume Catto <guillaume.catto@alcatel-lucent.com>
#   * Fix crms00238623: request a reboot in case the upgrade was in step 2 and nothing has to be done
#   * Add a reason and a owner to the reboot function
#
#   - Tue Jul 27 2010 - Guillaume Catto <guillaume.catto@alcatel-lucent.com>
#   * Fix crms00246998: in static dhcp mode, send alse send the protocol used with the server's IP/Name
#
#   - Wed Sep 01 2010 - Guillaume Catto <guillaume.catto@alcatel-lucent.com>
#   * Fix crms00256623: truncate upgrade report file if they are too big
#
#   - Wed Sep 08 2010 - Guillaume Catto <guillaume.catto@alcatel-lucent.com>
#   * Fix crms00258187: do a cleanup if an update has been started but input parameters are wrong
#
#	- Mon Oct 25 2010 - Guillaume Catto <guillaume.catto@alcatel-lucent.com>
#	* Fix crms00259524: manage webapp deployment mode setting
#
#	- Tue Nov 16 2010 - Guillaume Catto <guillaume.catto@alcatel-lucent.com>
#	* Fix crms00273199: add a new flag to detect needed upgrade
#
#	- Tue Jan 25 2011 - Guillaume Catto <guillaume.catto@alcatel-lucent.com>
#	* Fix crms00286989: Modify the "clean" option behavior
#
#	- Fri Jan 28 2011 - Guillaume Catto <guillaume.catto@alcatel-lucent.com>
#	* Fix crms00289400: in case of error in upgrade process, keep the "in upgrade" status
#
#	- Tue Mar 15 2011 - Guillaume Catto <guillaume.catto@alcatel-lucent.com>
#	* Fix crms00294436: Create 2 new directories to install customization RPMs
#
#	- Thu Apr 28 2011 - Guillaume Catto <guillaume.catto@alcatel-lucent.com>
#	* Fix crms00310043: add function to check ibl/u-boot version's from kernel command line
#
#   - Thu Aug 16 2012 - Jerry ZHOU <Jerry.zhou@alcatel-lucent.com>
#   * Fix crms00390845: Fix not display upgrade url information.

##################################
# Source external scripts        #
##################################
. /usr/lib/upgrade/rpmdbbackup-addon.sh

workdir=/config/upgrade
statusfile=$workdir/status
rebootset=/usr/sbin/setnextboot
debug_output=/dev/null

echo "entering $0 ..."

RETVAL=0
UPGD_PART=0

reset_upgrade() {
    local FUNCTION=reset_upgrade
    ENTER_FUNCTION
    local rpm_name=$1

    echo "Upgrade process has encountered an error while flashing partition $rpm_name"
    rpm -e --nodeps $rpm_name

    RETURN
}

start() {
    local FUNCTION=start
    ENTER_FUNCTION

    #Check for "restore" flag in kernel command line
    : "${attr}Check for \"restore\" flag in kernel command line${default}"
    grep "ictouch=restore" /proc/cmdline > /dev/null
    if [[ $? == 0 ]]; then
        : "${attr}Set next reboot to SYSTEM partition${default}"
	    $rebootset SYSTEM
	    reset_upgrade "upgrade_image"
        # Fix crms00371518: full cleanup when detecting upgrade partition is loss
	    cleanup 1
    fi

    # Get application manager status. if not running do not start script in background
    get_application_manager_status
    if [[ $? == 1 ]]; then
	# Start script in background, redirect STDERR and STDOUT to debug_output
	child_pid_status
	if [[ $? == 0 ]]; then
            #crms00386821 jerryzh+
            : "${attr}Start script in background, redirect STDERR and STDOUT to \"$debug_output\"${default}"
	    ($process_management "wait_presentation" > $debug_output 2>&1 &)
    	    RETVAL=$?
            #crms00386821 jerryzh-
	fi
    else
        : "${attr}ApplicationManager not start, start script in foreground${default}"
	$process_management
	RETVAL=$?
    fi

    RETURN
}

stop() {
    local FUNCTION=stop
    ENTER_FUNCTION
    # In case no background script is running, do nothing
    child_pid_status
    [[ $? == 0 ]] && return 0

    # Else kill script and cleanup
    kill QUIT $CHLDPID
    RETURN $?
}

# Compute the status of the upgrade
get_upgd_status() {
    local FUNCTION=get_upgd_status
    ENTER_FUNCTION

    [ ! -e $statusfile ] && { RETURN 0; }

    source $statusfile
    lastlevel=$STEP

    : "${attr}==== lastlevel: $lastlevel ====${default}"
    case "$lastlevel" in
	pre0|6) RETVAL=0 ;;
	*) RETVAL=1 ;;
    esac

    RETURN
}

#crms00390845 jerryzh+
# get seting value of upgrade file
get_setting_value "ENETCFG_UPGRADE_FILE"
#crms00390845 jerryzh-

# See how we were called.
case "$1" in
    start)
	echo "ENETCFG_UPGRADE_FILE =" $ENETCFG_UPGRADE_FILE	    
        start
        ;;
    stop)
	echo "ENETCFG_UPGRADE_FILE =" $ENETCFG_UPGRADE_FILE
        stop
        ;;
    restart)
	echo "ENETCFG_UPGRADE_FILE =" $ENETCFG_UPGRADE_FILE
        stop
	cleanup 1
	start
        ;;
    check)
	echo "ENETCFG_UPGRADE_FILE =" $ENETCFG_UPGRADE_FILE
	$process_management "check"
	RETVAL=$?
	;;
    status)
	echo "ENETCFG_UPGRADE_FILE =" $ENETCFG_UPGRADE_FILE
	get_upgd_status
	[ $RETVAL -eq 1 ] && echo "Upgrade is running" || echo "Upgrade is not running"
        ;;
    clean)
	echo "ENETCFG_UPGRADE_FILE =" $ENETCFG_UPGRADE_FILE
# +++ crms00286989
	cleanup 1
# --- crms00286989
        RETVAL=$?
        ;;
    current_version)
        current_version
        RETVAL=$?
        ;;
    next_version)
        next_version
        RETVAL=$?
        ;;
# +++ crms00378922
    newdeploymode)
	# called when detecting new webapp deployment mode setting change
	# treatment move here from WEBAPP_DEPLOYMENT_MODE post hook
	get_upgd_status
	if [[ $RETVAL -eq 0 ]]; then
		echo "Upgrade has been informed for webapp deployment mode change"
		cleanup 1
	fi
       	RETVAL=0
        ;;
# --- crms00378922
    *)
        echo $"Usage: $0 {start|stop|restart|check|status|clean|current_version|next_version}"
        ;;
esac

echo leaving $0 ...

exit $RETVAL
