#!/bin/sh
############
# CHANGELOG
#===========
# 2013/03/08    PR442574      Qianjun Zhang
#               embedded command for "NOE Enable"
#
################

# upgrade status
. /etc/init.d/rc.config

CMD=${0##*/}
rebootshell=$(which reboot)
cli="/usr/bin/CLISettings"
#set -x

Usage()
{
    echo "Usage: appl_mode [SIP|NOE]"
    echo "   SIP  change current running mode to SIP"
    echo "   NOE  change current running mode to NOE"
}

cur_mode=`$cli get CURRENT ADMCFG_NOE_PROTOCOL`

mode=SIP
if [[ $# -gt 0 ]]; then
    case "$1" in
        NOE|noe)
            mode=NOE
            ;;
        SIP|sip)
            mode=SIP
            ;;
        *)
        Usage
            echo "$CMD KO"
            exit 1
        ;;
    esac
else 
    if [ a"$cur_mode" == a"true" ]; then
        echo "Current running mode is NOE"
    else
        echo "Current running mode is SIP"
    fi
    echo "$CMD OK"
    exit 0
fi

if [[ a"$mode" == a"SIP" ]] && [[ a"$cur_mode" == a"false" ]]; then
    echo "Current local running mode is $mode"
#$cli set LOCAL ADMCFG_NOE_PROTOCOL false 

elif [[ a"$mode" == a"NOE" ]] && [[ a"$cur_mode" == a"true" ]]; then
    echo "Current local running mode is $mode"
    
else
    read -p "Are you sure switch local running mode to $mode and reboot [yes|no]:" switch_ack
    if [ a"$switch_ack" == a"yes" ]; then
        if [ a"$mode" == a"NOE" ];then
            $cli set LOCAL ADMCFG_NOE_PROTOCOL true 
            protocol=`$cli get LOCAL ADMCFG_NOE_PROTOCOL`
            if [[ a"$protocol" == a"true" ]];then
                $rebootshell --owner upgrad-noe --reason "Current running mode change to $mode!"
                else
                echo "Local TFTP configuration is not right,change to NOE failed"
                echo "$CMD KO"
                exit 1
            fi	
        else
            $cli set LOCAL ADMCFG_NOE_PROTOCOL false
            $rebootshell --owner upgrad-noe --reason "Current running mode change to $mode!"
        fi
    else
        echo "$CMD KO"
        exit 1
    fi
fi

echo "$CMD OK"
exit 0
