
#!/bin/sh
# FIle	: import_netapp_data.sh
# By	: Maarten.deBoer@atos.net, 2019-7-10
# Subject	: Script to import data from NetApp in AFSP-(NetApp-)DB
# 
PGM=`basename $0|cut -d\. -f1`
VER="0.1"
TMP="/tmp/${PGM}.$$"

BASEDIR="`pwd | sed 's/scripts//'|sed 's/bin//'`"
LOG="${BASEDIR}/log/${PGM}.log"
NETAPPDBDIR="${BASEDIR}/data/database"
DBFILENAME=""
COUNTRY2ASUP="${BASEDIR}/data/country2asup.csv"

COUNTRYDIR="`echo ${BASEDIR}| cut -d\/ -f6`"
if [ ${COUNTRYDIR} != "netapp" ]; then
  echo "  COUNTRYDIR= NOT netapp . Wrong DIR. Exiting ..."
  exit 3
fi

INDIR="${BASEDIR}/incomming"
FILE="ATOS-AMER_NetApp-SAM-Report-2019-07_ASUP.csv"
#FILE="ATOS-EMEA_NetApp-SAM-Report-2019-07_ASUP.csv"
INFILE="${INDIR}/${FILE}"

echo "`date` ${PGM} v${VER} started." | tee -a ${LOG}
echo "BASEDIR=${BASEDIR}"
echo "INDIR=${INDIR}"
echo "INFILE=${INFILE}"
# Read Functions file
source ./afsp-db_functions.sh
DB_FUNC_VER
sleep 1

if [ -f ${INFILE} ]; then
  cat ${INFILE}|grep -v ^#|while read LINE
  do
    SYSTEMNAME=`echo ${LINE}|awk -F\; '{print $1}'`
# Check for nr as 1st char.
    SERNR=`echo ${LINE}|awk -F\; '{print $2}'|grep ^[0-9]`
    MODEL=`echo ${LINE}|awk -F\; '{print $3}'`
    OSVERSION=`echo ${LINE}|awk -F\; '{print $4}'`
    GROUPNAME=`echo ${LINE}|awk -F\; '{print $5}'`
    LASTASUP=`echo ${LINE}|awk -F\; '{print $6}'`
    ASUPTYPE=`echo ${LINE}|awk -F\; '{print $7}'`

    DBFILE="${NETAPPDBDIR}/${SERNR}.asc"
    echo "  ${SERNR}=${SYSTEMNAME}"

    if [ "${SERNR}" != "" ]; then
    # If DB-file found, do read 1st
      if [ -f ${DBFILE} ]; then
        DB_READ ${DBFILE}
      fi  # SERNR
    # Update VAR's
      DB_SERIAL_NUMBER="${SERNR}"
      DB_CONTROLER_NAME="${SYSTEMNAME}"
      DB_MODEL_NAME="${MODEL}"
      DB_OS_VERSION="${OSVERSION}"
      DB_NA_GROUP_NAME="${GROUPNAME}"
      DB_NA_LAST_ASUP="${LASTASUP}"
      DB_NA_ASUP_STATUS="${ASUPTYPE}"
# Update only when most of the VAR's are filed
      if [ "${DB_SERIAL_NUMBER}" != "" ] && [ "${DB_CONTROLER_NAME}" != "" ]; then
        DB_WRITE ${DBFILE}
      fi

    fi  # != ""

  done  # cat

fi  # INFILE


exit 0

