
#!/bin/sh
# File	: proc_atos_html.sh
# By	: MaartenDeBoer.nl, 191002
# Subject	: Script to process mail at account atos
#(0.2)	: Add Subject check
#(0.3)	: Added SUBJECT after : , rename to proc_atos_web.sh
#(0.4),210420	: SOme mod's
PGM="`basename $0|cut -d\. -f1`"
VER="0.4"
TMP="/tmp/${PGM}.$$"
LOG="${HOME}/log/${PGM}.log"
ETC="${HOME}/etc/${PGM}"
MAILTO="maarten@agrarix.nl"
MAILFROM=""
SUBJECT=""
WEBDIR="/var/www/vhosts/atos.agrarix.it/html"
MAILDIR="${HOME}/mail"

USAGE()
{
  echo "Usage: ${PGM} [options]"
  echo "Options            : "
  echo "     -f|--from     : From address (${MAILFROM})"
  echo "     -h|--help     : This help"
  echo "     --mailto      : change MAILTO-address (${MAILTO})"
  echo "     -s|--subject  : Subject (${SUBJECT})"
  echo "     -V            : Version"
}
while [ ${#} -gt 0 ]
do
  case "${1}" in
    '-h'|'--help') USAGE; exit 1 ;;
    '-f'|'--from') MAILFROM="${2}"; shift ;;
    '--mailto') MAILTO="${2}"; shift ;;
    '-s'|'--subject') SUBJECT="${2}"; shift ;;
    '-x')  set -x ;;
    '-V')  echo "Version=${VER}" ; exit 1 ;;
    *) echo "Option ${1} not found." ; USAGE; exit 1 ;;
  esac
  shift
done

echo "`date` ${PGM} v${VER} ($$) started."|tee -a ${LOG}
if [ ! -d ${MAILDIR} ]; then
  echo "  MAILDIR (${MAILDIR}) not found. Exiting ..."|tee -a ${LOG}
  exit 2
fi
if [ ! -d ${WEBDIR} ]; then
  echo "  WEBDIR (${WEBDIR}) not found. Exiting ..."|tee -a ${LOG}
  exit 3
fi
if [ "${MAILFROM}" = "" ]; then
  echo "  No MAILFROM address. Exiting ..."|tee -a ${LOG}
  exit 4
fi

## Check for YYYYMMDDHHMMSS (and NOT longer). Therefor Space is used.
#SUBJECT2=`echo "${SUBJECT} "|cut -d\: -f2|grep "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] "|sed 's/ //g'`
#if [ "${SUBJECT2}" = "" ]; then
#  echo "  No (or wrong) SUBJECT2 (${SUBJECT2}). Exiting ..."|tee -a ${LOG}
##  echo "  No (or wrong) SUBJECT (${SUBJECT})."|mailx -s "NO / WRONG subject (${SUBJECT}) to ATOS [${PGM} v${VER}]" ${MAILFROM}
#  exit 5
#fi
# MAIN
# Creating NEW dir (SUBJECT2) and move mailfile (SUBJECT) to this dir.
#mkdir ${WEBDIR}/${SUBJECT2}

echo "<H3> ${SUBJECT} </H3>" >> ${WEBDIR}/${SUBJECT}.html
# ls ${MAILDIR}/${SUBJECT}|tee -a ${LOG}
if [ -f "${MAILDIR}/${SUBJECT}" ]; then
  mv ${MAILDIR}/${SUBJECT} ${WEBDIR}/${SUBJECT}.html
  chmod 644 ${WEBDIR}/${SUBJECT}.html
  echo "  File ${SUBJECT} moved to ${WEBDIR}/${SUBJECT}.html"|tee -a ${LOG}
fi

echo "`date` ${PGM} v${VER} ($$) finished."|tee -a ${LOG}
exit 0

