#!/bin/ksh
#set -x
#
#######
# The boomperfstore is used to submit performance data
# for a performance class that was previously created with
# boomperf_create.
# The script listens on stdin for incoming data and forwards
# it to the agent.
#

#Variable to hold Program Directory
PROG_DIR=
USE_NC=0

# 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: boomperfstore <performance class name> [options|<values>*]"
  echo "options:"
  echo "  -i pipe : the data will not be submitted on stdin or the command line, but through the named pipe specified as <pipe> arguement"
}

if [ $# -lt 1 ]
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 boomperfstore "$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

pclass="$1"
pdir="./"

SUBMITSTR="R\"${pdir}\" \"$pclass\" " 

if [ -z "$2" ]; then
  exec 3<&0
elif [ "$2" = "-i" ]; then
  if [ -p "$3" ]; then
    exec 3<${3}
  else
    echo "Pipe not accessible or wrong parameters specified"
    usage
    exit 2
  fi
else
  while  [ "$2" != "" ]; do
    p1="\"${2}\""
    shift
    if [ "$2" != "" ]; then
      SUBMITSTR=${SUBMITSTR}${p1}" "
    else
      SUBMITSTR=${SUBMITSTR}${p1}
    fi
  done
  if [ "$USE_NC" -eq 1 ]; then
    RETV=$(echo "${SUBMITSTR}" | nc 127.0.0.1 ${PORT})
  else
    if [ "$USE_NC" -eq 2 ]; then
      RETV=$(echo "${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*)
          {   telnet 127.0.0.1 ${PORT} 2>/dev/null <<EOF
${SUBMITSTR}${line}
EOF
          } |  while read -r RETV; do echo $RETV >/dev/null;done
          telnetpid=
          ;;
        *)
          telnet 2>/dev/null |&
	  telnetpid="$!"
          print -p "o 127.0.0.1 ${PORT}"
          print -p "${SUBMITSTR}"
          sleep 1
          print -p "quit" 2>/dev/null
          while read -p RETV; do
            RETVOLDOLD=$RETVOLD
            RETVOLD=$RETV
            echo $RETV >/dev/null
          done
          ;;
      esac
    fi
  fi

  if [ "$RETV" != "T" -a "$RETVOLD" != "TConnection closed."  -a "$RETVOLDOLD" != "TConnection closed." ]; then
    echo "Error submitting the data"
    exit 2
  else
    echo "OK"
    exit 0
  fi
fi

while read line <&3; do
  if [ "$USE_NC" -eq 1 ]; then
    RETV=$(echo "${SUBMITSTR}${line}" | nc 127.0.0.1 ${PORT})
  else
    if [ "$USE_NC" -eq 2 ]; then
      RETV=$(echo "${SUBMITSTR}${line}" | 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*)
          {   telnet 127.0.0.1 ${PORT} 2>/dev/null <<EOF
${SUBMITSTR}${line}
EOF
          } |  while read -r RETV; do echo $RETV >/dev/null;done
          telnetpid=
          ;;
        *)
          telnet 2>/dev/null |&
	  telnetpid="$!"
          print -p "o 127.0.0.1 ${PORT}"
          print -p "${SUBMITSTR}${line}"
          sleep 1
          print -p "quit" 2>/dev/null
          while read -p RETV; do
            RETVOLDOLD=$RETVOLD
            RETVOLD=$RETV
            echo $RETV >/dev/null
          done
          ;;
      esac
    fi
  fi
 
  if [ "$RETV" != "T" -a "$RETVOLD" != "TConnection closed."  -a "$RETVOLDOLD" != "TConnection closed." ]
  then
    echo "Error submitting the data"
  else
    echo "OK"
  fi
   #just in case - on some platforms telnet remain hanging
  if [ "$USE_NC" -eq 0 ]; then
    if [ -n "$telnetpid" ]; then
      kill "$telnetpid" >/dev/null 2>&1
    fi
  fi
done
exit 0
