
#!/bin/sh
# File	: proc_db_to_model-versions.sh
# By	: Maarten.deBoer@Atos.net, 150806
# SUbject	: Script to process DB-filers into model-version-file
#(0.2)	: Mod. When MODEL = "", them ?
#(0.3)	: Mod ? into model?
#(0.4)	: Mod proc_db_to_model-versions.sh -> No month
PGM="`basename $0|cut -d\. -f1`"
VER="0.4"
TMP="/tmp/${PGM}.$$"

BASEDIR="`pwd | sed 's/scripts//'|sed 's/bin//'`"
ETCENV="${BASEDIR}/etc/afsp.env"
LOG="${BASEDIR}/log/${PGM}.log"

DBDIR="${BASEDIR}/data/database"
OUT="${BASEDIR}/data/model-version/filer-name-loc-model-os-fw-sn-partner.csv"

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

echo '#"Controller Name";"Controller Locations";"Controller Model";"Controller OS Version";"Controlle r Firmware Version";"Controller Serial Number";"Controller Partner";"Controller Contact";' > ${OUT}

ls -1 ${DBDIR}/*.asc|while read DBFILE
do
# read DBFILE in
  DB_SERIAL_NUMBER="`grep ^SERIAL_NUMBER ${DBFILE}|awk -F\= '{print $2}'`"
  DB_CONTROLER_NAME="`grep ^CONTROLER_NAME ${DBFILE}|awk -F\= '{print $2}'`"
  DB_MODEL_NAME="`grep ^MODEL_NAME ${DBFILE}|awk -F\= '{print $2}'`"
  DB_OS_VERSION="`grep ^OS_VERSION ${DBFILE}|awk -F\= '{print $2}'`"
  DB_PARTNER_HOSTNAME="`grep ^PARTNER_HOSTNAME ${DBFILE}|awk -F\= '{print $2}'`"
  DB_SNMP_CONTACT="`grep ^SNMP_CONTACT ${DBFILE}|awk -F\= '{print $2}'`"
  DB_SNMP_LOCATION="`grep ^SNMP_LOCATION ${DBFILE}|awk -F\= '{print $2}'`"
  DB_ASUP_GENERATED_ON="`grep ^ASUP_GENERATED_ON ${DBFILE}|awk -F\= '{print $2}'`"
  DB_SYSTEM_ID="`grep ^SYSTEM_ID ${DBFILE}|awk -F\= '{print $2}'`"

  if [ "${DB_MODEL_NAME}" = "" ]; then
    DB_MODEL_NAME="model?"
  fi

  if [ "${DB_OS_VERSION}" = "" ]; then
    DB_OS_VERSION="version?"
  fi

  echo "${DB_CONTROLER_NAME};${DB_SNMP_LOCATION};${DB_MODEL_NAME};${DB_OS_VERSION};<fwversion>;${DB_SERIAL_NUMBER};${DB_PARTNER_HOSTNAME};${DB_SNMP_CONTACT};" |tee -a ${OUT}

done

# Create month dir
# etc/month-week.csv
ETC="${BASEDIR}/etc/date-week-month.csv"
if [ -f ${ETC} ]; then
#  DATE="`date +%F`"
#  MONTH="`grep ${DATE} ${ETC}|awk -F\; '{print $3}'`"
  MONTH="`date +%Y'm'%m`"

  echo "${DATE} -> ${MONTH}"
# Check if new week dir exists, otherwise create
  if [ ! -d ${BASEDIR}/data/versions/${MONTH} ]; then
    mkdir ${BASEDIR}/data/versions/${MONTH}
    cp ${OUT} ${BASEDIR}/data/versions/${MONTH}/
    echo "  Created ${BASEDIR}/data/versions/${MONTH}/ and copied ${OUT} to it"|tee -a ${LOG}
  fi  # -d
fi  # date-week-month


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

