#!/bin/sh
############
# CHANGELOG
#===========
# 2013/04/07    crms00429511    Qianjun Zhang
#               NOE enable(Switch to NOE,can't download lanpbx.cfg,but it pop up can't retrieve a good noe binary from TFTP server,err code 0)
# 2013/02/18    crms00429205    Qianjun Zhang
#               NOE enable(remove factory control in the initializaition script)
# 2013/02/18    PR442574    Qianjun Zhang
#               NOE enable(this script must after the network process)
#
################

source /etc/init.d/rc.config

minsystemsize=280
UPGD_STATUS=/usr/sbin/upgd_status
NOE_STOREPATH=/data/noe_upgrade
NOE_STOREBIN=$NOE_STOREPATH/noe_image.rpm
transformershell=/usr/lib/upgrade/noeimage_download.sh
rebootshell=$(which reboot)
setnextbootshell=$(which setnextboot)
TFTP_ADDRESS0=$(CLISettings get CURRENT ENETCFG_NOE_TFTP0_ADDR)
TFTP_ADDRESS1=$(CLISettings get CURRENT ENETCFG_NOE_TFTP1_ADDR)
TFTP_PORT=$(CLISettings get CURRENT ENETCFG_NOE_TFTP_PORT)
restoredir=/config/restore/rpm


#==============================================
#check the runmode switch is triggered
#==============================================
pre_check_runmode() {
    runmodeswitch=`CLISettings get CURRENT ADMCFG_NOE_PROTOCOL | tr [A-Z] [a-z]`
    dhcprunmode=

    if [[ "$runmodeswitch" == "true" ]];then
        dhcprunmode=`CLISettings get DHCP ADMCFG_NOE_PROTOCOL | tr [A-Z] [a-z]`
        if [[ a"$dhcprunmode" == a"true" ]];then
            logger -t upgrade-noe -p emerg "NOE protocol enable by DHCP,Switching to NOE triggered"
            displaypopup -t 4 "NOE protocol enable by DHCP,Switching to NOE triggered"
        else
            logger -t upgrade-noe -p emerg "NOE protocol enable by LOCAL setting,Switching to NOE triggered"
            displaypopup -t 4 "NOE protocol enable by Local setting,Switching to NOE triggered"
        fi
        return 0
    else
        return 1
    fi
}

#==============================================
#check the SYSTEM partition size is larger than 280M
#==============================================
pre_check_system_volumn_size() {

    systemsize=$(ubi-wrapper ubinfo ubi0:SYSTEM | awk '/^Size:/{ printf("%d", substr($4,2)/1024/1024)}')

    if [[ $systemsize -lt $minsystemsize ]];then
        logger -t upgrade-noe -p emerg "SYSTEM partition size is not enough $systemsize, quit upgrade!"
        return 1
    fi

    return 0
}

#==============================================
#check the flash page size is 2048 and block size is 128
#==============================================
pre_check_flash_type() {
    flashpagesize=`ubinfo -d 0|awk '/^Minimum input\/output unit size:/{print $5}'`
    if [[ $flashpagesize -ne 2048 ]];then
        logger -t upgrade-noe -p emerg "flash page size $flashpagesize is not 2048, quit upgrade to NOE!"
        return 1
    fi
    
    flashblocksize=`mtdinfo /dev/mtd0|awk '/^Eraseblock size:/{print $5}'|sed 's/\.[0-9]\+//'`
    if [[ $flashblocksize -ne 128 ]];then
        logger -t upgrade-noe -p emerg "flash block size $flashblocksize is not 128, quit upgrade to NOE!"
        return 1
    fi

    return 0
}

#==============================================
#if in normal upgrade state,we skipped
#==============================================
pre_check_upgrade_state() {

    if [ -x $UPGD_STATUS ]; then                                                                    
        $UPGD_STATUS >/dev/null 2>&1                                                                
        if [ $? -eq 1 ]; then
            logger -t upgrade-noe -p emerg "normal upgrade process in running, quit upgrade to NOE!"
            return 1
        fi   
    fi

    return 0
}

#==============================================
#if the base RPMs files is not right ,we skipped
#==============================================

pre_check_restore_possibility() {
    list1="$(sort $restoredir/.base_rpmlist)"
    list2="$(cd $restoredir && { ls -A *.rpm | sort; })"

    if [[ "$list1" != "$list2" ]]; then
        logger -t upgrade-noe -p local0.EMERG "Restore pool is not complete ! cannot upgrade to NOE."
        return 1
    else
        return 0
    fi
}

#==============================================
#if the noe binary RPM is not existed , then 1st transform is not right
#==============================================
post_check_noe_binary() {

    if [ ! -f $NOE_STOREBIN ];then
        logger -t upgrade-noe -p local0.EMERG "noe_image.rpm in /data/noe_upgrade is missing"
        return 1
    fi

    return 0
}

#==============================================
#err handler function
#==============================================
errhandler() {
    needreboot=$1 
    errinfo="$2"

    if [[ $needreboot -eq 1 ]];then
        $rebootshell --waitSettings --owner upgrad-noe --reason "$errinfo" 
    else
	logger -t upgrade-noe -p local0.EMERG "upgrade-noe error,$errinfo"
	displaypopup -t 4 -l error "upgrade-noe error,$errinfo"
    fi 

    exit 0
}

#==============================================
#parse err reason function
#==============================================
parse_errcode()
{
	case "$1" in
		0)
		echo "NOE binary download succeed"
		;;
		1)
		echo "download lanpbx.cfg failed"
		;;
		2)
		echo "parse lanpbx.cfg failed"
		;;
		3)
		echo "download key.xml failed"
		;;
		4)
		echo "import GPG key failed"
		;;
		5)
		echo "download NOE image failed"
		;;
		6)
		echo "verify NOE image failed"
		;;
		7)
		echo "signature verify of NOE rpm failed"
		;;
		10)
		echo "failed on inner reasons"
		;;
		*)
		echo "failed on unknow reasons"
		;;
	esac
}
#==============================================
#main start function
#==============================================
start() {
    rm /config/upgrade/.upgradetonoe 2>/dev/null
    
    pre_check_runmode
    [[ $? -ne 0 ]] && exit 0
   
    pre_check_flash_type
    [[ $? -ne 0 ]] && errhandler 1 "Not a supported flash"
    
    pre_check_system_volumn_size
    [[ $? -ne 0 ]] && errhandler 1 "system volumn space is not enough for noe"
    
    pre_check_upgrade_state
    [[ $? -ne 0 ]] && errhandler 1 "in upgrade procedure,cannot upgrade to noe"
    
    pre_check_restore_possibility
    [[ $? -ne 0 ]] && errhandler 1 "base rpm is missing,cannot upgrade to noe"

    if [[ a$TFTP_ADDRESS0 != a'0.0.0.0' ]];then
        transoption=$TFTP_ADDRESS0
    fi
    if [[ a$TFTP_ADDRESS1 != a'0.0.0.0' ]];then
        transoption="$transoption $TFTP_ADDRESS1"
    fi

    $transformershell $transoption $TFTP_PORT
    ret=$?
    errstring=
    if [[ $ret -ne 0 ]];then
        errstring=`parse_errcode $ret`
        errhandler 1 "$errstring"       
        exit 0
    fi

#post check
    post_check_noe_binary
    [[ $? -ne 0 ]] && errhandler 1 "noe binary check failed"

#ready~ go to upgrade partition
    touch /config/upgrade/.upgradetonoe
    $setnextbootshell UPGRADE
    CLISettings unset DHCP ADMCFG_NOE_PROTOCOL
    $rebootshell --waitSettings --reason "switch to UPGRADE and continue" --owner "upgrade-noe" $REBOOT_TYPE

}


case "$1" in 
    start)
        #pre check
        start
        ;;
    stop)
        ;;
       *)
        ;;
esac










