#!/bin/bash
######################################################################
#
#  Name: boomDNLD
#
#  Description: Finds out Agent ID of system in boom and executes via
#               boom server CLI "-c BOOM_AGENT_GETFILE" (download file).
#
#
#  Author: Solmsdorf
#  Version: 1.01
#  Date: 15.7.2021
#
#  Input parameter: <system> <src_file> <dst_file>
#
#  Return: 0 = Success
#          1 = Warning (System >>${node}<< not available in boom)
#          2 = Error ( >>${ACTION} -cc "${cmd}" -a "${id}" -a 20<< failed! )
#         >3 = Error (>>GET_AGENTS_STATUS<< failed (exit_val=${ret})!)
#
#  Note: requires configuration settings (SRVCLI, SVRCLI_CFG) inside boom_env_ssl.cnf
#  Note: requires configured boom server CLI usage (/opt/boom/server/srv/cli/boom_cli.cfg)
#
#
#  History:
#
#     date    |name| reason
#  ----------------------------------------------------------------
#  15/07/2021 | ps | (1.00) Initial Release
#  20/12/2023 | ps | (1.01) small layout adaptions
#
######################################################################

NAME=$(basename $0)
PID=$$

CNFFIL="boom_env_ssl.cnf"

fil=${0}
fil_short=$(basename ${fil})
if [ `echo ${fil} | egrep "^\/" | wc -l` -eq 0 ]
then
# path not starting with "/"
  if [ `echo ${fil} | egrep "^\.\." | wc -l` -ge 1 ] || [ `echo ${fil} | egrep "\/" | wc -l` -ge 1 ]
  then
# path starting with ".."
    pd=$(realpath ${fil})
    pd=$(dirname ${pd})
    fil="${pd}/${fil_short}"
  else
# path not starting with ".."
    pd=$(pwd)
    fil="${pd}/${fil_short}"
  fi
fi
WORKDIR=$(dirname ${fil})

source "${WORKDIR}/${CNFFIL}"

USAGE="
Usage: ${NAME} <system_name> <source_path-and_file_name> <destination_path-and_file_name>
        Absolute path name for source and destination always required!
	File at destination must not exist before!
Note: requires boom server CLI configuration (${SRVCLI}/${SVRCLI_CFG})
"

user=$(id |awk -F"=" '{print $2}' |awk -F"(" '{print $2}' | awk -F")" '{print $1}')

# boom CLI
BOOMCLI="java -jar ${SRVCLI}/boom_cli.jar -f ${SRVCLI}/${SVRCLI_CFG}"
GETSTAT="${BOOMCLI} -c GET_AGENTS_STATUS"
GETCARDS="${BOOMCLI} -c GETAGENTCARDS"
ACTION="${BOOMCLI} -c ACTION"
GETFILE="${BOOMCLI} -c BOOM_AGENT_GETFILE"
PUTFILE="${BOOMCLI} -c BOOM_AGENT_PUTFILE"

OUT_HEAD_FOOT="Action finished.*|Result =*|==="

OnDie()
{
  ShowT WARNING "(${PID}) [${user}] ${NAME} ${node} aborted!" ${LOGFILE}
  exit 9
}


function Ausgang
{

  local ret=${1}

##  ShowT INFO "(${PID}) [${user}] ${NAME} finished  " ${LOGFILE}
  exit $ret
}

function ShowT()
{
  local timstmp=$(date "+%d.%m.%Y %T")

   echo -e "${timstmp} ${1}: ${2}"
}

# INIT+++++++++++++++++++++++++++++++++++++++

#  Arguments?
if [ $# -lt 3 ]
then
  printf "${USAGE} \n"
  exit 1
fi
args=("$@")
system=${1}
node=${system}
node_short=$(echo ${node} | cut -f1-1 -d .)

src=${2}
dest=${3}


# MAIN++++++++++++++++++++++++++++++++++++++++++

getstat_all=$(${GETSTAT}) ; ret=$?
if [ ${ret} -ne 0 ]
then
# ALARM
  ShowT ERROR "(${PID}) [${user}] ${node_short} - >GET_AGENTS_STATUS<< failed (exit_val=${ret})!" 
  exit ${ret}
fi
# Get ID
IFS=$'\n'; id=$(echo "${getstat_all}" |grep -w "${node}" |awk '{print $1}'); ret=$?;  unset IFS
if [ -z "${id}" ]
then
  ShowT WARNING "(${PID}) [${user}] ${node_short} - System >>${node}<< not available in boom " 
  Ausgang 1
fi

## source/destination: case "." ==> n/a

# Get File
out=$(${GETFILE} -cc ${id} -a "${src}" -a "${dest}")
echo ${out} |grep "Result = Success" >/dev/null
if [ $? -eq 0 ]
then
  ShowT INFO "(${PID}) [${user}] ${node_short} - get >>${src}<< to >>${dest}<< done"
  Ausgang 0
else
  ShowT ERROR "(${PID}) [${user}] ${node_short} - >>${GETFILE} -cc ${id} -a ${src} -a ${dest}<< failed! 
${out}" 
  Ausgang 2
fi

