#!/bin/ksh -p
#
#  Validate the explorer defaults file
#
#  $Id$
#

EXP_VERSION=4.0

EXP_CHECK_VERSION='$Revision$'
export EXP_CHECK_VERSION

if [ -n "${DO_VERSION}" ]
then
    LIB=${0##*/}
    echo "${LIB} version: ${EXP_CHECK_VERSION}"
    exit
fi

# Source common functions
. ${EXP_LIB}/exp_common

# Must have EXP_DEFAULTS
require 'test -n "${EXP_DEFAULTS}"'
WARNINGS=0

# VARS variable index
VARS="EXP_SERIAL_${HOSTID} \
EXP_CUSTOMER_NAME \
EXP_CONTRACT_ID \
EXP_USER_NAME \
EXP_USER_EMAIL \
EXP_PHONE \
EXP_ADDRESS_1 \
EXP_ADDRESS_2 \
EXP_ADDRESS_CITY \
EXP_ADDRESS_STATE \
EXP_ADDRESS_ZIP \
EXP_ADDRESS_COUNTRY \
EXP_GEO \
EXP_REPLY \
EXP_CONFIGDB_NOTIFY \
EXP_WHICH"

exp_error () {
    MSG=$*

    echo "WARNING: ${MSG}"

    ((WARNINGS=WARNINGS + 1))
}

if [ -z "${EXP_DEFAULTS}" ]
then
    exp_error "DEFAULTS not set!"
    exit 1
fi

# Make sure we have a file to validate
if [ ! -f "${EXP_DEFAULTS}" ]
then
    exp_error "no defaults file!"
    exit 1
fi

# Walk through the variables we are interested in
for VAR in ${VARS}
do
    # Is variable present?
    cat ${EXP_DEFAULTS} | sed -e 's/#.*//g' | egrep -s -e "^${VAR}="
    if [ "$?" != 0 ]
    then
	# EXP_ADDRESS_2 need not be present
	if [ "${VAR}" = "EXP_ADDRESS_2" ]
	then
	    continue
	elif [ "${VAR}" = "EXP_CONFIGDB_NOTIFY" ]
	then
	    continue
	else
	    exp_error "${VAR} not found!"
	    continue
	fi
    fi

    # Is variable set?
    VAL=`cat ${EXP_DEFAULTS} | sed -e 's/#.*//g' | egrep -e "^${VAR}=" | awk -F= '{ print $2 }' | sed -e 's/"//g'`
    if [ -z "${VAL}" ]
    then
	# EXP_ADDRESS_2 need not be set
	if [ "${VAR}" = "EXP_ADDRESS_2" ]
	then
	    continue
	elif [ "${VAR}" = "EXP_CONFIGDB_NOTIFY" ]
	then
	    continue
	else
	    exp_error "${VAR} not set!"
	    continue
	fi
    fi

    # Make sure SPLIT size is between 1 and 9 MB
    if [ "${VAR}" = "EXP_EMAIL_SPLIT" ]
    then
	if [ "${VAL}" -lt 1048576 -o "${VAL}" -gt 9437184 ]
	then
	    exp_error "${VAR} is outside the accepted range 1048576 - 9437184"
	    continue
	fi
    fi
done

if [ "${WARNINGS}" -gt 0 ]
then
    echo ""
    echo "${WARNINGS} warnings found in ${EXP_DEFAULTS}"
    echo ""

    exit 1
fi
exit 0
