
#!/bin/sh
# File	: get_cmz-names.sh
# By	: Maarten.deBoer@atos.net, 240230
# Subject	: Script to get CMZ name (info) from NADT
PGM="`basename $0|cut -d\. -f1`"
VER="0.1"
TMP="/tmp/${PGM}.$$"
CSV="/tmp/${PGM}.csv"
LOG="${HOME}/log/${PGM}.log"
SSH="/usr/bin/ssh -n"
HOSTNAME="`hostname|cut -d\. -f1`"
MAILTO="maarten.deboer@atos.net"
MAIL=""
ALLCUSTCODES="${HOME}/etc/all-cust-codes.asc"

#curl --location 
NADT_API="https://nadt3.atos-srv.net/api/customer"
#?cmz=dsm' --header 'Authorization: Token xxxx' --insecure
TOKEN="e51b12139bddb8278392df2bc32ee90a6ddb808c"


USAGE()
{
  echo "Usage: ${PGM} [options]"
  echo "  Version: ${VER}"
  echo "  options          :"
  echo "    -a             : etc/All-cust-codes file (${ALLCUSTCODES})"
  echo "    -e             : Etc/clusters file (${CLUSTERS})"
  echo "    -h | --help    : this help"
  echo "    -m | --mail    : do send Mail"
  echo "    --mailto       : change MAILTO address & do send mail (${MAILTO})"
  echo "    -V             : Version"
  echo "    -x             : set -x"
}

# Check options
while [ ${#} -gt 0 ]
  do
  case ${1} in
    -a) ALLCUSTCODES=${2}; shift ;;
    -e) CLUSTERS=${2}; shift ;;
    -m | --mail) MAIL=1 ;;
    -h | --help) USAGE; exit 1 ;;
    --mailto) MAILTO=${2}; MAIL=1; shift ;;
    -V) echo "${PGM}: v${VER}"; exit 3 ;;
    -x)  set -x ;;
    *)  echo "Option ${1} not known."; USAGE; exit 1 ;;
  esac
    shift
done

# MAIN
echo "`date` ${PGM} v${VER} started."|tee -a ${LOG} 
touch ${TMP}
echo "ALLCUSTCODES=${ALLCUSTCODES}"
echo "MAIL=${MAIL}"
echo "MAILTO=${MAILTO}"
sleep 1

echo "FSOD;CMZ;NAME;NOTES;SDM_MAIL;CMGR_MAIL;CLA_MAIL;CLO_MAIL;CHMGR_MAIL;CHARGE_TO_ID;" > ${CSV}

cat ${ALLCUSTCODES}|grep -v ^#|while read LINE
do
  echo "${LINE}:"
  curl -# --header "Authorization: Token ${TOKEN}" --insecure --location "${NADT_API}?cmz=${LINE}#?format=api" | jq "." > ${TMP}

  CMZ=`grep "cmz" ${TMP}|cut -d\: -f2-|sed 's/[\",]//g'`
  NAME=`grep "name" ${TMP}|cut -d\: -f2-|sed 's/[\",]//g'`
  NOTES=`grep "notes" ${TMP}|cut -d\: -f2-|sed 's/[\",]//g'`
  CHARGE_TO_ID=`grep "charge_to_id" ${TMP}|cut -d\: -f2-|sed 's/[\",]//g'`
  SDM_MAIL=`grep "service_delivery_manager_email" ${TMP}|cut -d\: -f2-|sed 's/[\",]//g'`
  CMGR_MAIL=`grep "contract_manager_email" ${TMP}|cut -d\: -f2-|sed 's/[\",]//g'`
  CLA_MAIL=`grep "customer_landscape_architect_email" ${TMP}|cut -d\: -f2-|sed 's/[\",]//g'`
  CLO_MAIL=`grep "customer_landscape_owner_email" ${TMP}|cut -d\: -f2-|sed 's/[\",]//g'`
  CHMGR_MAIL=`grep "change_manager_email" ${TMP}|cut -d\: -f2-|sed 's/[\",]//g'`

  echo "${LINE};${CMZ};${NAME};${NOTES};${SDM_MAIL};${CMGR_MAIL};${CLA_MAIL};${CLO_MAIL};${CHMGR_MAIL};${CHARGE_TO_ID};" |tee -a ${CSV}

#  echo "------------------"
#  curl  --header "Authorization: Token ${TOKEN}" --insecure --location "${NADT_API}?cmz=${LINE}#?format=json"
#  echo "===================="
#NADT_API="https://nadt3.atos-srv.net/api/customer"
#?cmz=dsm' --header "Authorization: Token ${TOKEN}" --insecure


done  # LINE


if [ ${MAIL} ]; then
  date | mailx -a ${CSV} -s ":${HOSTNAME}: All-Cust-Codes [${PGM} v${VER}]" ${MAILTO}
  echo "  Mailed All-Cust-Codes.csv to ${MAILTO}"|tee -a ${LOG}
fi  #  MAIL

# Finish
rm ${TMP} 
echo "`date` ${PGM} v${VER} finished."|tee -a ${LOG}
exit 0

