#!/bin/sh

# Version 1.0
# Last modified: September 20, 2012

# Run 'isi_gconfig celog' and get the contents 
# and then parse out the contact information
# 	celog.contact_info.company
# 	celog.contact_info.location
# 	celog.contact_info.primary_name
# 	celog.contact_info.primary_phone1
# 	celog.contact_info.primary_phone2
# 	celog.contact_info.primary_email
# 	celog.contact_info.secondary_name
# 	celog.contact_info.secondary_phone1
# 	celog.contact_info.secondary_phone2
# 	celog.contact_info.secondary_email
#
# There are no arguments

# Logging
LOGFILE=/var/log/supportiq.log
log() {
    echo `date +"%FT%"T"%z"` $@ >> ${LOGFILE}
    logger -i "" ${@}
}

# The output filename, in the format that gstools expects:
Prefix='IsilonLogs'
GatherType='ContactInfo'
TimeTag=`date "+%Y%m%d-%H%M%S"`
Cluster=`grep "<name>" /etc/ifs/array.xml | awk -F'</?name>' '{print $2}'`
Suffix="txt"
OutputFile="${Prefix}-${GatherType}-${Cluster}-${TimeTag}.${Suffix}"

OutputDir='/var/crash/support/pkg'
mkdir -p "${OutputDir}"                   #Make sure the local-storage output directory exists

OutputPath=${OutputDir}/${OutputFile}

# Drop error file here if we are unable to proceed with the requested operation:
ErrDir=/var/crash/SupportIQ/upload/web
ErrFile='isi_gconfig_celog-contents-error.log'
ErrPath=${ErrDir}/${ErrFile}
ErrMsg="Cannot perform isi_gconfig celog gather on local storage"

# Try run the command:
CMD="isi_gconfig celog.contact_info >> ${OutputPath}"
echo "${CMD}"
CmdOutput=`eval "${CMD}" 2>&1`

Status=$?

isi_gather_info --esrs --re-upload=${OutputPath}




# If the command failed try figure out why and log it.
case ${Status} in
  "0")
    MSG="The gather of isi_gconfig celog contents command Succeeded."
    echo ${MSG}
    log "$0: ${MSG}"
	
	# It succeeded so remove the temp file
	CMD3="rm -rf ${OutputPath}"
	echo "${CMD3}"
	CmdOutput=`eval "${CMD3}" 2>&1`
    ;;
  *)
    echo The isi_gconfig celog contents gather command failed
    if [ ! -f "$OutputPath" ]; then
      ErrMsg="isi_gconfig celog output file $OutputPath does not exist"
      echo "$ErrMsg"
      echo ErrPath is "$ErrPath"
      date > $ErrPath
      echo "$CmdOutput" >> $ErrPath
      echo "$ErrMsg" >> $ErrPath
      #logging
      log "$0: ${ErrMsg}"
      log "$0: exited with: ${Status}"
      exit 1
    fi
    if [ ! -s "$OutputPath" ]; then
      ErrMsg="isi_gconfig celog output file $OutputPath is empty"
      echo "$ErrMsg"
      date > $ErrPath
      echo "$CmdOutput" >> $ErrPath
      echo "$ErrMsg" >> $ErrPath
      #logging
      log "$0: ${ErrMsg}"
      log "$0: exited with: ${Status}"
      exit 1
    fi
esac

#Success!
log "$0: exited with: ${Status}"
exit 0
