#!/bin/bash
#
# recordum_export_sla
#
# v0.1 2017-01-20 RE
#
# script to export sla data

MYSELF=$( basename $0 )
TIMESTAMP=$( date +%Y%m%d-%H%M%S )

# stop script if $ALERT Percent are in use
ALERT=80

# get our ID
# serial remove any dots
SERIAL=$( /usr/local/recordum/dataproc/getserial 2>/dev/null |tr . _ )
PRODUCT=$( cat /usr/local/recordum/PRODUCT_CODE )
if [[ -z "$PRODUCT" || ${SERIAL} == "UNKNOWN" ]]
then
    echo "Unable to identify product or serial"
    exit 2
fi

# helper
script_help() {
    cat <<EOF
${MYSELF} help:

This script creates an export for sla data. It needs root privileges!

Usage: ${MYSELF} -f <from> \\
             [ -t <type> ]

where:
    <from> ... full timestamp: 2014-06-12 12:20:00
    <type> ... type: daily, monthly, yearly
                     based upon type we use the relevant data from the timestamp
                   
EOF
}

if [[ $# -lt 1 ]]
then
    script_help
    exit 1
fi

while getopts "f:t:" option
do
    case ${option} in
        f) FROMDT=$OPTARG
            ;;
        t) SLATYPE=$OPTARG
            ;;
        *) script_help
            exit 2
            ;;
    esac
done

case ${SLATYPE} in
    1) TYPE=DAILY
        FOR=${FROMDT:8:2}
        ;;
    2) TYPE=MONTHLY
        FOR=${FROMDT:5:2}
        ;;
    3) TYPE=YEARLY
        FOR=${FROMDT:0:4}
        ;;
    *) echo "Unknown SLA type ("${SLATYPE}")"
        exit 3
esac

# backup directory
BAKDIR=backup/exports/sla
# db export file name
# exp.sla.TYPE[DAILY|MONTHLY|YEARLY].FOR[01-31|01-12|2017].PRODUCTCODE.SERIAL.CREATION_TIMESTAMP[YYYYMMDD-HHMMSS]
DBPOST=xml
DBFILE=exp.sla.${TYPE}.${FOR}.${PRODUCT}.${SERIAL}.${TIMESTAMP}.${DBPOST}

if [ "$( id -un )" != recordum ]; then
    echo "$MYSELF needs to be run as recordum. You are $USER."
    exit 4
fi

# test for space
USAGE=$( df -h /backup |grep '/backup' |awk '{ print $5 }' |cut -d '%' -f1 )
if [[ ${USAGE} -ge ${ALERT} ]]; then
    echo "Running out of space for exports. /backup ("${USAGE}"%)"
    exit 5
fi

# create the db export
DATAPROC=/usr/local/recordum/dataproc
EXPORTER=export_sla
# the -- indicates that all subsequent arguments should go to the script and not to the PHP interpreter binary
echo ${DBFILE}" "${FROMDT}" "${TYPE}
nice -n 15 /usr/bin/php -f ${DATAPROC}/${EXPORTER} -- "${DBFILE}" "${FROMDT}" "${TYPE}"

# return code of zero means we should have a xml file
ERRCODE=$?
if [[ ! ${ERRCODE} -eq 0 ]]
then
    echo "${EXPORTER}: failed to create a valid export file. <${ERRCODE}>"
    exit 6
fi
if [[ ! -f /${BAKDIR}/${DBFILE} ]]
then
    echo "/$BAKDIR/$DBFILE does not exist"
    exit 61
fi

# now create a tar ball of the xml file
# tar name
TARPOST=tgz
TARFILE=exp.sla.${TYPE}.${FOR}.${PRODUCT}.${SERIAL}.${TIMESTAMP}.${TARPOST}
echo "Packing it all together"
cd /
nice -n 15 tar czf /${BAKDIR}/${TARFILE} /${BAKDIR}/${DBFILE} 2>/dev/null

if [[ ! -f /${BAKDIR}/${TARFILE} ]]
then
    echo "/$BAKDIR/$TARFILE was not created"
    exit 7
fi

# cleanup
rm ${BAKDIR}/${DBFILE}
# set default owner
/bin/chown recordum:adm /${BAKDIR}/${TARFILE}

echo "Export of /$BAKDIR/$TARFILE is DONE"
# full success
exit 0
