
#!/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)
#(0.2),190925	: Added -f|--filter (MFILTER)
#(0.3),190925	: Added -d|--datafile (alias-datafile)
#(0.4),190925	: Added: Check WEBDIR
#
PGM=`basename $0|cut -d\. -f1`
VER="0.4"
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=""
AFILTER="@atos.net"
WEBURL="http://nlxnetapp02.bcklan.ao-srv.com/mail-aliases"
PROCMAIL=""
MFILTER="[?]*"
FROM="Maarten de Boer <maarten.deboer@atos.net>"
REPLYTO="Maarten de Boer <maarten.deboer@atos.net>"
ADDDATAFILE=""
MAILTO="maarten.deboer@atos.net"
#
SAMREPORTS="https://sp2013.myatos.net/ms/gd/cloud/cs_rsd/IaaS%20Storage%20and%20Backup/Forms/AllItems.aspx?RootFolder=%2Fms%2Fgd%2Fcloud%2Fcs%5Frsd%2FIaaS%20Storage%20and%20Backup%2FNetApp%20Competence%20Center%2FGlobal%20SAM%20presentations%20and%20reports"

USAGE()
{
  echo "Usage: ${PGM} [<options>]"
  echo "  Version: ${VER}"
  echo "  options              :"
  echo "    -a | --aliases     : process mailaddresses (in ${DATADIR}) to /etc/aliases"
  echo "    -d | --datafile    : add aliases-datafile (from ${DATAFILE}) to the mail"
  echo "    -h | --help        : this help"
  echo "    -m | --mail        : Mail to members (from ${DATADIR})"
  echo "    -w | --webpage     : update Webpage and mail to ${MAILTO}"
  echo "    -V                 : Version"
  echo "    -x                 : set -x"
}
while [ ${#} -ge 1 ]
  do
  case ${1} in
    -h | --help) USAGE; exit 1 ;;
    -d | --datafile) ADDDATAFILE=1 ;;
    -a | --aliases) PROCMAILALIASES=1 ;;
    -f | --filter) MFILTER="${2}"; shift ;;
    -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"

echo "ADDDATAFILE=${ADDDATAFILE}"
echo "AFILTER=${AFILTER}"
echo "DATADIR=${DATADIR}"
echo "FROM=${FROM}"
echo "MFILTER=${MFILTER}"
echo "PAGE=${PAGE}"
echo "PGM=${PGM}"
echo "PROCMAIL=${PROCMAIL}"
echo "PROCMAILALIASES=${PROCMAILALIASES}"
echo "REPLYTO=${REPLYTO}"
echo "UPDWEBPAGE=${UPDWEBPAGE}"
echo "VER=${VER}"
echo "WEBDIR=${WEBDIR}"
echo "WEBURL=${WEBURL}"
sleep 1

if [ ! -d ${WEBDIR} ]; then
  echo "  Web-Dir. (${WEBDIR}) NOT found. Exiting "|tee -a ${LOG}
  exit 3
fi


if [ ${PROCMAILALIASES} ]; then
  echo "  Process mail-addresses (in dir. ${DATADIR}) to /etc/aliases ..."|tee -a ${LOG}
  cd ${DATADIR}
  ls -1 atos_*|while read FNAME
  do
    echo -n "${FNAME}:		"|tee -a ${TMP}
# Read addresses, check it on (@atos) AFILTER and not " " (space) and put them on 1 line
    cat ${FNAME}|sort -u|grep -v ^#|grep "${AFILTER}"|grep -v " "|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}) and mailing to ${MAILTO} ..."|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-addresses involved in managing NetApp-systems in countries<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 "${AFILTER}"|grep -v " "|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}
  echo "  Webpage ${WEBURL} has been updated"|tee -a ${LOG}|mailx -s "Mail-aliases page updated [${PGM} v${VER}]" ${MAILTO}
fi  # UPDWEBPAGE

if [ ${PROCMAIL} ]; then
  echo "  Sending mail to alias members (from ${DATADIR}) ..."|tee -a ${LOG}
  cd ${DATADIR}
  ls -1 atos_*|while read FNAME
  do
# Check also for no " " (space)
# And filer on AFILTER (@atos.net) & MFILTER (<some_name>)
    cat ${FNAME}|sort -u|grep -v ^#|grep "${AFILTER}"|grep "${MFILTER}"|grep -v " "|while read ADDRESS
    do
      CNAME=`echo ${FNAME}|cut -d\_ -f2|tr [:lower:] [:upper:]`
      WEBCNAME="${WEBURL}/#${CNAME}"
      echo "  ${ADDRESS} in ${CNAME} URL=${WEBCNAME}"
     
      echo "From: ${FROM}" > ${TMP}.mail
      echo "To: ${ADDRESS}" >> ${TMP}.mail
      echo "Reply-to: ${REPLYTO}" >> ${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 your mail-address (${ADDRESS}) is listed as accountable manager or responsible engineer for NetApp-systems in ${CNAME}. <BR>
<BR>
When you are not involved anymore in managing NetApp-systems, <B>please reply with the name(s) to whom it has been handed over. </B> <BR>
You can see <A HREF="${WEBCNAME}">here</A> who are also on the list for ${CNAME}. <B>Please reply if you're missing some names.</B> <BR>
<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 <A HREF="${SAMREPORTS}">those</A> reports, like; <BR>
<UL>
<LI> [Risks] at a system: <BR>
<B>We ask you to look into those risks and mitigate the (high) ones (first).</B> <BR>
So, the chance of system failures is reduced. <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 on the (hardware) failures and point out the risks of the systems. <BR>
<B>So, please check (in the SAM-report) if your NetApp-systems are sending ASUPs to NetApp.</B> <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>
<BR>
Kind regard, <BR>
<B>Maarten de Boer</B> <BR>
Storage (NL-)landscape & lead architect at NetApp Competence Center <BR>
M: +31-6-2242 2326 <BR>
The Netherlands <BR>
<A HREF="https://sp2013.myatos.net/ms/gd/cloud/cs_rsd/IaaS%20Storage%20and%20Backup/Forms/AllItems.aspx?RootFolder=%2Fms%2Fgd%2Fcloud%2Fcs%5Frsd%2FIaaS%20Storage%20and%20Backup%2FNetApp%20Competence%20Center">NetApp-CC</A> <BR>
<BR>
</HTML>
[EOT]
      if [ -s ${TMP}.mail ]; then
# Works only when uuencode is "installed"
        if [ ${ADDDATAFILE} ]; then
          if [ -f /usr/bin/uuencode ]; then
            cat ${FNAME}|grep -v ^#|uuencode > ${TMP}.mail
          else
            echo "  /usr/bin/uuencode NOT found. Attachment can NOT be added."|tee -a ${LOG}
          fi
        fi
        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

