
#!/bin/sh
# File	: techlab-portal-request-proc
# By	: Maarten.deBoer@Atos.net, 241011
# Subject	: Script to process TechLab-Portal requests
#(0.2),241011	: Added ERR in mail
#(0.3),241011	: Added more checks
PGM=`basename $0|cut -d\. -f1`
VER="0.3"
TMP="/tmp/${PGM}.$$"
LOG="${HOME}/log/${PGM}.log"

INDIR="${HOME}/data/in"
DONEDIR="${HOME}/data/done"

echo "`date` ${PGM} v${VER} started."

cd ${INDIR}
ls -1 *.rc 2> /dev/null |while read FNAME
do
  touch ${TMP}
  echo ${FNAME}
  . ${INDIR}/${FNAME}
  if [ "${EMAIL}" != "" ]; then
    if [ "${PHONENUMBER}" == "" ]; then
      echo "Your Phone number is EMPTY." >> ${TMP} 
      echo "" >> ${TMP} 
      echo "  PHONENUMBER (in ${FNAME}) is EMPTY."|tee -a ${LOG}
    fi  # PHONENUMBER
    if [ "${SUBJECT_ACTION}" == "" ]; then
      echo "Your Subject - Actions is EMPTY (Keuze)." >> ${TMP} 
      echo "" >> ${TMP} 
      echo "  SUBJECT_ACTION (in ${FNAME}) is EMPTY."|tee -a ${LOG}
    fi  # SUBJECT_ACTION
    if [ "${OS_TYPE}" == "" ]; then
      echo "Your OS - Type is EMPTY (Keuze)." >> ${TMP} 
      echo "" >> ${TMP} 
      echo "  OS_TYPE (in ${FNAME}) is EMPTY."|tee -a ${LOG}
    fi  # OS_TYPE

    if [ -s ${TMP} ]; then
      echo "" >> ${TMP}
      echo "Please provide missing information in the Tech.Lab-PORTAL via a NEW request. Do NOT reply on this mail. (Ref=${DATI})" >> ${TMP}
      cat ${TMP} | mailx -r noreply@techlab.atos.net -s "Tech.Lab portal request MISSING info" "${EMAIL}"
      echo "  Mailed to ${EMAIL}"|tee -a ${LOG}
      rm ${TMP}
    else
# No "errors", proceed
      FIRST_NAME=`echo ${EMAIL}|cut -d\@ -f1`
      echo "Dear ${FIRST_NAME}, " >> ${TMP}
      echo "You have chosen " >> ${TMP}
      case ${SUBJECT_ACTION} in
        vm_new)
	  echo "for a new VM with the OS " >> ${TMP}

          case ${OS_TYPE} in
            linux_centos6) echo "Linux - CentOS 6 (64-bit)" >> ${TMP} ;;
	    linux_centos7) echo "Linux - CentOS 7 (64-bit)" >> ${TMP} ;;
	    linux_centos8) echo "Linux - CentOS 8 (64-bit)" >> ${TMP} ;;
	    linux_oel6)  echo "Oracle Enterprise Linux 6 (64-bit)" >> ${TMP} ;;
	    linux_oel7) echo "Oracle Enterprise Linux 7 (64-bit)" >> ${TMP} ;;
	    linux_oel8) echo "Oracle Enterprise Linux 8 (64-bit)" >> ${TMP} ;;
	    linux_oel9) echo "Oracle Enterprise Linux 9 (64-bit)" >> ${TMP} ;;
	    linux_rhel6) echo "Linux - RHEL 6 (64-bit)" >> ${TMP} ;;
	    linux_rhel7) echo "Linux - RHEL 7 (64-bit)" >> ${TMP} ;;
	    linux_rhel8) echo "Linux - RHEL 8 (64-bit)" >> ${TMP} ;;
	    linux_rhel9) echo "Linux - RHEL 9 (64-bit)" >> ${TMP} ;;
	    windows_10) echo "Windows - MS Windows 10 (64-bit)" >> ${TMP} ;;
	    windows_11) echo "Windows - MS Windows 11 (64-bit)" >> ${TMP} ;;
	    windows_server-2019) echo "Windows - MS Windows Server 2019 (64-bit)" >> ${TMP} ;;
	    windows_server-2022) echo "Windows - MS Windows Server 2022 (64-bit)" >> ${TMP} ;;

            *)
	      echo "an unknown OS and TYPE (${OS_TYPE}) " >> ${TMP}
	      ;;
	  esac
	  ;;
	vm_remove)	
	  echo "to remove a VM " >> ${TMP}
	  ;;
	*)
	  echo "an unknown SUBJECT and ACTION (${SUBJECT_ACTION}) " >> ${TMP}
	  ;;
      esac

      if [ -s ${TMP} ]; then
	echo "" >> ${TMP}
	echo "There will be NO further action. Because that has NOT been developed yet." >> ${TMP}
	echo "" >> ${TMP}
	echo "Kind regards, Technology-team (technologylab@atos.net)." >> ${TMP}

	cat ${TMP} | mailx -r noreply@techlab.atos.net -s "Tech.Lab portal request info (Ref=${DATI})" "${EMAIL}"
        echo "  Mailed to ${EMAIL}"|tee -a ${LOG}

        rm ${TMP}
      fi  # -s TMP

    fi  # -s TMP

  else
    echo "  EMAIL (in ${FNAME}) is empty." | tee -a ${LOG}
  fi  # EMAIL <> ""

  mv ${FNAME} ${DONEDIR}
done  # FNAME


echo "`date` ${PGM} v${VER} finished."
exit 0

