
#!/bin/sh
# File	: proc_atos-netapp-filer-list.sh
# By	: Maarten.deBoer@atos.net, 140627, 150610
# Subject	: Script to process a list of Atos & NetApp filers
#set -x
#(0.2)	: Added countries
PGM="`basename $0|cut -d\. -f1`" 
VER="0.2"
TMP="/tmp/${PGM}.$$"
HOSTNAME="`hostname|cut -d\. -f1`"

MAILTO="maarten.deboer@atos.net"

BASEDIR="`pwd | sed 's/scripts//'|sed 's/bin//'`"
NETAPPHOME="/appl/mdr/mdrglob/afsp/netapp"
LOG="${BASEDIR}/log/${PGM}.log"

# Input
NALIST1="${NETAPPHOME}/data/netapp-filer-list_2014-03-25.csv"
NALIST2="${NETAPPHOME}/data/netapp-filer-list_2015-03-09.csv"

# Output
ATOSLIST="${BASEDIR}/data/atos-filer-list.csv"
ATOSNALIST="${BASEDIR}/data/atos-netapp-filer-list.csv"

# "model-version/filer-name-loc-model-os-fw-sn-partner.csv"

echo "`date` ${PGM} v${VER} started."|tee -a ${LOG}
touch ${TMP}

if [ ! -f ${NALIST1} ]; then
  echo "NO ${NALIST1} found. So exiting ..."|tee -a ${LOG}
  exit 2
fi
if [ ! -f ${NALIST2} ]; then
  echo "NO ${NALIST2} found. So exiting ..."|tee -a ${LOG}
  exit 2
fi

echo "  Getting all Atos filers from all countries in 1 list (${ATOSFILE}) ..."|tee -a ${LOG}
cp /dev/null ${ATOSLIST}
for COUNTRY in `cd /appl/mdr/mdrglob/afsp/; ls -1d ??`
do
  cd /appl/mdr/mdrglob/afsp/${COUNTRY}/data/model-version/
  cat *.csv|grep -v ^#|awk -F\; '{print $1,$6}'|sort -u|while read CONTROLER SERNR REST
  do
    echo "${CONTROLER};${SERNR};${COUNTRY};" >> ${ATOSLIST}
  done  # read LINE
done  # COUNTRY


echo "  Getting all ser.nrs in 1 list ..."|tee -a ${LOG}
cp /dev/null ${TMP}
echo "`cat ${ATOSLIST}|awk -F\; '{print $2}'`" >>  ${TMP}
echo "`cat ${NALIST1}|awk -F\; '{print $3}'`" >>  ${TMP}
echo "`cat ${NALIST2}|awk -F\; '{print $5}'`" >>  ${TMP}

echo "  Finding filers per country ..."|tee -a ${LOG}
for COUNTRY in `cd /appl/mdr/mdrglob/afsp/; ls -1d ??`
do
  echo "${COUNTRY} ..."
# Check if model-version files are not empty
  ANSW="`cat /appl/mdr/mdrglob/afsp/${COUNTRY}/data/model-version/*.csv| grep -v ^#`"
  if [ "${ANSW}" != "" ]; then

    ATOSNALIST="${BASEDIR}/data/${COUNTRY}-atos-netapp-filer-list.csv"
    echo "# Atos country; Atos name (Ser.Nr); NetApp Country; Name (Ser.Nr) from ${NALIST2};Country;Name (Ser.Nr) from ${NALIST1};" > ${ATOSNALIST}

    cat ${TMP}|sort -u|while read SERNR REST
    do
      # double grep needed to select correct SerNr (deslect partner ser.nr)
      ATOSSNFILER="`grep ${SERNR} ${ATOSLIST}|awk -F\; '{print $1"("$2")"}'|grep ${SERNR}`"
      ATOSSNCOUNTRY="`grep ${SERNR} ${ATOSLIST}|awk -F\; '{print $3}'|sort -u`"
      NA1SNFILER="`grep ${SERNR} ${NALIST1}|awk -F\; '{print $4"("$3")"}'|grep ${SERNR}`"
      NA1SNCOUNTRY="`grep ${SERNR} ${NALIST1}|awk -F\; '{print $12}'|sort -u`"
      NA2SNFILER="`grep ${SERNR} ${NALIST2}|awk -F\; '{print $8"("$5")"}'|grep ${SERNR}`"
      NA2SNCOUNTRY="`grep ${SERNR} ${NALIST2}|awk -F\; '{print $12}'|sort -u`"

      if [ "${COUNTRY}" = "${ATOSSNCOUNTRY}" ]; then
        echo "${ATOSSNCOUNTRY};${ATOSSNFILER};${NA2SNCOUNTRY};${NA2SNFILER};${NA1SNCOUNTRY};${NA1SNFILER};"|tee -a ${ATOSNALIST} 
      fi  

    done  # cat
#    date | mailx -a ${ATOSNALIST} -s "${HOSTNAME}: ${COUNTRY} Atos-NetApp-filer-list [${PGM} v${VER}]" ${MAILTO}
  fi  # EC

done  # COUNTRY


#rm ${TMP}
echo "`date` ${PGM} Finished."|tee -a ${LOG}
exit 0

