#!/bin/sh
############
# CHANGELOG
#===========
# 2011/02/16    CRMS00262654      M.SULYAN
#               add erase log option (option --rmlog)
#
# 2011/01/31    CRMS00289617      M.SULYAN
#               add silent (option --silent) for hard & soft
#
# 2010/06/03    CRMS00229700      M. SULYAN
#               add reset flash
#
# 2010/05/03    CRMS00221489      TCREMEL
#               correct typo errors
#
# 2010/03/30    CRMS00219063      TCREMEL
#               add a reason and an owner options
#
# 2010/03/30    CRMS00220154      TCREMEL
#               add warm/cold reboot (option soft and hard)
################

# upgrade status
. /etc/init.d/rc.config
UPGD_STATUS=/usr/sbin/upgd_status

CMD=${0##*/}

#set -x

Usage()
{
# CRMS00289617+
        echo "Usage: reset [soft|hard|flash|rmlog] [--silent] [--help]"
# CRMS00289617-
        echo "   soft    soft reset of the terminal"
        echo "   hard    hard reset of the terminal"
# CRMS00229700+
        echo "   flash   remove flash config of the terminal and hard reset"
# CRMS00229700-
# CRMS00262654+
        echo "   rmlog   remove reset log files & exit without reboot"
# CRMS00262654-
# CRMS00289617+
        echo " "
        echo "   --silent  use silent reset mode"
        echo "   --help    display help"
# CRMS00289617-
}

# CRMS00262654+
RmLog()
{
        echo "Deleting reset log files..."
        rm -f /log/Reset.log    > /dev/null 2>&1
        rm -f /log/Reset.log.*  > /dev/null 2>&1
        DateTime=`date +"%c"`
        echo "Reset log deleted on $DateTime" > /log/Reset.log
        # restart syslog
        /bin/kill -HUP `cat /var/run/syslog-ng.pid 2> /dev/null` 2> /dev/null || true
}
# CRMS00262654-

# get param, default soft reset
# CRMS00289617+
silent=0
param="soft"
mode=0
while [[ $# -gt 0 ]]; do
    case "$1" in
        soft|hard|flash)
            if [ $mode == "0" ]; then  
	       param=$1
               mode=1
            else
               Usage
               echo "$CMD KO"
               exit 1
            fi
        ;;
        --silent) 
	    silent=1
        ;;
# CRMS00262654+
        rmlog) 
            RmLog
            echo "$CMD OK"
	    exit 0
        ;;
# CRMS00262654-
	--help) :
            Usage
            echo "$CMD OK"
            exit 0
        ;;
        *)
            Usage
            echo "$CMD KO"
            exit 1
        ;;
    esac
    shift
done

if [ $silent == "1" ]; then
   CMD_reboot="/sbin/rebootbyappli"
   OPT_reboot="--$param --silent"
   REASON_reboot="silent $param reset"
   COMMENT_reboot="silent $param reboot requested"
else
   CMD_reboot="/sbin/reboot"
   OPT_reboot="--$param"
   REASON_reboot="$param reset"
   COMMENT_reboot="$param reboot requested"
fi
# CRMS00289617-

# log the origi of reset : console or ssh session
if [[ ! -z "$SSH_CONNECTION" ]]; then
   SSH_IP=${SSH_CONNECTION%% *}
   REBOOT_ORIGIN="by $USER from $SSH_IP"
else
   REBOOT_ORIGIN="by $USER from console"
fi

# CRMS00289617+
if [ $param == "soft" ] || [ $param == "hard" ]; then
   echo "$COMMENT_reboot"
# CRMS00220154+
# CRMS00219063+
   $CMD_reboot $OPT_reboot --reason "$REASON_reboot $REBOOT_ORIGIN" --owner "embedded reset script"
# CRMS00219063-
# CRMS00220154-
# CRMS00289617-
else
if [ $param == "flash" ]; then
# CRMS00229700+
   if [ -x $UPGD_STATUS ]; then
      $UPGD_STATUS >/dev/null 2>&1
      if [ $? -eq 1 ]; then
         echo "upgrade in progress, reset flash not allowed"
         echo "$CMD KO"
         exit 1
      fi
   fi
   # do reset flash
   if [ -x /sbin/reset_flash ]; then
      touch /.reset_flash
      echo "reset flash running for now..."
      /sbin/reset_flash now
      echo "Hard Reboot requested"
      reboot --hard --reason "reset flash $REBOOT_ORIGIN" --owner "embedded reset script"
   else
      echo "no reset_flash command found"
      echo "$CMD KO"
      exit 1
   fi
# CRMS00229700-
fi
fi

echo "$CMD OK"
exit 0
