#!/bin/sh
#set -x
#
# install:
# the install script will query the basic required information
# and update the agents configuration files accordingly
#
# 12/12/2022 bs (3.00): arguments [-atp <agent TLS port>] [-ct <SOCKET|TLS>] added
# 13/12/2022 ps (3.01): consuming boom_agt.cfg: JAVA_BIN, JAVA_OPTS, USER remain unchanged in boom_agt.cfg (overlay effect); 
#                       JAVA_OPTS="-Xmx512M" (max. heap size default linux64) 
 

#Variable to hold active PIDS of Agent
AGT_PIDS=
#Variable to hold user that will be starting the agent
AGT_USER=
#Variable to hold the current user
CURR_USER=
#Variable to hold Operating System
OSNAME=
#Variable to hold Program Directory
PROG_DIR=
#Variables to hold parsed value and comment flag
PVALUE=
PCOMMENT=
#Variable to hold number or string from user input
QUERYNUM=
QUERYSTRING=
#Variables to hold Java and init.d Directories
JAVA_DIR=
PERL_DIR=
RC_DIR=
RC_LEVEL=
#Variable to hold ESX firewall selection
ESXFW=
#Variable to set if only known PID should be used for stop
PID_ONLY=
#Variable to hold info for silent install
SILENT=
# Variable to hold init system (initd, systemd etc.)
SYS_INIT=

BOOM_ROOT_STR="BOOM_ROOT"
JAVA_BIN_STR="JAVA_BIN"
PERL_PATH_STR="PERL_PATH"
USER_STR="USER"
JAVA_OPTS_STR="JAVA_OPTS"
JAVA_OPTS="-Xmx512M"


# usage: print usage information
usage() {
  echo "install [-u [-s]|-s [-a <boomservers>] [-ap <agent port>] [-atp <agent TLS port>] [-ct <SOCKET|TLS>] [-c false|true -h <fixed hostname> -i <fixed agent IP>] [-h <fixed hostname>] [-i <fixed agent IP>] [-ll <1|2|3|4>] [-lc <count>] [-ls <sizeMB>] [-j <path to java bin>] [-n false|true] [-p <path to perl bin>] [-rl <2|3|4|5>] [-sh <server hostname>] [-si <server ip address>] [-sp <server port>] [-us <user>] [-m <mode>]]"
  echo "no paramters: interactive install of the boom agent"
  echo "-s: silent install the boom agent"
  echo "-u: uninstall the boom agent"
  echo "-a <boom servers>: specify a comma separated list of boom server hostnames and ip addresses that are allowed to talk to this agent (no spaces between entries). This flag is mainly used in environments with multiple boom servers or where the boom server runs on a cluster, so that the physical and virtual IPs of the cluster need to be entered."
  echo "-ap: default: 23021 - the port that the agent should listen on."
  echo "-atp: default: 23031 - the TLS port that the agent should listen on."
  echo "-ct: communication type: SOCKET | TLS"
  echo "-c: default: false - the agent resides on a cluster node. This requires to set -h and -i parameter."
  echo "-h <agent hostname>: if this parameter is set, the agent will skip the detection of its local hostname and communicate <agent hostname> as its ip name to the server. This flag is mainly used in environments in which the local name differs from the name that the server has to use to connect to the agent or for clusternodes, where a virtual name might be detected instead of physical."
  echo "-i <agent ip>: if this parameter is set, the agent will skip the detection of its primary ip and communicate <agent ip> as its ip address to the server. The specified IP address must be valid for this system. This flag is mainly used for systems with multiple network interfaces or for clusternodes, where a virtual ip might be detected instead of physical."
  echo "-ll <1...4>: default: 1 - set the loglevel (1 - only errors, 4 - trace level)."
  echo "-lc <count>: number of log files to retain."
  echo "-ls <size>: maximum size per logfile in MB, e.g. 10MB."
  echo "-j <java path>: specify the path to the java runtime bin directory of the version that should be used."
  echo "-n: default: false - multiple agents might communicate using the same IP. This switch instructs the agent to send some more identifier information with its packages to ensure that the server accepts and assigns packages to the right agent. This is typically used for NAT environments where traffic to the server will show the NAT as originating IP and for clusternodes, where originating IP might show a virtual IP address."
  echo "-p <perl path>: specify the path to the perl runtime bin directory of the version that should be used."
  echo "-rl <runlevel>: specify the runlevel at which the agent has to be started."
  echo "-si <server ip>: if set, the specified ip address will be used as IP address of the boom server and replace the one that is specified in the default agent.conf configuration file."
  echo "-sh <server hostname>: if set, the specified hostname will be used as hostname of the boom server and replace the one that is specified in the default agent.conf configuration file."
  echo "-sp <server port>: if set, the specified server port will be used to connect on the server."
  echo "-us <user>: if set, the specified user will be used to start the agent."
  echo "-m <mode>: [0|7] 0 - Agent actively trying to reach server, 7 - Agent passively waits for server connection."
  echo ""
}

# parsecmdline: parse the commandline arguments
# Params: $* from call
parsecmdline() {
 if [ -n "$1" ]; then
   case "$1" in
     -u|-U|-uninstall|-deinstall)
        shift
        case "$1" in
          -s|-S|-silent)
            SILENT=true
            echo "Information: Silent De-installing boom agent"
           ;;
          *) echo "Information: De-installing boom agent"
           ;;
        esac
        uninstall_boomagt
        exit 0
      ;;
     -s|-S|-silent)
        SILENT=true
        echo "Information: Silent-Installing boom agent"
        shift
        ;;
     *) usage
        exit 1
      ;;
   esac
 fi

 while [ -n "$1" ]; do
   case "$1" in
     -a|-A|-allowedservers) #CLUSTER_NODES - allowed boom servers
        shift
        CLUSTER_NODES="$1"
      ;;
     -ap|-AP|-agentport) #override default agent port
        shift
        AGENT_PORT="$1"
      ;;
	 -atp|-ATP|-agenttlsport) #override default agent TLS port
        shift
        AGENT_TLS_PORT="$1"
      ;;
     -m|-M|-mode) #mode
        MODE="$1"
        if [ "$MODE" = "0" ] || [ "$MODE" = "7" ]; then
          # all good
		  echo "MODE=${MODE}"
        else
          MODE="0"
          echo "Error: invalid parameter for -m: $1. '0' will be used."
        fi
      ;;
     -c|-C|-cluster) #Clusternode
        shift
        case "$1" in
          true|True|TRUE|T|t|Y|y|yes|YES)
            IS_CLUSTER=1
            FIXED_IP=true
            FIXED_NAME=true
            SHARED_IP=true
          ;;
          false|False|FALSE|F|f|N|n|no|NO)
            IS_CLUSTER=
            FIXED_IP=false
            FIXED_NAME=false
            SHARED_IP=false
          ;;
          *) echo "Error: invalid parameter for -c: $1"
          ;;
        esac
      ;;
	 -CT|-ct|-commtype) #communication type [SOCKET | TLS ]
        shift
        COMM_TYPE="$1"
        check_comm_type $COMM_TYPE
        rc=$?
        if [ "$rc" != "0" ]; then
           exit 3 
        fi
      ;;
     -H|-h|-hostname) #Hostname (Fixed)
        shift
        AGENT_HOST="$1"
        FIXED_NAME=true
      ;;
     -I|-i|-agentip) #AgentIP (Fixed)
        shift
        AGENT_IP="$1"
        FIXED_IP=true
      ;;
     -J|-j|-javapath) #Java Path
        shift
        JAVA_DIR="$1"
      ;;
     -LC|-lc|-logcount) #Log count
        shift
        LOGCOUNT="$1"
      ;;
     -LL|-ll|-loglevel) #Log Level
        shift
        LOGLEVEL="$1"
      ;;
     -LS|-ls|-logsize) #Size per Logfile
        shift
        LOGSIZE="$1"
      ;;
     -N|-n|-nat|-sharedIP) #Shared IP
        shift
        case "$1" in
          true|True|TRUE|T|t|Y|y|yes|YES)
            SHARED_IP=true
          ;;
          false|False|FALSE|F|f|N|n|no|NO)
            if [ -z "$IS_CLUSTER" ]; then
              SHARED_IP=false
            else
              echo "Information: Cluster Setting takes precedence for SHARED_IP (NAT)."
            fi
          ;;
          *) echo "Error: invalid parameter for -n: $1"
          ;;
        esac
      ;;
     -P|-p|-perlpath) #Perl Path
        shift
        PERL_DIR="$1"
      ;;
     -RL|-rl|-runlevel) #Runlevel
        shift
        RUN_LEVEL="$1"
      ;;
     -SI|-si|-serverip) #boom server ip
        shift
        MAIN_SERVER_IP="$1"
      ;;
     -SH|-sh|-serverhost) #boom server hostname
        shift
        MAIN_SERVER_NAME="$1"
      ;;
     -SP|-sp|-serverport) #boom server port
        shift
        MAIN_SERVER_PORT="$1"
      ;;
     -US|-us|-user) #user to start agent
        shift
        AGT_USER="$1"
        check_user $AGT_USER
        rc=$?
        if [ "$rc" != "0" ]; then
           exit 2 
        fi
      ;;
     -XCL|-xcl|-xclone) #support cloning of agents
        shift
        case "$1" in
          true|True|TRUE|T|t|Y|y|yes|YES)
            CLONING_SUPPORT=true
          ;;
          false|False|FALSE|F|f|N|n|no|NO)
            CLONING_SUPPORT=false
          ;;
          *) echo "Error: invalid parameter for -xclone: $1"
          ;;
        esac
      ;;
     *) echo "Unknown parameter: $1"
        usage
        exit 1
      ;;
   esac
   shift
 done

 if [ -n "$IS_CLUSTER" ]; then
   if [ -z "$AGENT_HOST" ]; then
     echo "Error: for clusternodes the settings for -i and -h must be specified."
     exit 3
   fi
   if [ -z "$AGENT_IP" ]; then
     echo "Error: for clusternodes the settings for -i and -h must be specified."
     exit 3
   fi
 fi
}

# 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
}

#query_number
#queries the user for some number
#Params:
# $1 query to display
# $2 Default value
#Return: in QUERYNUM
#
query_number() {
  QUERYNUM=
  echo "$1"
  while [ -z "$QUERYNUM" ]; do
    echo $ECHON "[${2}]?${ECHOSYS5}"
    read answ
    QUERYNUM=${answ:-$2}
    QUERYNUM=`echo "$QUERYNUM"|sed 's/\([0-9]*\).*/\1/'`
    if [ -z "$QUERYNUM" ]; then
      echo "Wrong number. Please specify a valid number."
    fi
  done
}

#query_string
#queries the user for some string
#Params:
# $1 query to display
# $2 Default value
#Return: in QUERYSTRING
#
query_string() {
  QUERYSTRING=
  echo "$1"
  echo $ECHON "[${2}]?${ECHOSYS5}"
  read answ
  QUERYSTRING=${answ:-$2}
}

#query_ync
#queries the user for yes/no/cancel
#Params:
# $1 query to display
# $2 Default value
#Return: 0 - Yes, 1 - No, 2 - Cancel
#
query_ync() {
  echo "$1"
  retval=-1
  while [ $retval -eq -1 ]; do
    echo $ECHON "(yes|no|cancel) [${2}]?${ECHOSYS5}"
    read answ
    answ=${answ:-"$2"}
    echo "$answ"
    case "$answ" in
      y|Y|yes|Yes|YES) retval=0;;
      n|N|no|No|NO) retval=1;;
      c|C|cancel|Cancel|CANCEL) retval=2;;
      *) echo "Invalid input! Please select from (yes|no|cancel)";;
    esac
  done
  return $retval
}

#parse_value
#parse the passed line and set the
#read Value and Comment flag
#Params:
# $1 line to parse
#
parse_value() {
  comment=`echo $1|sed 's/^[ \t]*#.*//'`
  PVALUE=`echo $1|cut -d= -f2`
  PVALUE=`echo $PVALUE|tr -d '\r'`
  if [ -z "$comment" ]; then
    PCOMMENT=1
  else
    PCOMMENT=0
  fi
}

#set_fileperms
#update the file permissions
set_fileperms() {
 chmod -R 775 *
 chmod 660 ./conf/agent.conf
}

set_agt_pids() {
    if [ -z "$PID_ONLY" ]
    then
      if [ "$OSNAME" = "freebsd" ]; then
        AGT_PIDS=`ps -axo pid,args | grep BOOMAGENTPROC | grep java | awk '{print $1}'`
      elif [ "$OSNAME" = "darwin" ]; then
        AGT_PIDS=`UNIX95=1 COLUMNS=254 ps -eo pid,command | grep BOOMAGENTPROC | grep java | awk '{print $1}'`
      else
        AGT_PIDS=`UNIX95=1 COLUMNS=254 ps -eo pid,args | grep BOOMAGENTPROC | grep java | awk '{print $1}'`
      fi
    else
      if [ -f "$BOOM_ROOT/boom_agent.pid" ]
      then
        AGTPID=`cat "$BOOM_ROOT/boom_agent.pid"`
        if [ -z "$AGTPID" ]
        then
          AGT_PIDS=
        else
          if [ "$OSNAME" = "freebsd" ]; then
            AGT_PIDS=`ps -p $AGTPID -axo pid,args | grep BOOMAGENTPROC | grep java | awk '{print $1}'`
          elif [ "$OSNAME" = "darwin" ]; then
            AGT_PIDS=`UNIX95=1 COLUMNS=254 ps -p $AGTPID -o pid,command | grep BOOMAGENTPROC | grep java | awk '{print $1}'`
          else
            AGT_PIDS=`UNIX95=1 COLUMNS=254 ps -p $AGTPID -o pid,args | grep BOOMAGENTPROC | grep java | awk '{print $1}'`
          fi
        fi
      else
        AGT_PIDS=
      fi
    fi
    return 0
}

 #Send kill to all processes of AGT_PIDS
 #Parameter $1 is empty or the kill signal 
stop_pids() { 
    if [ -z "$1" ]; then
      KSIG=""
    else
      KSIG=$1
    fi
    if [ -z "$AGT_PIDS" ]; then 
      return 0
    fi
    KILL_OK="YES"
    for i in $AGT_PIDS
    do
      if kill $KSIG $i; then
        continue           
      else
        KILL_OK="NO"
        echo "Unable to stop process $i"
      fi 
    done
    if [ "$KILL_OK" = "YES" ]; then
      return 0
    else
      return 1
    fi 
}

force_stop() {
    set_agt_pids
    if [ -z "$AGT_PIDS" ]; then 
      return 0
    fi
    if [ "$OSNAME" = "darwin" ]; then
      launchctl unload /Library/LaunchDaemons/boom_agent.plist
      set_agt_pids
    fi
    stop_pids 
    sleep 2
    set_agt_pids
    if [ -z "$AGT_PIDS" ]; then 
      return 0
    fi
    stop_pids -9
    set_agt_pids
    if [ -z "$AGT_PIDS" ]; then 
      return 0
    else 
      echo "BOOM Agent could not be stopped."
      return 1
    fi
}

stop_agent() {
    set_agt_pids
    if [ -z "$AGT_PIDS" ]; then 
      return 0
    fi
    if [ "$OSNAME" = "darwin" ]; then
      launchctl unload /Library/LaunchDaemons/boom_agent.plist
      set_agt_pids
    fi
    stop_pids
    return $?
}

start_agent() {
    cd "$BOOM_ROOT"
    if [ -f "boom_agent.jar.tmp" ]; then
      mv -f boom_agent.jar.tmp boom_agent.jar 
      rc=$?
      if [ "$rc" != "0" ]; then
         echo "Installation failed."
         echo "Cannot 'mv -f boom_agent.jar.tmp boom_agent.jar'."
         echo "Exit."
         exit 2
      fi
    fi
    ./boom_agt -start
    cd "$curdir"
    return 0
}


#get_init_sys
#determine which init mechanism is used on the current system 
get_init_sys() {
  SYS_INIT=
  if [ "$OSNAME" = "freebsd" ]; then
    init_s=`ps -aux | grep "init" | grep -v grep | awk '{print $2}' | grep -w "1"`
  else
    init_s=`ps -ef | grep "init" | grep -v grep | awk '{print $2}' | grep -w "1"`
  fi
  if [ "$init_s" = "1" ]; then
     SYS_INIT="init"
     return 0
  fi
  
  if [ "$OSNAME" = "sun" ]; then
     SYS_INIT="init"
     return 0
  fi

  if [ "$OSNAME" = "freebsd" ]; then
    init_s=`ps -aux | grep "systemd" | grep -v grep | awk '{print $2}' | grep -w "1"`
  else
    init_s=`ps -ef | grep "systemd" | grep -v grep | awk '{print $2}' | grep -w "1"`
  fi
  if [ "$init_s" = "1" ]; then
     SYS_INIT="systemd"
     return 0
  fi

  if [ -z "$SYS_INIT" ]; then
    echo "The Init System used on this server is not or not fully supported by this install skript"
    echo "Some manual installation steps might be required"
    SYS_INIT="unkown"
    return 1
  fi

  return 0

}


#get_osname
#parse the uname information and set the OSNAME variable
get_osname() {
  OSNAME=
  osn=`uname | grep -i "Linux"`
  if [ -n "$osn" ]; then
    OSNAME="linux"
    osn=`uname -r | grep -i ELvmnix`
    if [ -n "$osn" ]; then
      OSNAME="esx"
    fi
    return
  fi
  osn=`uname | grep -i "Sun"`
  if [ -n "$osn" ]; then
    OSNAME="sun"
    return
  fi
  osn=`uname | grep -i "HP-UX"`
  if [ -n "$osn" ]; then
    OSNAME="hp-ux"
    return
  fi
  osn=`uname | grep -i "FreeBSD"`
  if [ -n "$osn" ]; then
    OSNAME="freebsd"
    return
  fi
  osn=`uname | grep -i "Darwin"`
  if [ -n "$osn" ]; then
    OSNAME="darwin"
    return
  fi
  osn=`uname | grep -i "AIX"`
  if [ -n "$osn" ]; then
    OSNAME="aix"
    return
  fi
  if [ -z "$OSNAME" ]; then
    OSNAME=`uname`
    echo "This Operating System ($OSNAME) is not or not fully supported by this install skript"
    echo "Some manual installation steps might be required"
    OSNAME="unkown"
  fi
}

#parse_agentconf
#read the current agent.conf file and set variables
#to current settings if they are not set by command line parameters.
#All unhandled lines are copied directly
#to the new configuration file
parse_agentconf() {
 exec 3<./conf/agent.conf
 cat /dev/null >$NEWCONF
 while read line <&3; do
   parse_value "$line"
   if [ $PCOMMENT -ne 0 ]; then  #just write comment lines
     echo $line >>$NEWCONF
   else
     case $line in
       *AGENT_HOST=*)
           if [ -z "$AGENT_HOST" ]; then
             AGENT_HOST="$PVALUE"
           fi #ignore value if it is set by Silent Install Parameter
         ;;
       *AGENT_IP=*)
           if [ -z "$AGENT_IP" ]; then
             AGENT_IP="$PVALUE"
           fi
         ;;
       *AGENT_PORT=*)
           if [ -z "$AGENT_PORT" ]; then
             AGENT_PORT="$PVALUE"
           fi
         ;;
	   *AGENT_TLS_PORT=*)
           if [ -z "$AGENT_TLS_PORT" ]; then
             AGENT_TLS_PORT="$PVALUE"
           fi
         ;;
       *CLONING_SUPPORT=*)
           if [ -z "$CLONING_SUPPORT" ]; then
             CLONING_SUPPORT="$PVALUE"
           fi
         ;;
       *CLUSTER_NODES=*)
           if [ -z "$CLUSTER_NODES" ]; then
             CLUSTER_NODES="$PVALUE"
           fi
         ;;
	   *COMM_TYPE=*)
           if [ -z "$COMM_TYPE" ]; then
             COMM_TYPE="$PVALUE"
           fi
         ;;
       *FIXED_IP=*)
           if [ -z "$FIXED_IP" ]; then
             FIXED_IP="$PVALUE"
           fi
         ;;
       *FIXED_NAME=*)
           if [ -z "$FIXED_NAME" ]; then
             FIXED_NAME="$PVALUE"
           fi
         ;;
       *LOGCOUNT=*)
           if [ -z "$LOGCOUNT" ]; then
             LOGCOUNT="$PVALUE"
           fi
         ;;
       *LOGLEVEL=*)
           if [ -z "$LOGLEVEL" ]; then
             LOGLEVEL="$PVALUE"
           fi
         ;;
       *LOGSIZE=*)
           if [ -z "$LOGSIZE" ]; then
            LOGSIZE="$PVALUE"
           fi
         ;;
       *MAIN_SERVER_IP=*)
           if [ -z "$MAIN_SERVER_IP" ]; then
             MAIN_SERVER_IP="$PVALUE"
           fi
         ;;
       *MAIN_SERVER_NAME=*)
           if [ -z "$MAIN_SERVER_NAME" ]; then
             MAIN_SERVER_NAME="$PVALUE"
           fi
         ;;
       *MAIN_SERVER_PORT=*)
           if [ -z "$MAIN_SERVER_PORT" ]; then
             MAIN_SERVER_PORT="$PVALUE"
           fi
         ;;
       *SHARED_IP=*)
           if [ -z "$SHARED_IP" ]; then
             SHARED_IP="$PVALUE"
           fi
         ;;
       *MODE=*)
           if [ -z "$MODE" ]; then
             MODE="$PVALUE"
           fi
         ;;
	   *PROTOCOL_VERSION=*)
	       if [ -z "$PROTOCOL_VERSION" ]; then
	    	 PROTOCOL_VERSION="$PVALUE"
	       fi
	     ;;
       *) echo $line >>$NEWCONF
         ;;
     esac
   fi
 done
 exec 3<&-
}

#proc_agentconf
#add all specified configuration settings to the intermediate agent conf file
proc_agentconf() {
  if [ -z "$SILENT" ]; then
    query_string "Please specify the agent port that should be used." "${AGENT_PORT:-"23021"}"
    AGENT_PORT="$QUERYSTRING"

    query_string "Please specify communication type [SOCKET|TLS]." "${COMM_TYPE:-"SOCKET"}"
    COMM_TYPE="$QUERYSTRING"
    if [ "$COMM_TYPE" = "TLS" ]; then
      query_string "Please specify the agent TLS port that should be used." "${AGENT_TLS_PORT:-"23031"}"
      AGENT_TLS_PORT="$QUERYSTRING"
	  # set encryption off
	  PROTOCOL_VERSION=0
	else
	  PROTOCOL_VERSION=2
    fi

	query_ync "Should the agent use heartbeats and actively send data to the server (Y:MODE=0 no:MODE=7)?" "Y"
    if [ "$?" = "0" ]; then
		MODE="0"
	else	
		MODE="7"
	fi

    query_string "Please specify the server port that should be used (common for all agents)." "${MAIN_SERVER_PORT:-"23020"}"
    MAIN_SERVER_PORT="$QUERYSTRING"
    QUERYSTRING=
    while [ -z "$QUERYSTRING" ]; do
      query_string "Please enter the full qualified name of the Management Server." "$MAIN_SERVER_NAME"
    done
    MAIN_SERVER_NAME="$QUERYSTRING"
  fi

  if [ -z "${MAIN_SERVER_NAME}" ]; then
    echo "Error: can not continue without boom server name. Please set -sh commandline flag or MAIN_SERVER_NAME in conf/agent.conf."
    exit 3
  fi

  cont=1
  LOOKUP_CMD=
  rc=
  which nslookup 1>/dev/null 2>&1
  rc=$?
  if [ "$rc" = "0" ]; then
     LOOKUP_CMD="nslookup"
  else
     which host 1>/dev/null 2>&1
     rc=$?
     if [ "$rc" = "0" ]; then
        LOOKUP_CMD="host"
     else
        if [ -z "$SILENT" ]; then
           query_ync "DNS Lookup command is not found. Continue anyway?" "Y"
           answ=$?
        else
           echo "Error: DNS Lookup command is not found. Continuing anyway"
           answ=0
        fi
        case $answ in
          0)
             cont=
             ;;
          1)
             exit 1
             ;;
          2)
             exit 2
             ;;
        esac
     fi
  fi

  if [ "$cont" = "1" ]; then
     rc=
     $LOOKUP_CMD ${MAIN_SERVER_NAME} 1>/dev/null 2>&1
     rc=$?
     if [ "$rc" != "0" ]; then
        if [ -z "$SILENT" ]; then
           query_ync "DNS Lookup of $MAIN_SERVER_NAME failed. Continue anyway?" "Y"
           answ=$?
        else
           echo "Error: DNS Lookup of $MAIN_SERVER_NAME failed. Continuing anyway"
           answ=0
        fi
        case $answ in
          1)
             exit 1
             ;;
          2)
             exit 2
             ;;
         esac
     fi
  fi

  if [ -n "$AGENT_HOST" ]; then
    echo "AGENT_HOST=$AGENT_HOST" >>$NEWCONF
  fi
  if [ -n "$AGENT_IP" ]; then
    echo "AGENT_IP=$AGENT_IP" >>$NEWCONF
  fi
  if [ "$AGENT_PORT" != "23021" ]; then
    echo "AGENT_PORT=$AGENT_PORT" >>$NEWCONF
  fi
  if [ -n "$AGENT_TLS_PORT" ]; then
    echo "AGENT_TLS_PORT=$AGENT_TLS_PORT" >>$NEWCONF
  fi
  if [ -n "$COMM_TYPE" ]; then
    echo "COMM_TYPE=$COMM_TYPE" >>$NEWCONF
  fi
  if [ -n "$CLONING_SUPPORT" ]; then
    echo "CLONING_SUPPORT=$CLONING_SUPPORT" >>$NEWCONF
  fi
  if [ -n "$CLUSTER_NODES" ]; then
    echo "CLUSTER_NODES=$CLUSTER_NODES" >>$NEWCONF
  fi
  if [ -n "$FIXED_IP" ]; then
    echo "FIXED_IP=$FIXED_IP" >>$NEWCONF
  fi
  if [ -n "$FIXED_NAME" ]; then
    echo "FIXED_NAME=$FIXED_NAME" >>$NEWCONF
  fi
  if [ -n "$LOGCOUNT" ]; then
    echo "LOGCOUNT=$LOGCOUNT" >>$NEWCONF
  fi
  if [ -n "$LOGLEVEL" ]; then
    echo "LOGLEVEL=$LOGLEVEL" >>$NEWCONF
  fi
  if [ -n "$LOGSIZE" ]; then
    echo "LOGSIZE=$LOGSIZE" >>$NEWCONF
  fi
  if [ -n "$MAIN_SERVER_IP" ]; then
    echo "MAIN_SERVER_IP=$MAIN_SERVER_IP" >>$NEWCONF
  fi
  echo "MAIN_SERVER_NAME=$MAIN_SERVER_NAME" >>$NEWCONF
  if [ -n "$SHARED_IP" ]; then
    echo "SHARED_IP=$SHARED_IP" >>$NEWCONF
  fi
  if [ "$MAIN_SERVER_PORT" != "23020" ]; then
    echo "MAIN_SERVER_PORT=$MAIN_SERVER_PORT" >>$NEWCONF
  fi
  if [ -n "$MODE" ]; then
    echo "MODE=$MODE" >>$NEWCONF
  fi
  if [ -n "$PROTOCOL_VERSION" ]; then
    echo "PROTOCOL_VERSION=$PROTOCOL_VERSION" >>$NEWCONF
  fi
}

#check_perl
#verify that a specified PERL is usable
check_perl() {
  if [ -n "$SILENT" ]; then
    if [ -n "$PERL_DIR" ]; then
      answ=`$PERL_DIR/perl -version 2>&1 | grep "This is perl"`
      if  [ -z "$answ" ]; then
        echo "Error: Failed to start $PERL_DIR/perl. Make sure that perl is installed in $PERL_DIR."
        #PERL_DIR=
      fi
    fi
  fi
}

# check if the default user has to start the agent
get_user() {
  rc=0
  if [ -z "$SILENT" ]; then
    query_ync "Should the agent be started under the default [$CURR_USER] user?" "Y"
    case $? in
      1) QUERYSTRING=
         while [ -z "$QUERYSTRING" ]; do
#           query_string "Please enter the user." "$CURR_USER"
           query_string "Please enter the user." ""
           check_user $QUERYSTRING
           rc=$?
           if [ "$rc" != "0" ]; then
              QUERYSTRING=
           else
              AGT_USER=$QUERYSTRING
           fi
         done
         ;;
      2) echo "Installation cancelled"
         exit 2
         ;;
    esac
  fi
}

# check if the fdescfs is mounted
check_mount() {
  fs=
  fs=`mount | awk '{print $1}' | grep "fdescfs"`
  if [ -z "$fs" ]; then
     if [ -z "$SILENT" ]; then
        query_ync "'fdescfs' is not mounted. Should it be mounted now?" "Y"
        case $? in
          0) rc=0
             mount -t fdescfs null /dev/fd
             rc=$?
             if [ "$rc" != "0" ]; then
                echo "Mount of fdescfs failed."
                echo "Please try to mount it manually 'mount -t fdescfs null /dev/fd'."
             fi
             ;;
          2) echo "Install cancelled."
             exit 2
             ;;
        esac
     else
        rc=0
        mount -t fdescfs null /dev/fd
        rc=$?
        if [ "$rc" != "0" ]; then
            echo "Mount of fdescfs failed."
            echo "Please try to mount it manually 'mount -t fdescfs null /dev/fd'."
        fi
     fi
  fi

  fs=
  fs=`grep "fdescfs" /etc/fstab`
  if [ -z "$fs" ]; then
     if [ -z "$SILENT" ]; then
        query_ync "'fdescfs' is not found in /etc/fstab. Should it be appended now?" "Y"
        case $? in
          0) echo "fdescfs /dev/fd fdescfs rw 0 0" >> /etc/fstab
             ;;
          2) echo "Install cancelled."
             exit 2
             ;;
        esac
     else
        echo "fdescfs /dev/fd fdescfs rw 0 0" >> /etc/fstab
     fi
  fi
}

#check if the user does exist
check_user() {
  tmp_str=
  if [ -n "$1" ]; then
     if [ "$OSNAME" = "darwin" ]; then
        tmp_str=`dscl . list /users | grep $1`
        if [ "X${tmp_str}Y" != "X${1}Y" ]; then
            echo "Error: Specified user '$1' does not exist."
            return 1
        fi
     else
        tmp_str=`grep $1 /etc/passwd`
        if [ -n "$tmp_str" ]; then
           tmp_usr=`echo $tmp_str | cut -d: -f1`
           if [ "X${tmp_usr}Y" != "X${1}Y" ]; then
             echo "Error: Specified user '$1' does not exist."
             return 1
           fi
        else
           echo "Error: Specified user '$1' does not exist."
           return 1
        fi
     fi
  else
    echo "Error: Empty user."
    return 1 
  fi
  return 0
}

check_comm_type() {
  if [ "$COMM_TYPE" = "SOCKET" ] || [ "$COMM_TYPE" = "TLS" ]; then
    return 0
  fi
  echo "Error: Specified COMM_TYPE: '$1' - does not supported."
  return 1
}

#check_java
#verify that Java is in the search patch and
#that a correct version is used.
check_java() {
  if [ -n "$SILENT" ]; then
    if [ -n "$JAVA_DIR" ]; then
      answ=`$JAVA_DIR/java -version 2>&1 | grep "version"`
      if  [ -z "$answ" ]; then
        echo "Error: Failed to start $JAVA_DIR/java. Trying java in default search path..."
        JAVA_DIR=
      fi
    fi
    if [ -z "$JAVA_DIR" ]; then
      whichstr=`which java|grep -v "no java"`
      newdir=`echo "$whichstr"|sed "s/java$//"`
      answ=`$newdir/java -version 2>&1 | grep "version"`
      if  [ -z "$answ" ]; then
        echo "Error: no usable Java version found. Exiting..."
        exit 4
      else
        JAVA_DIR="$newdir"
      fi
    fi
    echo "Information: Using $JAVA_DIR/java"
  else
    #Interactive Install
    query_ync "The path to Java can be hardcoded in the BOOM server scripts (i.e. the rc start/stop files). If this is not selected, ensure that the path to Java is in the PATH variable of the init process. Should the path be hardcoded?" "Y"
    case $? in
     0) if [ -n "$JAVA_DIR" ]; then
          NEW_DIR="$JAVA_DIR"
          JAVA_DIR=
        else
          BOOM_ROOT_JAVA_DIR=$BOOM_ROOT/../java/bin
          answ=`$BOOM_ROOT_JAVA_DIR/java -version 2>&1 | grep "version"`
          if [ -n "$answ" ]; then
             NEW_DIR=`echo $BOOM_ROOT_JAVA_DIR | sed "s/agent\/..\///"`
          else
            whichstr=`which java|grep -v "no java"`
            NEW_DIR=`echo "$whichstr"|sed "s/java$//"`
            if [ -z "$NEW_DIR" ]; then
              echo "Failed to start Java. Please make sure that the Java runtime 1.5 or later is installed and in the system PATH."
              NEW_DIR="/opt/java/bin"
            fi
          fi
        fi
        while [ -z "$JAVA_DIR" ]; do
          query_string "Please enter the full path to the java executable." "$NEW_DIR"
          answ=`$QUERYSTRING/java -version 2>&1 | grep "version"`
          if  [ -z "$answ" ]; then
            echo "Failed to start $QUERYSTRING/java."
          else
            JAVA_DIR="$QUERYSTRING"
          fi
        done  
        SKIPJAVA=
        ;; 
      1) echo "Please make sure that Java is installed and the Path is set before the agent gets started"
        SKIPJAVA=1
        ;;
      2) echo "Installation cancelled"
         exit 2
        ;;
    esac
  fi
  if [ -z "$SKIPJAVA" ]; then
    jver=`echo $answ|sed 's/.*version.*1\.\([1-9]\)\..*/\1/'`
    if [ "$jver" -lt 5 ]; then
      echo "Java must be Version 1.5 or later. The installed version is:"
      echo $answ|sed 's/.*version.*\(1\.[1-9]\.[0-9_]*\).*/\1/'
      echo "Please upgrade Java and rerun the installation script."
      exit 2
    fi
  fi
}

#get_rclevel
#query if and at which runlevel the agent should be started
get_rclevel() {
  RC_DIR=
  RC_LEVEL=
  if [ "$OSNAME" = "unkown" ]; then
    echo "Error: The installer currently does not support for this operating system the automatic creation of the necessary configurations to automatically start/stop the agent with the init process. Please create the necessary entries manually."
    return 0
  fi
  if [ -z "$SILENT" ]; then
    query_ync "Should the script add the according entries in the run level directories to start and stop the agent automatically at system boot time?" "Y"
    answ=$?
  else
    answ=0
  fi

  case $answ in
    0) case "$OSNAME" in
         freebsd) if [ -d "/usr/local/etc/rc.d" ]; then
                      RC_DIR="/usr/local/etc/rc.d" 
                      INIT_DIR="/usr/local/etc/rc.d" 
                    elif [ -d "/etc/rc.d" ]; then
                      RC_DIR="/etc/rc.d"
                      INIT_DIR="/etc/rc.d"
                    fi
                    RC_LEVEL="X"
           ;;
         darwin) RC_DIR="/Library/LaunchDaemons"
                 INIT_DIR="/Library/LaunchDaemons"
                 RC_LEVEL="X"
           ;;
         *) if [ -n "$RUN_LEVEL" ]; then
              RC_LEVEL=`echo "$RUN_LEVEL"|sed 's/\([2-5]*\).*/\1/'`
              if [ -z "$RC_LEVEL" ]; then
                 echo "Error: invalid Run Level specified: ${RUN_LEVEL}. Trying to determine the current runlevel."
              fi
            fi

            if [ -z "$RC_LEVEL" ]; then
               case "$OSNAME" in
                 linux) RC_LEVEL=`runlevel|cut -d' ' -f2|sed 's/\([0-9]*\).*/\1/'`
                        if [ -z "$RC_LEVEL" ]; then
                          echo "Error: failed to determine current run level. Trying runlevel 3"
                          RC_LEVEL=3
                        fi
                 ;;
                 esx) RC_LEVEL=`runlevel|cut -d' ' -f2|sed 's/\([0-9]*\).*/\1/'`
                      if [ -z "$RC_LEVEL" ]; then
                        echo "Error: failed to determine current run level. Trying runlevel 3"
                        RC_LEVEL=3
                      fi
                 ;;
                 sun) RC_LEVEL=`who -r | nawk '{ print $3}'`
                      if [ -z "$RC_LEVEL" ]; then
                        echo "Error: failed to determine current run level. Trying runlevel 3"
                        RC_LEVEL=3
                      fi
                 ;;
                 aix) RC_LEVEL=`who -r | awk '{ print $2}'`
                      if [ -z "$RC_LEVEL" ]; then
                        echo "Error: failed to determine current run level. Trying runlevel 2"
                        RC_LEVEL=2
                      fi
                 ;;
                 hp-ux) RC_LEVEL=`who -r | awk '{ print $3}'`
                      if [ -z "$RC_LEVEL" ]; then
                        echo "Error: failed to determine current run level. Trying runlevel 3"
                        RC_LEVEL=3
                      fi
                 ;;
                 *) RC_LEVEL=3
                    ;;
               esac
            fi

            if [ -z "$SILENT" ]; then
               DEFAULT=$RC_LEVEL
               if [ "$OSNAME" = "sun" ]; then
                  query_number "At which runlevel should the agent be started or stopped (2-4)?" "$DEFAULT"
                  if [ $QUERYNUM -lt 2 ] || [ $QUERYNUM -gt 4 ]; then
                     echo "Wrong runlevel specified. Default '$DEFAULT' will be used."
                  else
                     RC_LEVEL="$QUERYNUM"
                  fi
               else
                  query_number "At which runlevel should the agent be started or stopped (2-5)?" "$DEFAULT"
                  if [ $QUERYNUM -lt 2 ] || [ $QUERYNUM -gt 5 ]; then
                     echo "Wrong runlevel specified. Default '$DEFAULT' will be used."
                  else
                     RC_LEVEL="$QUERYNUM"
                  fi
               fi
            fi 

            if [ -d "/etc/rc${RC_LEVEL}.d" ]; then
              RC_DIR="/etc/rc"
            elif [ -d "/sbin/rc${RC_LEVEL}.d" ]; then
              RC_DIR="/sbin/rc"
            elif [ -d "/etc/init.d/rc${RC_LEVEL}.d" ]; then
              RC_DIR="/etc/init.d/rc"
            elif [ -d "/usr/local/etc/rc${RC_LEVEL}.d" ]; then
              RC_DIR="/usr/local/etc/rc"
            elif [ -d "/etc/rc${RC_LEVEL}.d" ]; then
              RC_DIR="/etc/rc"
            elif [ -d "/etc/rc.d/rc${RC_LEVEL}.d" ]; then
              RC_DIR="/etc/rc.d/rc"
            fi
            if [ -d "/etc/init.d" ]; then
              INIT_DIR="/etc/init.d" 
            elif [ -d "/sbin/init.d" ]; then
              INIT_DIR="/sbin/init.d"
            elif [ -d "/usr/local/etc/rc.d" ]; then
              INIT_DIR="/usr/local/etc/rc.d"
            elif [ -d "/etc/rc.d/init.d" ]; then
              INIT_DIR="/etc/rc.d/init.d"
            elif [ -d "/etc/rc.d" ]; then
              INIT_DIR="/etc/rc.d"
            fi
           ;;
       esac 
       if [ -z "$RC_DIR" ] || [ -z "$INIT_DIR" ]; then
        if [ "$SYS_INIT" != "systemd" ]; then
         echo "Error: Could not identify the correct init.d and rc.d directories. Please configure the startup manually as described in ux-daemon.cfg/readme."
        fi
         RC_DIR=
         RC_LEVEL=
        INIT_DIR=

       fi
      ;;   
    2) echo "Installation cancelled"
       exit 2
      ;;   
  esac
}

#check_vmwareesx
#check if the system is a vmware system
check_vmwareesx() {
  ESXFW=
  if [ "$OSNAME" = "esx" ]; then
    if [ -z "$SILENT" ]; then
      query_ync "Register the Agent with the firewall and open up the ports?" "y"
      answ=$?
    else
      answ=0
    fi
    case $answ in
      0) if [ -d /etc/vmware/firewall ]; then
           ESXFW="Yes"
         else
           echo "Warning: The firewall service configuration directory /etc/vmware/firewall was not found or writeable."
           echo "The Agent registration will be skipped."
           echo "The Agent registration file ux-daemon.cfg/BOOMAgent.xml needs to be copied to /etc/vmware/firewall"
           echo "and the ports need to be opened by executing esxcfg-firewall --e BOOMAgent."
         fi
       ;;
      1) echo "Please open up the incoming and outgoing ports for the agent to server communication manually."
         echo "The agent registration file ux-daemon.cfg/BOOMAgent.xml needs to be copied to /etc/vmware/firewall"
         echo "and the ports need to be opened by executing esxcfg-firewall --e BOOMAgent."
        ;;
      2) echo "Installation cancelled."
         exit 2
        ;;
    esac
  fi
}

#proc_vmwareesx
#actually process the VMWare Firewall config file
proc_vmwareesx() {
 echo "<!-- Firewall configuration information for BOOM Agent -->
<ConfigRoot>
  <service>
    <id>BOOMAgent</id>
    <rule id='0000'>
      <direction>inbound</direction>
      <protocol>tcp</protocol>
      <port type='dst'>
        <begin>${AGENT_PORT}</begin>
        <end>${AGENT_PORT}</end>
      </port>
    </rule> 
    <rule id='0001'>
      <direction>outbound</direction>
      <protocol>tcp</protocol>
      <port type='dst'>
        <begin>${MAIN_SERVER_PORT}</begin>
        <end>${MAIN_SERVER_PORT}</end>
      </port>
    </rule>
  </service>
</ConfigRoot>
" >"ux-daemon.cfg/BOOMAgent.new"
  mv ux-daemon.cfg/BOOMAgent.xml ux-daemon.cfg/BOOMAgent.$$
  rc=$?
  if [ "$rc" != "0" ]; then
    echo "Installation failed."
    echo "Cannot 'mv ux-daemon.cfg/BOOMAgent.xml ux-daemon.cfg/BOOMAgent.$$'."
    echo "Exit."
    exit 2
  fi

  mv ux-daemon.cfg/BOOMAgent.new ux-daemon.cfg/BOOMAgent.xml
  rc=$?
  if [ "$rc" != "0" ]; then
    echo "Installation failed."
    echo "Cannot 'mv ux-daemon.cfg/BOOMAgent.new ux-daemon.cfg/BOOMAgent.xml'."
    echo "Exit."
    exit 2
  fi

}

#update_vmwareesx
#register the boom agent with the firewall and enable it
update_vmwareesx() {
  if [ -n "$ESXFW" ]; then
    proc_vmwareesx
    cp -fp ux-daemon.cfg/BOOMAgent.xml /etc/vmware/firewall/
    rc=$?
    if [ "$rc" != "0" ]; then
      echo "Installation failed."
      echo "Cannot 'cp -fp ux-daemon.cfg/BOOMAgent.xml /etc/vmware/firewall/'."
      echo "Exit."
      exit 2
    fi

    if esxcfg-firewall --e BOOMAgent; then
      echo "Firewall registration done."
      esxcfg-firewall -q BOOMAgent
    else
      echo "Error: failed to open ports for the boom agent"
    fi
  fi
}

#proc_macosx
#move the agents registration file to the according system directory
#register the boom agent with the launchctl daemon which will start the agent
proc_macosx() {
	echo "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
	<plist version=\"1.0\">
	    <dict>
	        <key>Label</key>
	        <string>com.blixx.boom.agent</string>

	        <key>Disabled</key>
	        <false/>
			
	        <key>KeepAlive</key>
	        <false/>
			
	        <key>UserName</key>
	        <string>root</string>

	        <key>GroupName</key>
	        <string>wheel</string>

	        <key>ProgramArguments</key>
	            <array>
	                <string>${BOOM_ROOT}/boom_agt</string>
	                <string>-startfg</string>
	             </array>	
	        <key>RunAtLoad</key>
	        <true/>
	    </dict>
	</plist>
	" > "boom_agent.plist"
  chmod 640 boom_agent.plist
  cp -f boom_agent.plist "$INIT_DIR"
  rc=$?
  if [ "$rc" != "0" ]; then
    echo "Installation failed."
    echo "Cannot 'cp -f boom_agent.plist $INIT_DIR'."
    echo "Exit."
    exit 2
  fi

  chmod 640 "$INIT_DIR"/boom_agent.plist
  launchctl load "$INIT_DIR"/boom_agent.plist
}

# update_boomagt_cfg
# update the boom_agt.cfg file and ux-daemon.cfg/boom_agt.cfg
# with the selected configuration values
update_boomagt_cfg() {

  proc_boom_agt_cfg "./boom_agt.cfg.new"
  mv ./boom_agt.cfg ./boom_agt.cfg.$$ 1>/dev/null 2>&1

  mv ./boom_agt.cfg.new ./boom_agt.cfg
  rc=$?
  if [ "$rc" != "0" ]; then
     echo "Installation failed."
     echo "Cannot 'mv ./boom_agt.cfg.new ./boom_agt.cfg'."
     echo "Exit."
     exit 2
  fi

# initd is used on the server ->
# prepare the corresponding boom_agt file.

  if [ "$SYS_INIT" = "init" ] && [ "$OSNAME" != "darwin" ]; then
    proc_boom_agt_cfg_d "ux-daemon.cfg/boom_agt" "ux-daemon.cfg/boom_agt.new"
    mv ux-daemon.cfg/boom_agt ux-daemon.cfg/boom_agt.$$
    rc=$?
    if [ "$rc" != "0" ]; then
       echo "Installation failed."
       echo "Cannot 'mv ux-daemon.cfg/boom_agt ux-daemon.cfg/boom_agt.$$'."
       echo "Exit."
       exit 2
    fi

    mv ux-daemon.cfg/boom_agt.new ux-daemon.cfg/boom_agt
    rc=$?
    if [ "$rc" != "0" ]; then
       echo "Installation failed."
       echo "Cannot 'mv ux-daemon.cfg/boom_agt.new ux-daemon.cfg/boom_agt'."
       echo "Exit."
       exit 2
    fi
  fi

# systemd is used on the server -> 
# prepare the corresponding boom_agt.service file.
  if [ "$SYS_INIT" = "systemd" ]; then
    proc_boom_agt_cfg_systemd "ux-daemon.cfg/boom_agt.service" "ux-daemon.cfg/boom_agt.service.new"
    mv ux-daemon.cfg/boom_agt.service ux-daemon.cfg/boom_agt.service.$$
    rc=$?
    if [ "$rc" != "0" ]; then
       echo "Installation failed."
       echo "Cannot 'mv ux-daemon.cfg/boom_agt.service ux-daemon.cfg/boom_agt.service.$$'."
       echo "Exit."
       exit 2
    fi

    mv ux-daemon.cfg/boom_agt.service.new ux-daemon.cfg/boom_agt.service
    rc=$?
    if [ "$rc" != "0" ]; then
       echo "Installation failed."
       echo "Cannot 'ux-daemon.cfg/boom_agt.service.new ux-daemon.cfg/boom_agt.service'."
       echo "Exit."
       exit 2
    fi
## provide section Service boom_agt.service with entry "User=<usr>" if AGT_USER 
## contains CHARs by substitution of "Service]" with "Service]\nUser=.."
    echo "${AGT_USER}" | grep "[a-zA-Z]" >/dev/null; rc=$?
    if [ ${rc} -eq 0 ]
    then
      sed 's/Service\]/Service\]\nUser='"${AGT_USER}"'/' ux-daemon.cfg/boom_agt.service >ux-daemon.cfg/boom_agt.service.tmp
      mv ux-daemon.cfg/boom_agt.service.tmp ux-daemon.cfg/boom_agt.service
      if [ $? -ne 0 ]; then
       echo "Installation failed."
       echo "Cannot 'mv ux-daemon.cfg/boom_agt.service.tmp ux-daemon.cfg/boom_agt.service'."
       echo "Exit."
       exit 2
     fi
    fi
  fi
}

#check_nc
#check if nc (netcat) is available and set
#netcat.sh accordingly
check_nc() {
  echo '#
# This flag determines if netcat (nc) or telnet 
# should be used to connect to the agent.
# netcat is faster and more reliable, but not available on all platforms.
# Set USE_NC=0 to use telnet
# Set USE_NC=1 to use nc
# Set USE_NC=2 to use netcat' >spi/netcat.sh

  PATH=./spi:$PATH

# A bit weird redirection of stderr, but necessary for SPARC SunOS 10
  exec 4>&2
  exec 2>/dev/null
  ncstr=`nc -h | grep hostname`
  if [ -z "$ncstr" ]; then
    ncstr=`netcat -h | grep hostname`
    if [ -z "$ncstr" ]; then
      echo "USE_NC=0" >>spi/netcat.sh
    else
      echo "USE_NC=2" >>spi/netcat.sh
    fi
  else
    echo "USE_NC=1" >>spi/netcat.sh
  fi
  exec 2>&4
  exec 4>&-
  chmod 775 spi/netcat.sh
} 


# proc_boom_agt_cfg
# create the boom_agt.cfg config file
# Params:
# $1 filename of output file


proc_boom_agt_cfg() {

  cat /dev/null >"$1"
#insert the required configuration values
  echo "${BOOM_ROOT_STR}=\"$BOOM_ROOT"\" >>"$1"
  javabin=`grep ${JAVA_BIN_STR} ./boom_agt.cfg 2>/dev/null | grep -v "#"`
  if [ ! -z "${javabin}" ] 
  then 
# take setting of JAVA_BIN from boom_agt.cfg
    echo "${javabin}" >>"$1"
  else
    if [ -n "$JAVA_DIR" ]; then
      echo "${JAVA_BIN_STR}=\"$JAVA_DIR"\" >>"$1"
    fi 
  fi
  if [ -n "$PERL_DIR" ]; then
    echo "${PERL_PATH_STR}=\"$PERL_DIR"\" >>"$1"
  fi
  agtuser=`grep ${USER_STR} ./boom_agt.cfg 2>/dev/null | grep -v "#"`
  if [ ! -z "${agtuser}" ]
  then 
# take setting of USER from boom_agt.cfg
    echo "${agtuser}" >>"$1"
  else
    if [ -n "$AGT_USER" ]; then
      echo "${USER_STR}=\"$AGT_USER"\" >>"$1"
    else
      if [ -n "$CURR_USER" ]; then
        echo "${USER_STR}=\"$CURR_USER"\" >>"$1"
      fi
    fi 
  fi
  javaopts=`grep ${JAVA_OPTS_STR} ./boom_agt.cfg 2>/dev/null | grep -v "#"`
  if [ ! -z "${javaopts}" ]
  then 
# take setting of JAVA_OPTS from boom_agt.cfg
    echo "${javaopts}" >>"$1"
  else
    echo "${JAVA_OPTS_STR}=\"$JAVA_OPTS"\" >>"$1"
  fi
}



proc_boom_agt_cfg_d() {
d_start=
d_stop=

 if [ "$RC_LEVEL" = "2" ]; then
    d_start="2 3 4 5"
    d_stop="0 1 6"
 elif [ "$RC_LEVEL" = "3" ]; then
    d_start="3 4 5"
    d_stop="0 1 2 6"
 elif [ "$RC_LEVEL" = "4" ]; then
    d_start="4 5"
    d_stop="0 1 2 3 6"
 elif [ "$RC_LEVEL" = "5" ]; then
    d_start="5"
    d_stop="0 1 2 3 4 6"
 fi

 exec 3<"$1"
 cat /dev/null >"$2"
 #first process all comments
 iscomment=
 while [ -z "$iscomment" ]; do
   read line <&3
   iscomment=`echo $line|sed 's/^#.*//'`
   if [ -z "$iscomment" ]; then
      isfound=
      isfound=`echo $line | awk '/Default-Start:/'`
      if [ -n "$isfound" ]; then
        echo "# Default-Start: $d_start" >>"$2"
      else
        isfound=
        isfound=`echo $line | awk '/Default-Stop:/'`
        if [ -n "$isfound" ]; then
          echo "# Default-Stop: $d_stop" >>"$2"
        else
          echo $line >>"$2"
        fi
      fi
   fi
   if [ -z "$line" ]; then
     #break at first empty line
     break
   fi
 done
 #insert the new configuration values
 echo "BOOM_ROOT=$BOOM_ROOT" >>"$2"
 #process the rest of the file and remove any
 #further occurances of BOOM_ROOT, JAVA_BIN, PATH settings
 while read line <&3; do
   case $line in
     BOOM_ROOT=\`pwd\`*) echo "$line" >>"$2"
       ;;
     BOOM_ROOT=* ) ;;
     export\ BOOM_ROO* ) ;;
     JAVA_BIN=* ) ;;
     PERL_BIN=* ) ;;
     PATH=* ) ;;
     export\ PATH* ) ;;
     *) echo "$line" >>"$2"
       ;;
   esac
 done
 exec 3<&-
}

# proc_boom_agt_cfg_systemd
# replace the lines ExecStart=BOOM_AGT_CMD and WantedBy=WANTED_BY
# with the real values in boom_agt.service file.
# Params:
# $1 filename of input file
# $2 filename of output file
proc_boom_agt_cfg_systemd() {

 TARGET=
 if [ "$RC_LEVEL" = "5" ]; then
    TARGET="multi-user.target graphical.target"
 else
    TARGET="multi-user.target"
 fi

 exec 3<"$1"
 cat /dev/null >"$2"

 while read line
 do
   iscomment=
   iscomment=`echo $line|awk '/^#/'`
   if [ -n "$iscomment" ]; then
     echo "$line" >>"$2"
     continue
   fi

   isfound=
   isfound=`echo $line | awk '/PIDFile/'`
   if [ -n "$isfound" ]; then
     echo "PIDFile=${BOOM_ROOT}/boom_agent.pid" >>"$2"
     continue
   fi

   isfound=
   isfound=`echo $line | awk '/ExecStartPre/'`
   if [ -n "$isfound" ]; then
     echo "ExecStartPre=${BOOM_ROOT}/boom_agt -stop" >>"$2"
     continue
   fi

   isfound=
   isfound=`echo $line | awk '/ExecStart/'`
   if [ -n "$isfound" ]; then
     echo "ExecStart=${BOOM_ROOT}/boom_agt -start_wait" >>"$2"
     continue
   fi

   isfound=
   isfound=`echo $line | awk '/ExecStop/'`
   if [ -n "$isfound" ]; then
     echo "ExecStop=${BOOM_ROOT}/boom_agt -stop" >>"$2"
     continue
   fi

   isfound=
   isfound=`echo $line | awk '/WantedBy/'`
   if [ -n "$isfound" ]; then
     echo "WantedBy=${TARGET}" >>"$2"
     continue
   fi

   echo "$line" >>"$2"
 done <&3
 exec 3<&-
}


#
#proc_rc_conf_freebsd
#parse the /etc/rc.conf file and add/remove
#the service entry for the boom agent
proc_rc_conf_freebsd() {
  cat /etc/rc.conf | grep -v boom_agt_enable >/etc/rc.conf.new
  mv /etc/rc.conf /etc/rc.conf.boom
  rc=$?
  if [ "$rc" != "0" ]; then
     echo "Installation failed."
     echo "Cannot 'mv /etc/rc.conf /etc/rc.conf.boom'."
     echo "Exit."
     exit 2
  fi

  mv /etc/rc.conf.new /etc/rc.conf
  rc=$?
  if [ "$rc" != "0" ]; then
     echo "Installation failed."
     echo "Cannot 'mv /etc/rc.conf.new /etc/rc.conf'."
     echo "Exit."
     exit 2
  fi

  if [ "$1" = "add" ]; then
    echo "boom_agt_enable=\"YES\"" >>/etc/rc.conf
  fi
}

# install_systemd
# copy ux-daemon.cfg/boom_agt.service file to /usr/lib/systemd/system
# and register the boom_agt to be started at boot.
install_systemd() {
  INIT_DIR="/etc/systemd/system/."

  cp -fp ux-daemon.cfg/boom_agt.service $INIT_DIR
  rc=$?
  if [ "$rc" != "0" ]; then
     echo "Installation failed."
     echo "Cannot 'cp -fp ux-daemon.cfg/boom_agt.service $INIT_DIR'."
     echo "Exit."
     exit 2
  fi
 
  which systemctl 1>/dev/null 2>&1
  rc=$?
  if [ "$rc" != "0" ]; then
     echo "Installation failed."
     echo "'systemctl' is not found."
     echo "Exit."
     exit 2
  fi

  systemctl daemon-reload
  rc=$?
  if [ "$rc" != "0" ]; then
     echo "Installation failed."
     echo "Cannot 'systemctl daemon-reload'."
     echo "Exit."
     exit 2
  fi

  systemctl disable boom_agt.service 1>/dev/null 2>&1

  systemctl enable boom_agt.service 1>/dev/null 2>&1
  rc=$?
  if [ "$rc" != "0" ]; then
     echo "Installation failed."
     echo "Cannot 'systemctl enable boom_agt.service'."
     echo "Exit."
     exit 2
  fi
  
}



#install_rcs
#move the ux-daemon.cfg/boom_agt script to the init.d directory and
#create the necessary links in the rc directories.
#special handling for darwin (mac os/x)
install_rcs() {
  if [ -n "$RC_LEVEL" ]; then
   #here are the platform specifics
   #Linux expects to have full rc links on each level, level 6 is reboot
   #  some implementations reorder scripts, so ordernumber in link might be changed.
   #HP-UX expects start/kill link only on the repective level
   case "$OSNAME" in
     darwin) proc_macosx
       ;;
     freebsd) cp -fp ux-daemon.cfg/boom_agt "$INIT_DIR/"
              rc=$?
              if [ "$rc" != "0" ]; then
                 echo "Installation failed."
                 echo "Cannot 'cp -fp ux-daemon.cfg/boom_agt $INIT_DIR'."
                 echo "Exit."
                 exit 2
              fi

              proc_rc_conf_freebsd "add"
       ;;
     linux|esx)
          cp -fp ux-daemon.cfg/boom_agt "$INIT_DIR/"
          rc=$?
          if [ "$rc" != "0" ]; then
             echo "Installation failed."
             echo "Cannot 'cp -fp ux-daemon.cfg/boom_agt $INIT_DIR'."
             echo "Exit."
             exit 2
          fi

          for LEVEL in 0 1 2 3 4 5 ; do
            rm -f ${RC_DIR}${LEVEL}.d/K*boom_agt
            rm -f ${RC_DIR}${LEVEL}.d/S*boom_agt
            if [ "$LEVEL" -lt "$RC_LEVEL" ]; then
              ln -s "${INIT_DIR}/boom_agt" "${RC_DIR}${LEVEL}.d/K01boom_agt"
              rc=$?
              if [ "$rc" != "0" ]; then
                 echo "Installation failed."
                 echo "Cannot 'ln -s ${INIT_DIR}/boom_agt ${RC_DIR}${LEVEL}.d/K01boom_agt'."
                 echo "Exit."
                 exit 2
              fi

            else
              ln -s "${INIT_DIR}/boom_agt" "${RC_DIR}${LEVEL}.d/S99boom_agt"
              rc=$?
              if [ "$rc" != "0" ]; then
                 echo "Installation failed."
                 echo "Cannot 'ln -s ${INIT_DIR}/boom_agt ${RC_DIR}${LEVEL}.d/S99boom_agt'."
                 echo "Exit."
                 exit 2
              fi

            fi
          done
          rm -f ${RC_DIR}6.d/K*boom_agt
          ln -s "${INIT_DIR}/boom_agt" "${RC_DIR}6.d/K01boom_agt"
          rc=$?
          if [ "$rc" != "0" ]; then
              echo "Installation failed."
              echo "Cannot 'ln -s ${INIT_DIR}/boom_agt ${RC_DIR}6.d/K01boom_agt'."
              echo "Exit."
              exit 2
          fi

          insserv boom_agt 1>/dev/null 2>&1
       ;;
     sun) cp -fp ux-daemon.cfg/boom_agt "$INIT_DIR/"
          rc=$?
          if [ "$rc" != "0" ]; then
             echo "Installation failed."
             echo "Cannot 'cp -fp ux-daemon.cfg/boom_agt $INIT_DIR'."
             echo "Exit."
             exit 2
          fi

          for LEVEL in 0 1 2 3 ; do
            rm -f ${RC_DIR}${LEVEL}.d/K*boom_agt
            rm -f ${RC_DIR}${LEVEL}.d/S*boom_agt
            if [ "$LEVEL" -lt "$RC_LEVEL" ]; then
              ln -s "${INIT_DIR}/boom_agt" "${RC_DIR}${LEVEL}.d/K01boom_agt"
              rc=$?
              if [ "$rc" != "0" ]; then
                echo "Installation failed."
                echo "Cannot 'ln -s ${INIT_DIR}/boom_agt ${RC_DIR}${LEVEL}.d/K01boom_agt'."
                echo "Exit."
                exit 2
              fi

            elif [ "$LEVEL" -eq "$RC_LEVEL" ]; then
              ln -s "${INIT_DIR}/boom_agt" "${RC_DIR}${LEVEL}.d/S99boom_agt"
              rc=$?
              if [ "$rc" != "0" ]; then
                echo "Installation failed."
                echo "Cannot 'ln -s ${INIT_DIR}/boom_agt ${RC_DIR}${LEVEL}.d/S99boom_agt'."
                echo "Exit."
                exit 2
              fi

            fi
           done
          rm -f ${RC_DIR}S.d/K*boom_agt
          ln -s "${INIT_DIR}/boom_agt" "${RC_DIR}S.d/K01boom_agt"
          rc=$?
          if [ "$rc" != "0" ]; then
             echo "Installation failed."
             echo "Cannot 'ln -s ${INIT_DIR}/boom_agt ${RC_DIR}S.d/K01boom_agt'."
             echo "Exit."
             exit 2
          fi

       ;;
     hp*) cp -fp ux-daemon.cfg/boom_agt "$INIT_DIR/"
          rc=$?
          if [ "$rc" != "0" ]; then
             echo "Installation failed."
             echo "Cannot 'cp -fp ux-daemon.cfg/boom_agt $INIT_DIR'."
             echo "Exit."
             exit 2
          fi

          KLEVEL="$(( $RC_LEVEL - 1 ))"
          for LEVEL in 0 1 2 3 4 ; do
            rm -f ${RC_DIR}${LEVEL}.d/K*boom_agt
            rm -f ${RC_DIR}${LEVEL}.d/S*boom_agt
            if [ "$LEVEL" -eq "$KLEVEL" ]; then
               ln -s "${INIT_DIR}/boom_agt" "${RC_DIR}${LEVEL}.d/K001boom_agt"
               rc=$?
               if [ "$rc" != "0" ]; then
                 echo "Installation failed."
                 echo "Cannot 'ln -s ${INIT_DIR}/boom_agt ${RC_DIR}${LEVEL}.d/K001boom_agt'."
                 echo "Exit."
                 exit 2
               fi
            fi
            if [ "$LEVEL" -eq "$RC_LEVEL" ]; then
               ln -s "${INIT_DIR}/boom_agt" "${RC_DIR}${LEVEL}.d/S999boom_agt"
               rc=$?
               if [ "$rc" != "0" ]; then
                 echo "Installation failed."
                 echo "Cannot 'ln -s ${INIT_DIR}/boom_agt ${RC_DIR}${LEVEL}.d/S999boom_agt'."
                 echo "Exit."
                 exit 2
               fi

            fi
          done
       ;;
     aix) cp -fp ux-daemon.cfg/boom_agt "$INIT_DIR/"
          rc=$?
          if [ "$rc" != "0" ]; then
              echo "Installation failed."
              echo "Cannot 'cp -fp ux-daemon.cfg/boom_agt $INIT_DIR'."
              echo "Exit."
              exit 2
          fi

          for LEVEL in 2 3 4 5 6 7 8 9 ; do
            rm -f ${RC_DIR}${LEVEL}.d/K*boom_agt
            rm -f ${RC_DIR}${LEVEL}.d/S*boom_agt
            if [ "$LEVEL" -eq "$RC_LEVEL" ]; then
              ln -s "${INIT_DIR}/boom_agt" "${RC_DIR}${LEVEL}.d/K001boom_agt"
              rc=$?
              if [ "$rc" != "0" ]; then
                echo "Installation failed."
                echo "Cannot 'ln -s ${INIT_DIR}/boom_agt ${RC_DIR}${LEVEL}.d/K001boom_agt'."
                echo "Exit."
                exit 2
              fi

              ln -s "${INIT_DIR}/boom_agt" "${RC_DIR}${LEVEL}.d/S999boom_agt"
              rc=$?
              if [ "$rc" != "0" ]; then
                echo "Installation failed."
                echo "Cannot 'ln -s ${INIT_DIR}/boom_agt ${RC_DIR}${LEVEL}.d/S999boom_agt'."
                echo "Exit."
                exit 2
              fi
            fi
          done
       ;;
     *)
       ;;
   esac
  fi
}

#uninstall_boomagt
#stop the agent, remove the boom_agt configuration from system startup and
#remove all agent files
uninstall_boomagt() {

  get_init_sys

  if [ -z "$SILENT" ]; then
    query_ync "Uninstall the Agent ($BOOM_ROOT)?" "Y"
    case $? in
      1) echo "Please run the install script from the boom agent installation path."
         exit 2
         ;;
      2) echo "Uninstall cancelled."
         exit 2
         ;;
    esac
  fi

  echo "Information: Checking if Agent is running and stop it..."
  force_stop
 
  echo "Information: Removing the Agent daemon registration and runlevel entries..."
  RC_DIR=
  DONE=
  #here are the platform specifics
  case "$OSNAME" in
    darwin) if [ -r /Library/LaunchDaemons/boom_agent.plist ]; then
              launchctl unload /Library/LaunchDaemons/boom_agent.plist >/dev/null 2>&1
              rm -f /Library/LaunchDaemons/boom_agent.plist
            fi
            DONE=1
      ;;
    esx) insserv -r boom_agt 1>/dev/null 2>&1
         if [ -r /etc/vmware/firewall/BOOMAgent.xml ]; then
           echo "Information: Closing Port and Removing Firewall Setting for the boom Agent"
           esxcfg-firewall -d BOOMAgent
           rm -f /etc/vmware/firewall/BOOMAgent.xml
         fi
      ;;
    freebsd) if [ -r "/usr/local/etc/rc.d/boom_agt" ]; then
               rm -f "/usr/local/etc/rc.d/boom_agt"
             elif [ -r "/etc/rc.d/boom_agt" ]; then
               rm -f "/etc/rc.d/boom_agt"
             fi
             proc_rc_conf_freebsd
             DONE=1
           ;;
    linux)  if [ "$SYS_INIT" = "systemd" ]; then
               systemctl stop boom_agt.service 1>/dev/null 2>&1
               systemctl disable boom_agt.service 1>/dev/null 2>&1
               rm -f /usr/lib/systemd/system/boom_agt.service
            else
               insserv -r boom_agt 1>/dev/null 2>&1
            fi
      ;;
  esac

  if [ -z "$DONE" ]; then
    if [ -d "/etc/rc1.d" ]; then
      RC_DIR="/etc/rc"
    elif [ -d "/sbin/rc1.d" ]; then
      RC_DIR="/sbin/rc"
    elif [ -d "/etc/init.d/rc1.d" ]; then
      RC_DIR="/etc/init.d/rc"
    elif [ -d "/etc/rc.d/rc2.d" ]; then
      RC_DIR="/etc/rc.d/rc"
    fi
    if [ -d "/etc/init.d" ]; then
      INIT_DIR="/etc/init.d" 
    elif [ -d "/sbin/init.d" ]; then
      INIT_DIR="/sbin/init.d"
    elif [ -d "/etc/rc.d/init.d" ]; then
      INIT_DIR="/etc/rc.d/init.d"
    fi
    if [ -z "$RC_DIR" ] || [ -z "$INIT_DIR" ]; then
      echo "Error: Could not identify the correct init.d and rc.d directories. Please remove the daemon startup file and links manually."
    else
      rm -f "$INIT_DIR/boom_agt"
      rm -f ${RC_DIR}*.d/K*boom_agt
      rm -f ${RC_DIR}*.d/S*boom_agt
    fi
  fi
  if [ -z "$SILENT" ]; then
    query_ync "Remove all files including configuration and temporary files in $BOOM_ROOT?" "Y"
    case $? in
      1) echo "Agent files will remain. You can manually remove these files."
         exit 0
         ;;
      2) echo "Uninstall cancelled."
         exit 2
         ;;
    esac
  fi
  echo "Information: Removing all directories and files in $BOOM_ROOT"
  cd /
  rm -rf $BOOM_ROOT
  exit 0
}

#############################################
 #Install/DeInstall script
 # 1. identify directory
 # 2. stop agent if it is running
 # 3. parse agent.conf and query/update
 # 4. Check Java Path and version
 # 5. parse boom_agt and query/update
 # 6. move init scripts in place
#############################################
 #DeInstall:
 # 1. identify directory
 # 2. stop agent if it is running
 # 3. remove init scripts
 # 4. remove agent files

 curdir=`pwd`
 getdir install "$0"
 if [ -z "$PROG_DIR" ]; then
   echo "Error: Failed to identify the path for BOOM_ROOT."
   echo "Error: Please set the BOOM_ROOT variable to the Agent directory and rerun the script."
   exit 2
 fi
 cd "$PROG_DIR"
 NEWCONF="./conf/agent.newconf"
 BOOM_ROOT=`pwd`;export BOOM_ROOT

 #determine if echo -n or echo "...\c" should be used
 ccount=`echo -n "X" | wc -c` 
 if [ $ccount -gt 1 ]; then
   ECHON=
   ECHOSYS5="\c"
 else
   ECHON="-n"
   ECHOSYS5=
 fi

 CURR_USER=`id -un`

 get_osname
 parsecmdline "$@"

 echo "Set BOOM_ROOT="$BOOM_ROOT

 if [ -z "$SILENT" ]; then
   query_ync "Install Agent to $BOOM_ROOT?" "Y"
   case $? in
     1) echo "Please move the Agent files to the destination path and rerun the install script from there."
        exit 2
        ;;
     2) echo "Installation cancelled."
        exit 2
        ;;
   esac
 fi
 
 echo "Information: Checking if Agent is running and stop it..."
 force_stop
 parse_agentconf
 proc_agentconf
 check_java
 get_user
 check_perl
 get_rclevel
 check_vmwareesx
 if [ "$OSNAME" = "freebsd" ]; then
    check_mount
 fi

 
 echo "----------------------------"
 echo "Collection of information finished."
 echo ""
 echo "Selection Summary:"
 echo "Installation directory: $BOOM_ROOT"
 echo "BOOM Server:       $MAIN_SERVER_NAME"
 echo "BOOM Server Port:  $MAIN_SERVER_PORT"
 if [ -n "$MAIN_SERVER_IP" ]; then
   echo "BOOM Server IP:    $MAIN_SERVER_IP"
 fi
 if [ -n "$CLUSTER_NODES" ]; then
   echo "Allowed Servers:   $CLUSTER_NODES"
 fi
 if [ -n "$AGENT_HOST" ]; then
   echo "Fixed Agent Name:  $AGENT_HOST"
 fi
 if [ -n "$AGENT_IP" ]; then
   echo "Fixed Agent IP:    $AGENT_IP"
 fi
   echo "Agent Port:         $AGENT_PORT"
   echo "Agent TLS Port:     $AGENT_TLS_PORT"
   echo "Communication type: $COMM_TYPE"
 if [ -n "$SHARED_IP" ]; then
   echo "NAT/Shared IP:     $SHARED_IP"
 fi
 if [ -n "$RC_LEVEL" ]; then
   echo "Start at Runlevel: $RC_LEVEL"
 fi
 if [ -n "$LOGLEVEL" ]; then
   echo "Log Level:         $LOGLEVEL"
 fi
 if [ -n "$LOGCOUNT" ]; then
   echo "Number Logfiles:   $LOGCOUNT"
 fi
 if [ -n "$LOGSIZE" ]; then
   echo "Size per Logfile:  $LOGSIZE"
 fi
 if [ -n "$AGT_USER" ]; then
   echo "Start the Agent as User: $AGT_USER"
 fi


 if [ -z "$JAVA_DIR" ]; then
  echo "The path to java will not be hardcoded in the script files".
 else
  echo "The path to java ($JAVA_DIR) will be hardcoded in the script files".
 fi
 if [ -n "$PERL_DIR" ]; then
  echo "The path to perl ($PERL_DIR) will be hardcoded in the script files".
 fi
 if [ -z "$RC_LEVEL" ]; then
  echo "The run level directories will not be changed to enable automatic start/stop of the agent"
 else
  echo "The agent start/stop entries will be entered in the runlevel directories to startup at level $RC_LEVEL"
 fi
 if [ "$OSNAME" = "esx" ]; then
   if [ -n "$ESXFW" ]; then
     echo "The agent will be registered as Service with the firewall and the ports will be opened."
   else
     echo "The agent will not be registered as Service with the firewall."
   fi
 fi

 echo ""
 echo "----------------------------"

 if [ -z "$SILENT" ]; then
   query_ync "Perform this installation" "Y"
   case $? in
     1) echo "Configuration will not be updated. Exiting...."
        rm -f $NEWCONF
        exit 2
        ;;
     2) echo "Installation cancelled."
        rm -f $NEWCONF
        exit 2
        ;;
   esac
 fi

 mv ./conf/agent.conf ./conf/agent.$$
 rc=$?
 if [ "$rc" != "0" ]; then
    echo "Installation failed."
    echo "Cannot 'mv ./conf/agent.conf ./conf/agent.$$'."
    echo "Exit."
    exit 2
 fi

 mv $NEWCONF ./conf/agent.conf
 rc=$?
 if [ "$rc" != "0" ]; then
    echo "Installation failed."
    echo "Cannot 'mv $NEWCONF ./conf/agent.conf'."
    echo "Exit."
    exit 2
 fi

 get_init_sys

 set_fileperms
 check_nc
 update_boomagt_cfg
 set_fileperms

 if [ "$SYS_INIT" = "systemd" ]; then
    install_systemd
 else
    install_rcs
 fi

 update_vmwareesx

# change owner of $BOOM_ROOT to $AGT_USER (if specified)
 if [ -n "$AGT_USER" ]; then
   chown -R $AGT_USER $BOOM_ROOT
   rc=$?
   if [ "$rc" != "0" ]; then
      echo "Installation failed."
      echo "Cannot 'chown -R $AGT_USER $BOOM_ROOT'."
      echo "Exit."
      exit 2
   fi
 fi


 echo "Installation finished."
 echo ""
 if [ -z "$SILENT" ]; then
   query_ync "Start the agent?" "Y"
   case $? in
     0) start_agent
        ;;
   esac
 else
  start_agent
 fi
exit 0;

