#!/bin/sh
############
# CHANGELOG
#===========
# 2011/01/31    CRMS00289617      M.SULYAN
#               add silent (option --silent)
# 2012/07/25    CRMS00386301      JERRY.ZHOU
#               reboot failed when applicationManage is alive, but Appli_plf is stop.
################


SEND_INFRA=/usr/bin/send_infra
. /etc/init.d/rc.config

debug=0

[[ -f /${debugflag} ]] && debug=1
[[ -f /tmp/${debugflag#.} ]] && debug=1
[[ $debug -eq 1 ]] && busybox=display

rebootlevel=0
reason="unknown"
owner="unknown"
immediate="false"
param=""
#crms00289617+
silent="false"
#crms00289617-

#CRMS00386301 jerryzh+
appli_platform_status=0

check_appli_platform()
{
    ps | grep "[P]latform" 2>/dev/null
    return $?
}
#CRMS00386301 jerryzh-

usage()
{
echo "usage : $0 [--soft|--hard] [--reason <reboot reason>] [--owner <reboot source>] [--immediate] "
}

if [ $# -eq 0 ]
then
    usage
    exit 1
fi
while [[ $# -gt 0 ]]; do
    case "$1" in
        --immediate) 
	    immediate="true"
        ;;
#crms00289617+
        --silent) 
	    silent="true"
        ;;
#crms00289617-
        --soft) 
	    rebootlevel=1
	    param="--soft"
        ;;
        --hard) 
	    rebootlevel=2
	    param="--hard"
        ;;
        --reason) :
            reason=$2
            shift
        ;;
        --owner) :
            owner=$2
            shift
        ;;
	--help) :
            usage
            exit 0
        ;;
        *)
        ;;
    esac
    shift
done

#crms00289617+
if [ $silent == "true" ]; then
    # change rebootlevel mask: set mask bit 2
    rebootlevel=`expr $rebootlevel + 4`
fi
#crms00289617-

#CRMS00386301 jerryzh+
check_appli_platform
appli_platform_status=$?
if [[ -f $applicationManagerPid ]] && [[ -d /proc/$(cat $applicationManagerPid) ]] && [[ $appli_platform_status -eq 0 ]]; then
#CRMS00386301 jerryzh-
        # application manager running
        $SEND_INFRA reboot $rebootlevel $immediate "$owner" "$reason"
        exit $?
else 
	/sbin/reboot $param --reason "$reason" --owner "$owner"
	exit $?
fi

