#!/bin/sh
#
# chkconfig: 345 51 --
#
# the template file of init.d script
# start or stop services

############
# CHANGELOG
#===========
#
# 2013/01/10    PR341502        Ziming B Xu <Ziming.b.Xu@alcatel-lucent.com>
#               Reduce Reboot Frequency
#
# 2012/08/23    CRMS00392052    Ziming B Xu <Ziming.b.Xu@alcatel-lucent.com>
#               Enhancement for CTL download and installation
#
# 2012/07/30    CRMS00386875    Ziming B Xu <Ziming.b.Xu@alcatel-lucent.com>
#               Add a force parsing option.
#
# 2011/05/30    CRMS00312748    Thanh lam NGUYEN
#               Fix multiple instance of either the daemon or the init script
#
# 2010/03/11    CRMS00217278    Thanh lam NGUYEN
#               Add a log to the console on a failure to download a file
#
# 2010/02/26    CRMS00152511    Thanh lam NGUYEN
#               modify --force managment
#               add clean & setforce function
#
# 2010/02/23    CRMS00215775	Thanh lam NGUYEN
#               Fix the update status check
# 2010/06/22    CRMS00237546    Jason Zhang
#               "asset_id" not update according to dm config flie
#
# 2010/09/08    CRMS00249956 Jason Zhang
#               Can not change "power priority" by mmi and config
#
# 2010/10/15    CRMS00267205 Alex CAO
#               Add 8021p priority mapping 
#
# 2011/02/14    CRMS00293844 Alex CAO
#               Rollback 8021p priority mapping 
#
# 2011/12/12    CRMS00350032 Michel Sulyan
#               Update current settings if no dmconfig call (Upgrade in progress)
################
# CRMS00312748
FUNCTION=MAIN esc=$'\x1b'
read <${ROOT}/proc/self/stat PS4_PREFIX - 2>/dev/null
PS4_TPLT="$esc[$((${PS4_PREFIX##*|}%6+31))m+(\$PS4_PREFIX)\$FUNCTION:$esc[0m "
PS4=$PS4_TPLT
# CRMS00312748 END

. /etc/init.d/rc.config

bin=/usr/sbin/dmconfig
# CRMS00312748
scriptname=${bin##*/}
# CRMS00312748 END
pidfile=/var/run/dmconfig.pid
lockfile=/var/lock/subsys/dmconfig
STATUS=/usr/sbin/upgd_status
# CRMS00152511
forceflag=${NVDM_ROOT}/.force
args=
# CRMS00152511 END
# CRMS00312748
pollingflag=/tmp/.$scriptname.polling
workdir=/tmp/config
# PR341502 zimingxu+
UPDT_FLAG=/tmp/.update_flag
# PR341502 zimingxu-

[[ ! -d $workdir ]] && mkdir -m 0755 -p $workdir


script_exit()
{
    local - FUNCTION=script_exit
    ENTER_FUNCTION

    if [[ ${mutexcheck:-0} -eq 1 ]]; then
        mutex_unlock mtx_done
        mutex_unlock mtx_next
        mutex_unlock mtx_run
        mutex_unlock mtx_start
        mutex_delete_context
    fi
    RETURN 0
}

trap script_exit 0
# CRMS00312748 END

start() {
# CRMS00312748
    mutex_create_context
    mutexcheck=1
# CRMS00312748 END
    : "If in upgrade, do nothing"
    if [ -x $STATUS ]; then
# crms00215775_wrong_update_status_check
# CRMS00217278
# CRMS00350032
    $STATUS >/dev/null 2>&1 || { echo "Upgrade in progress, skipping configuration file download, just update current config"; updateCurrentConfig; return; }
# CRMS00217278 END
# crms00215775_wrong_update_status_check end
    fi

    : "Already running ?"
# CRMS00312748
    ! mutex_trylock mtx_start && { [[ $ret -ne 2 ]] && echo "start mutex not free" >&2 || echo "no mutex context" >&2; return $?; }
    [[ -f $pidfile ]] && [[ -d /proc/$(cat $pidfile) ]] && RETVAL=1 && return $RETVAL

    : "Force reparsing ?"
# CRMS00152511
    if [[ -f $forceflag ]] || [[ -f /tmp/${forceflag##/} ]]; then
        args="${args:+$args }--force"
        rm $pollingflag
        rm -f $forceflag /tmp/${forceflag##/} 2>/dev/null
    fi
# CRMS00152511 END
    [[ -f $pollingflag ]] && args="${args:+$args }--polling_restart" && echoerror "Dmconfig had to be restarted"
# CRMS00312748 END

    : "Forward the xflag to $bin"
    if [[ "${-//x}" != "$-" ]]; then
# CRMS00152511
        sh -x $bin $args
    else
        $bin $args
# CRMS00152511 END
    fi
    RETVAL=$?
#CRMS00237546 jasonaz+    
    lldp_assetid=`/usr/bin/CLISettings get DM LLDPCFG_ASSETID`
    /usr/bin/CLISettings set CURRENT LLDPCFG_ASSETID $lldp_assetid
#CRMS00249956 jasonaz+
#    lldp_powerprio=`/usr/bin/CLISettings get DM LLDPCFG_POWER_PRIORITY`
#    /usr/bin/CLISettings set CURRENT LLDPCFG_POWER_PRIORITY $lldp_powerprio
#CRMS00249956 jasonaz-
#CRMS00237546 jasonaz-   

# CRMS00312748
    if [[ $RETVAL -ne 0 ]]; then
        stop 
    else
        touch $lockfile
    fi
    mutex_unlock mtx_start
# CRMS00312748 END
    return $RETVAL
}

stop() {
# CRMS00312748
    local pid=0
    [[ -f $pidfile ]] && read <$pidfile pid
    if [[ $pid -ne 0 ]]; then
        display "stopping dmconfig"
        kill -USR2 $pid 2>/dev/null
        RETVAL=$?
        if [[ $RETVAL -eq 0 ]]; then
            rm -f $pidfile >/dev/null 2>&1
            rmdir $lockfile >/dev/null 2>&1
        fi
    fi
    rm 2>/dev/null $pollingflag
# CRMS00312748 END
    return $RETVAL
}

# CRMS00152511
createtmpdir() {
    mkdir -p /tmp${forceflag%/*} >/dev/null 2>&1
    return 0
}
# CRMS00152511 END

# CRMS00312748
signal() {
    local pid=0 xtrace
    [[ -f $pidfile ]] && read <$pidfile pid
    if [[ -d /proc/$pid ]]; then
        kill -USR1 $pid
    fi
    # in case the signal would kill the process
    if [[ ! -d /proc/$pid ]]; then
        getPID
        xtrace=${-//[^x]}
        [[ -n "$xtrace" ]] && set +x
        start
        [[ -n "$xtrace" ]] && set -x
    fi
}

reload() {
    signal "$@"
}

hup() {
    signal "$@"
}

# PR341502 zimingxu+
update() {
    touch $UPDT_FLAG
    signal "$@"
}
# PR341502 zimingxu-

get() {
    $bin "$1" mandatory
    return $?	
}
# CRMS00312748 END
: "jumping to $1"
case "$1" in
    start|"") : "$1"
        start
    ;;
    stop) : "$1"
        stop
    ;;
    condrestart) : "$1"
        if [ -f $lockfile ]; then
            stop
            start
        fi
    ;;
    restart) : "$1"
        stop
        start
    ;;
# CRMS00312748
# PR341502 zimingxu+
    reload|hup|get|update) : "$1" "$2"
# PR341502 zimingxu-
        function=$1
        shift
        arg="$@ $$"
        mutex_create_context
        mutexcheck=1
        mutex_lock mtx_next
        echo "set -- $function \"$arg\"" >> $workdir/next
        mutex_unlock mtx_next
        data=""
        if mutex_trylock mtx_run; then
            touch $workdir/done
            while [[ -f $workdir/next ]]; do
                if mutex_lock mtx_next && mutex_lock mtx_done; then
                    data="$(diff $workdir/done $workdir/next | awk '/^\+[^+]/{sub(/^.*\+/,""); print; exit}')"
                    [[ -z "$data" ]] && rm $workdir/next
                else
                    data="break"
                fi
                mutex_unlock mtx_done
                mutex_unlock mtx_next
                if [[ -n "$data" ]]; then
                    eval $data
                    : "$esc[41;30m$@$esc[0m"
                    $1 "$2"
                    ret=$?
                    echo "$data" >> $workdir/done
                fi
            done
            mutex_lock mtx_done && rm $workdir/done && mutex_unlock mtx_done
            mutex_unlock mtx_run
        fi
    ;;
# CRMS00312748 END
    debug) : "$1"
        if [[ -f $pidfile ]] && [[ -e /proc/$(cat $pidfile) ]]; then
            kill -TRAP $(cat $pidfile)
        else
            set -x
            start
        fi
    ;;
    force) : "$1"
        stop
        touch $forceflag
        start
    ;;
# CRMS00386875
    reparse) : "$1"
# CRMS00392052 zimingx+
	# Remove the checksum files to force dmconfig to parse the corresponding files in next boot procedure.
	todel="$NVDM_ROOT/enet.cfg $(ls $NVDM_ROOT/.cksum.* 2>/dev/null)"
	todel="$todel ${todel//.cksum.}"
	rm -rf $todel 2>/dev/null
# CRMS00392052 zimingx-
    ;;
# CRMS00386875 END
# CRMS00152511
    clean) : "$1"
        defineConfigFileList
        todel=
        for file in $(ls $NVDM_ROOT/.cksum.* 2>/dev/null) $NVDM_ROOT/${configFileList// / $NVDM_ROOT\/}; do
            [[ -f $file ]] && todel="${todel:+$todel }$file"
            [[ "${file//.cksum.}" != "$file" ]] && todel="${todel:+$todel }${file//.cksum.}"
        done
        [[ -n "$todel" ]] && rm -f $todel >/dev/null 2>&1
    ;;
    createtmpdir) : "$1"
        createtmpdir
    ;;
    setforce) : "$1"
# CRMS00312748
        createtmpdir
        touch $forceflag
# CRMS00312748 END
    ;;
    *) : "$1"
# CRMS00386875
        echo "Usage $0 {start|stop|restart|reload|condrestart|clean|setforce|force|reparse}" >&2
# CRMS00386875 END
        RETVAL=1
    ;;
# CRMS00152511 END
esac

exit $RETVAL
