#! /bin/ash

ERROR=1
if [ "$2" != "" ]; then
  if [ "$3" != "" ]; then
    if [ "$4" != "" ]; then
      if [ "$5" != "" ]; then
        TIMEVALUE="$5"
      else
        TIMEVALUE="0"
      fi
      if [ "$6" != "" ]; then
        MESSAGE="$6"
      else
        MESSAGE="Shutdown activated from CMC-TC Master because of invalid operation condition!"
      fi    
      if [ "$1" = "shutdown" ]; then
        net -I "$2" -U "$3" -W "$4" rpc shutdown -t "$TIMEVALUE" -C "$MESSAGE"
        if [ "$?" = "0" ]; then
          ERROR=0
        fi 
      else
        if [ "$1" = "forcedshutdown" ]; then
          net -I "$2" -U "$3" -W "$4" rpc shutdown -t "$TIMEVALUE" -C "$MESSAGE" -f
          if [ "$?" = "0" ]; then
            ERROR=0
          fi
        else
          if [ "$1" = "abort" ]; then
            net -I "$2" -U "$3" -W "$4" rpc abortshutdown
            if [ "$?" = "0" ]; then
              ERROR=0
            fi
          else
            echo "No or unknown command specified."
          fi
        fi
      fi
    else
      echo "Domain name of server to shutdown missing."
    fi
  else
    echo "Username for requesting server shutdown missing."
  fi
else
  echo "IP-Address of server to shutdown missing."
fi   
exit $ERROR 
