
#!/bin/sh
# File	: copy_asup-data_to_countries.sh
# By	: Maarten.deBoer@Atos.net, 180523
# Subject	: Script to top ASUP-data (asup/data/database) to countries based on TZ from ASUP_GENERATED_ON
#(0.2),180523	: Added cp with check & mailx
PGM=`basename $0|cut -d\. -f1`
VER="0.2"
TMP="/tmp/${PGM}.$$"
MAILTO="maarten.deboer@atos.net"

BASEDIR="`pwd | sed 's/scripts//'|sed 's/bin//'`"
LOG="${BASEDIR}/log/${PGM}.log"
DATABASEDIR="${BASEDIR}/data/database"
COUNTRY="`echo ${BASEDIR}| cut -d\/ -f6`"

if [ "${COUNTRY}" != "asup" ]; then
  echo "  Wrong country (${COUNTRY}). Should be asup. Exiting ..."|tee -a ${LOG}
  exit 3
fi

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


cd ${DATABASEDIR}
ls -1 *.asc | while read FILE REST
do
  ASUP_GENERATED_ON=`grep ASUP_GENERATED_ON ${FILE}|cut -d\= -f2`
  ASUP_TZ=`echo ${ASUP_GENERATED_ON}|awk '{print $5}'`
  ASUP_SNMP_LOCATION=`grep SNMP_LOCATION ${FILE}|cut -d\= -f2`
  ASUP_CONTROLER_NAME=`grep CONTROLER_NAME ${FILE}|cut -d\= -f2`
  ASUP_SERIAL_NUMBER=`grep SERIAL_NUMBER ${FILE}|cut -d\= -f2`
#  echo "${FILE} ..."

  # https://en.wikipedia.org/wiki/List_of_time_zone_abbreviations
  case ${ASUP_TZ} in
    "UTC") DEST_COUNTRY="??" ;;  # Is UTC correct?
    "ART") DEST_COUNTRY="ar" ;;  # 	Argentina Time	UTC−03
    "BRT") DEST_COUNTRY="br" ;;  # Brasilia Time	UTC−03
    "BST") DEST_COUNTRY="uk" ;;  # British Summer Time (British Standard Time from Feb 1968 to Oct 1971) UTC+01
    "GMT") DEST_COUNTRY="uk" ;;  # Greenwich Mean Time	UTC±00
    "MEST") DEST_COUNTRY="de" ;;  # Middle European Summer Time Same zone as CEST	UTC+02
    "COT") DEST_COUNTRY="co" ;;  # Colombia Time	UTC−05
    "EET") DEST_COUNTRY="fi" ;;  # Eastern European Time	UTC+02
    "EEST") DEST_COUNTRY="fi" ;;  # Eastern European Summer Time	UTC+03

    "AKDT") DEST_COUNTRY="us" ;;  # Alaska Daylight Time	UTC−08
    "EDT") DEST_COUNTRY="us" ;;  # Eastern Daylight Time (North America)	UTC−04
	"EST") DEST_COUNTRY="us" ;;  # Eastern Standard Time (North America)	UTC−05
    "CDT") DEST_COUNTRY="us" ;;  # Central Daylight Time (North America)	UTC−05
    "MDT") DEST_COUNTRY="us" ;;  # Mountain Daylight Time (North America)	UTC−06
    "CST") DEST_COUNTRY="us" ;;  # Central Standard Time (North America)	UTC−06
    "PDT") DEST_COUNTRY="us" ;;  # Pacific Daylight Time (North America)	UTC−07
    "MST") DEST_COUNTRY="us" ;;  # Mountain Standard Time (North America)	UTC−07
	*) DEST_COUNTRY="" ;;
  esac

  if [ "${DEST_COUNTRY}" = "??" ]; then
    echo "${DEST_COUNTRY} : ASUP_GENERATED_ON=${ASUP_GENERATED_ON} ASUP_TZ=${ASUP_TZ} ASUP_SNMP_LOCATION=${ASUP_SNMP_LOCATION} ASUP_CONTROLER_NAME=${ASUP_CONTROLER_NAME} ASUP_SERIAL_NUMBER=${ASUP_SERIAL_NUMBER} "|tee -a ${TMP}
	echo "" >> ${TMP}
  fi

  if [ "${DEST_COUNTRY}" = "" ]; then
#    echo "${DEST_COUNTRY} : ASUP_GENERATED_ON=${ASUP_GENERATED_ON} ASUP_TZ=${ASUP_TZ} ASUP_SNMP_LOCATION=${ASUP_SNMP_LOCATION}"
	echo -n "."
  fi

  if [ "${DEST_COUNTRY}" != "" ] && [ "${DEST_COUNTRY}" != "??" ] && [ ! -f /appl/mdr/mdrglob/afsp/${DEST_COUNTRY}/data/database/${FILE} ]; then
    echo "  Copy ${ASUP_CONTROLER_NAME} (${ASUP_SERIAL_NUMBER}) to /appl/mdr/mdrglob/afsp/${DEST_COUNTRY}/data/database/${FILE}"|tee -a ${LOG}
	cp ${FILE} /appl/mdr/mdrglob/afsp/${DEST_COUNTRY}/data/database/${FILE}
  fi

done  # ls -1

cat ${TMP} | mailx -s "NetApp systems with UTC in ASUP-date [${PGM} v${VER}]" ${MAILTO}
echo "  Mailed to ${MAILTO}"|tee -a ${LOG}

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

