
#!/bin/sh
# File	: proc_mail-aliases.sh
# By	: Maarten.deBoer@Atos.net, 190923 
# Subject	: Script to process mail-aliases (for NetApp at Atos) and place it in (mail)/etc/aliases (option -m) and on Webpage (option -w)
#
PGM=`basename $0|cut -d\. -f1`
VER="0.1"
TMP="/tmp/${PGM}.$$"
LOG="${HOME}/log/${PGM}.log"
RCFILE="${HOME}/etc/${PGM}.rc"
UPDWEBPAGE=""
WEBDIR="/appl/netapp/html/mail-aliases"
PAGE="${WEBDIR}/index.html"
DATADIR="${HOME}/data/mail-aliases"
PROCMAILALIASES=""
MFILTER="@atos.net"
WEBURL="http://nlxnetapp01.bcklan.ao-srv.com/mail-aliases"
PROCMAIL=""

USAGE()
{
  echo "Usage: ${PGM} [<options>]"
  echo "  Version: ${VER}"
  echo "  options              :"
  echo "    -a | --aliases     : process mail-Aliases"
  echo "    -h | --help        : this help"
  echo "    -m | --mail        : process mail to alias members"
  echo "    -w | --webpage     : update Webpage"
  echo "    -V                 : Version"
  echo "    -x                 : set -x"
}
while [ ${#} -ge 1 ]
  do
  case ${1} in
    -h | --help) USAGE; exit 1 ;;
    -a | --aliases) PROCMAILALIASES=1 ;;
    -m | --mail) PROCMAIL=1 ;;
    -w | --webpage) UPDWEBPAGE=1 ;;
    -V) echo "${PGM}: v${VER}"; exit 3 ;;
    -x)  set -x ;;
    *)  echo "Option ${1} not known."; USAGE; exit 1 ;;
  esac
    shift
done  # while

echo "`date` ${PGM} v${VER} started."|tee -a ${LOG}
touch ${TMP} 
if [ -f ${RCFILE} ]; then
  echo "  RCFILE (${RCFILE}) found. Reading it ..."|tee -a ${LOG}
  . ${RCFILE}
fi
PAGE="${WEBDIR}/index.html"

if [ ${PROCMAILALIASES} ]; then
  echo "  Process Mailaliases (in dir. ${DATADIR} ..."|tee -a ${LOG}
  cd ${DATADIR}
  ls -1 atos_*|while read FNAME
  do
    echo -n "${FNAME}:		"|tee -a ${TMP}
# Read addresses, check it on MFILTER and put them on 1 line
    cat ${FNAME}|sort -u|grep -v ^#|grep "${MFILTER}"|while read ADDRESS
    do
      echo -n "${ADDRESS}," >> ${TMP}
    done  # cat ${FNAME}
    echo "" |tee -a ${TMP}

  done  # ls -1
  echo ""
  echo "  /etc/aliases:"
  /usr/bin/sudo /bin/cp /etc/aliases /etc/aliases_old
  cat /etc/aliases | grep -v ^atos > /tmp/aliases
  cat ${TMP}|sort -u >> /tmp/aliases
  /usr/bin/sudo /bin/cp /tmp/aliases /etc/aliases
  /usr/bin/sudo /usr/bin/newaliases
fi  # MAILALIASES

if [ ${UPDWEBPAGE} ]; then
  echo "  Updating Webpage (${PAGE}) ..."|tee -a ${LOG}
  echo "<HTML>" > ${PAGE}
  echo "<HEAD>" >> ${PAGE}
  echo "<META NAME='generator' content='${PGM} v${VER}' /> ">> ${PAGE}
  echo "<META NAME='up-date' content='`date +%Y-%m-%d-%H-%M-%S`' /> ">> ${PAGE}
  echo "<title>mail-aliases</title> ">> ${PAGE}

  echo "<BODY aLink=blue bgColor=#D3D0D3 link=blue text=#000000 vLink=blue>" >> ${PAGE}
  echo "<TABLE BORDER=0 WIDTH='100%'>" >> ${PAGE}
  echo "<TR HEIGHT=40px>" >> ${PAGE}
  echo "  <TD bgColor=#0066A1>" >> ${PAGE}
  echo "    <FONT COLOR=white face='verdana' size='5'> <B>Atos mail-aliases for NetApp management<BR> </FONT>" >> ${PAGE}
  echo "  <TD>" >> ${PAGE}
  echo "</TR>" >> ${PAGE}
  echo "</TABLE>" >> ${PAGE}

  echo "  <TABLE BORDER=1 WIDTH='100%'>" >> ${PAGE}
  echo "    <TR>" >> ${PAGE}
  echo "      <TD>" >> ${PAGE}
  echo "        <FONT COLOR=black face='verdana' size='5'>" >> ${PAGE}
  echo "<B>Country" >> ${PAGE}
  echo "      </TD>" >> ${PAGE}
  echo "      <TD>" >> ${PAGE}
  echo "        <FONT COLOR=black face='verdana' size='5'>" >> ${PAGE}
  echo "<B>Mail-addresses" >> ${PAGE}
  echo "      </TD>" >> ${PAGE}
  echo "    </TR>" >> ${PAGE}
  cd ${DATADIR}
  ls -1 atos_*|while read FNAME
  do
    echo "    <TR>" >> ${PAGE}
    echo "      <TD>" >> ${PAGE}
# CountryNAME
    CNAME=`echo ${FNAME}|cut -d\_ -f2|tr [:lower:] [:upper:]`
    echo "        <FONT COLOR=black face='verdana' size='4'>" >> ${PAGE}
    echo "        <A NAME=\"${CNAME}\" > " >> ${PAGE}
    echo "${CNAME}:" >> ${PAGE}
    echo "        </FONT>" >> ${PAGE}
    echo "      </TD>" >> ${PAGE}
    echo "      <TD>" >> ${PAGE}
    echo "        <FONT COLOR=black face='verdana' size='3'>" >> ${PAGE}
    cat ${FNAME}|sort -u|grep -v ^#|grep "${MFILTER}"|while read ADDRESS
    do
      echo "${ADDRESS} "
      echo "${ADDRESS} <BR> " >> ${PAGE}
    done  # cat ${FNAME}
    echo "      </TD>" >> ${PAGE}

    echo "    </TR>" >> ${PAGE}
  done  # ls -1 *
  echo "  </TABLE>" >> ${PAGE}

  echo "<TABLE BORDER=0 WIDTH='100%'> " >> ${PAGE}
  echo "<TR>  " >> ${PAGE}
  echo "  <TD bgColor=#0066A1 >  " >> ${PAGE}
  echo "    <FONT COLOR=white face='Verdana' size='1'> " >> ${PAGE}
  echo "      <CENTER> " >> ${PAGE}
  echo "Contact: |Content Owner: <A HREF=mailto:Maarten.deBoer@Atos.net>Maarten de Boer</A> |Last Update: `date` [${PGM} v${VER} ]<BR> For internal use (c) Atos S.A. `date +%Y` all rights reserved. Reproduction in whole or in part is prohibited without the written consent of Atos SA. " >> ${PAGE}
  echo "      </CENTER> " >> ${PAGE}
  echo "    </FONT> " >> ${PAGE}
  echo "  </TD> " >> ${PAGE}
  echo "</TR> " >> ${PAGE}
  echo "</TABLE> " >> ${PAGE}


  echo "</BODY>" >> ${PAGE}

  echo "</HTML>" >> ${PAGE}
fi  # UPDWEBPAGE

if [ ${PROCMAIL} ]; then
  echo "  Processing mail to alias members ..."|tee -a ${LOG}
  cd ${DATADIR}
  ls -1 atos_*|while read FNAME
  do
    cat ${FNAME}|grep maarten|sort -u|grep -v ^#|grep "${MFILTER}"|while read ADDRESS
    do
      CNAME=`echo ${FNAME}|cut -d\_ -f2|tr [:lower:] [:upper:]`
      WEBCNAME="${WEBURL}/#${CNAME}"
      echo "  ${ADDRESS} in ${CNAME} URL=${WEBCNAME}"
     
      echo "From: Maarten de Boer <maarten.deboer@atos.net>" > ${TMP}.mail
      echo "To: ${ADDRESS}" >> ${TMP}.mail
      echo "Reply-to: maarten.deboer@atos.net" >> ${TMP}.mail
      echo "Subject: NetApp-CC mail" >> ${TMP}.mail
      echo "Content-Type: text/html" >> ${TMP}.mail

cat << [EOT] >> ${TMP}.mail
<HTML>

Hello colleague, <BR>
<BR>
You're receiving this mail on regular base, because you are listed as accountable manager or responsible engineer for NetApp-systems in <country>. <BR>
<BR>
When you are not involved anymore in managing NetApp-systems, please reply with the name(s) to whom it has been handed over. <BR>
<BR>
You can see <A HREF="${WEBCNAME}">here</A> who are also on the list for ${CNAME}. <BR>

On regular bases you will receive a link (URL) to the NetApp-SAM (Service Account Manager) reports. <BR>
You can find a lot of information in those reports, like; <BR>
<UL>
<LI> [Risks] at a system: <BR>
We ask you to look into those risks and mitigate the (high) ones (first). <BR>
So, the change of system outages will be lowered. <BR>
 <BR>
<LI> [ASUP] status of a system: <BR>
Be aware not every NetApp-system is sending Autosupports (ASUPs) to NetApp. <BR>
When NetApp is not receiving ASUPs, then can not react proactively on the occurring failures. <BR>
So, please check (in the SAM-report) if your NetApp-systems are sending ASUPs to NetApp. <BR>
If it is not possible to send the ASUP (due to security), please send me the SerialNumber of the systems, so we can mark them as "Declined with reason". <BR>
<BR>

</HTML>
[EOT]
      if [ -s ${TMP}.mail ]; then
        cat ${TMP}.mail | /usr/sbin/sendmail -t
        echo "  Mailed to ${ADDRESS} "|tee -a ${LOG}
        rm ${TMP}.mail
      fi

    done  # cat ${FNAME}
  done  # ls -1 atos_*
fi  # PROCMAIL



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

