#!/bin/ksh
#set -x

#Variable to hold Program Directory
PROG_DIR=
USE_NC=0
FOUND="0"
RET_CODE=
RET_VAR=

# getdir: identify the path to the program and set
#         it as PROG_DIR
# Params: program-name and $0 from call
getdir() {
  prog="$1"
  call="$2"
  if [ -z "$prog" ]
  then
    return
  fi
  #   newdir=${0%/*}
  newdir=`echo "${call}"|sed "s/${prog}$//"`
  if [ -z "$newdir" ]
  then
    whichstr=`which ${prog}|grep -v "no ${prog}"`
    newdir=`echo "$whichstr"|sed "s/${prog}$//"`
    if [ -z "$newdir" ]
    then
      return
    fi
  fi
  if [ -d "$newdir" ]
  then
    PROG_DIR="$newdir"
  fi
}

usage () {
 echo "Usage:\t boomstartmt -ed <ExpectedDurationInterval> -ac {HIDE[-pa {NONE|DROP|PUBLISH}]|DROP}"
 echo "\t <attribute>=\"<SimplifiedPattern>\" [<attribute>=\"<SimplifiedPattern>\"]*"
 echo "\t [-du <MaxDurationInterval>]"
 echo ""
 echo "Description: Enable a maintenance window on the agent."
 echo "The Agent will inform the main boom server, that maintenance has been started"
 echo "for indications matching the defined filters. End of the maintenance can be triggered"
 echo "by boomstopmt command or automatically by specifying \"-du\" option."
 echo "Administrator can forcibly end the maintenance from the boom UI."
 echo ""
 echo "Return value: UUID of the maintenance."
 echo ""
 echo "Options:"
 echo "  -ed <ExpectedDurationInterval> - expected duration of the maintenance."
 echo "      An Indication will be triggered by server to inform if expected duration is overdue."
 echo "  -du <MaxDurationInterval> - MAX duration of the maintenance."
 echo "      The server will be informed to disable this maintenance after specified interval,"
 echo "      in case boomstopmt command was not performed before."
 echo "      Supported interval formats:"
 echo "        ?w?d?h?m - a combination of weeks, days, hours and minutes (Minimum interval: 1m)."
 echo "\tExamples:"
 echo "\t  2d3h4m - 2 days, 3 hours, 4 min"
 echo "\t  3d5h - 3 days and 5 hours"
 echo "\t  100m - a hundred of minutes"
 echo "  -ac <action> - required action on the server for matched indications during maintenance."
 echo "      actions:"
 echo "        HIDE - Indications need to be displayed in the outage browser"
 echo "        DROP - Indications must be dropped by the server"
 echo "  -pa <postAction> - Action to be performed after the end of the maintenance."
 echo "                     Valid only with options \"-ac HIDE\""
 echo "      postActions:"
 echo "        NONE - the indications will stay in the outage browser after the outage is ended"
 echo "        DROP - the indications will be dropped from the outage browser at the end of the maintenance window"
 echo "        PUBLISH - the indications will be moved to the active browser at the end of the maintenance window"
 echo "  <attribute>=\"<SimplifiedPattern>\" - one or more filters, based on attribute name and simplified patterns"
 echo "                                      to match indications targeted for maintenance processing."
 echo "      Supported attributes:"
 echo "        APPLICATION, GROUP, OBJECT, HOST (alias: NODE), CA, CA1 ... CA15, NODEGROUP, SOURCE, TEXT, SEVERITY, KEY"
 echo "        Examples:"
 echo "          APPLICATION=\"ORACLE<*>\" - matches any indications with attribute Application started with ORACLE"
 echo "          SEVERITY=\"<[critical|major]>\" - matches any indications with severity critical or major"
 echo "          TEXT=\"<*>\" - matches any indications"
 echo ""
 echo "Example:"
 echo "  boomstartmt -ed 2h -du 2h30m -ac HIDE -pa PUBLISH SEVERITY=\"<*>\""
 echo ""



}

if [ $# -lt 5 ]
then
 usage
 exit 1
fi

if [ -z "$BOOM_AGENT_PORT" ]
then
  if [ -z "$BOOM_ROOT" ]
  then
    echo "To avoid negative performance impacts please set the BOOM_ROOT variable"
    getdir boomindi "$0"
    if [ -z "$PROG_DIR" ]
    then
      echo "Failed to identify the path for BOOM_ROOT."
      echo "Please set the BOOM_ROOT variable to the Agent directory!"
      echo "The script continues with default values."
      PORT=23021
    else
      PORT=`cd "${PROG_DIR}"; grep "^AGENT_PORT.*" ../conf/agent.conf | cut -d"=" -f2`
    fi
  else
    PORT=`cd "${BOOM_ROOT}"; grep "^AGENT_PORT.*" ./conf/agent.conf | cut -d"=" -f2`
  fi
else
  PORT="$BOOM_AGENT_PORT"
fi

if [ -z "$PORT" ]; then
  PORT=23021
fi

if [ -f netcat.sh ]; then
 . ./netcat.sh
fi

SUBMITSTR=""
while [ "$1" != "" ]; do
  p1=$(echo $1 | sed 's/\\/\\\\/g' | sed 's/\"/\\\"/g' | sed 's/\([^=]*\)=\(.*\)/\1=\"\2\"/')
  shift
  if [ "$1" != "" ]; then
    SUBMITSTR=${SUBMITSTR}${p1}" "
  else
    SUBMITSTR=${SUBMITSTR}${p1}
  fi
done

if [ "$USE_NC" -eq 1 ]; then
  RET_VAR=$(echo "G-e ${SUBMITSTR}" | nc 127.0.0.1 ${PORT})
else
  if [ "$USE_NC" -eq 2 ]; then
    RET_VAR=$(echo "G-e ${SUBMITSTR}" | netcat 127.0.0.1 ${PORT})
  else
#
#   use telnet
#   Fetch the leading lines from telnet client and the result
#
    osname=`uname`
    case "$osname" in
      HP*)
        RET_VAR=`{   telnet 127.0.0.1 ${PORT} 2>/dev/null <<EOF
G-e ${SUBMITSTR}
EOF
        }`
        ;;
      *)
        telnet 2>/dev/null |&
        print -p "o 127.0.0.1 ${PORT}"
#sleep 1
        print -p "G-e ${SUBMITSTR}"
        sleep 1 
        print -p "quit" 2>/dev/null
        while read -p RETL
        do
          RET_VAR="$RET_VAR"'\n'"$RETL"
        done

        if [ "$RETL" != "" ]
        then
          RET_VAR="$RET_VAR"'\n'"$RETL"
        fi
        ;;
    esac
  fi
fi

echo ""

if [ "$USE_NC" -eq "1" ] || [ "$USE_NC" -eq "2" ]
then
  echo $"$RET_VAR"
else
  while read -r RETV
    do
      if echo $RETV | grep "Escape character is" > /dev/null
      then
        FOUND="1"
        continue
      fi
      if [ "$FOUND" = "1" ]
      then
        echo $RETV
      fi
    done <<EOT
$(echo $"$RET_VAR")
EOT

fi

#echo RET_CODE=$RET_CODE

#if [ "$RET_CODE" != "T" ]
#then
#  echo "Error executing command."
#  usage
#  exit 2
#fi

exit 0

