#! /usr/bin/sh
#
# ident "@(#)es-load-default 1.2     03/01/14 SMI"
#
# Copyright 2002-2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# File: es-load-default

# usage function
#
print_usage() {
	$ECHO ""
	echolog 'Usage: es-load-default -a\\|d -f \\<module_info_file\\>'
	$ECHO ""
}

#$1 - module_info_file name
#$2 - key to get the value of
getValue() {
    file_name=$1
    key=$2
    $CAT $file_name | $GREP -v "^[ 	]*#" | $GREP "^[ 	]*${key}[ 	]*=" | $AWK 'BEGIN{FS="="} {print $2}' | $SED -e "s/^[ 	]*//" | $SED -e "s/[ 	]*$//"
}

#
#Main Starts here
#
CAT=/usr/bin/cat
AWK=/usr/bin/awk
GREP=/usr/bin/grep
SED=/usr/bin/sed
PS=/usr/bin/ps
ECHO=/usr/bin/echo
FGREP=/usr/bin/fgrep
EGREP=/usr/bin/egrep
MV=/usr/bin/mv
CP=/usr/bin/cp
CHMOD=/usr/bin/chmod

#source es-common.sh to get ENV variables
BASEDIR=`/usr/bin/pkgparam SUNWescom BASEDIR`
. ${BASEDIR}/SUNWsymon/sbin/es-common.sh

set_basedir

# check that only superuser can execute this script
check_root

# setup text domain to ES_SCRIPTS
setup_textdomain

# check the number of arguments

if [ $# -ne 3 ] ; then
	$ECHO ""
	print_usage
	exit 1
fi
append=0
delete=0
while getopts adf: OPT 
do
    case $OPT in
	a)
	    append=1;;
	d)
	    delete=1;;
	f)
	    mod_info_file=$OPTARG;;
	\?)
	    print_usage
	    exit 1;;
    esac
done

# This catches 
#./es-load-default -ad f file
# case

if [ ! -n "$mod_info_file" ] ; then
	print_usage
	exit 1
fi

# This catches the 
#./es-load-default -ad 
#case

if [ $append -eq 1 ] && [ $delete -eq 1 ] ; then
        print_usage
        exit 1
fi

# Check if module_info_file exists
#
if [ ! -r $mod_info_file ] ; then
    if [ ! -r ${BASEDIR}/../${mod_info_file} ]; then
	echolog '$2 does not exist' "$mod_info_file"
	exit 1
    else
	file_path="${BASEDIR}/../${mod_info_file}"
    fi
else
    file_path="$mod_info_file"
fi

# Check if agent is running. If so, exit

$PS -aef | $GREP "esd - init agent" | $FGREP -v grep > /dev/null 2>&1
if [ $? -eq 0 ] ; then
	echolog "agent is currently running. stop the agent before adding/deleting entry to base-modules-d.dat"
	exit 3
fi

module_name=`getValue ${file_path} module`
if [ ! -n "$module_name" ]; then
    echolog 'The module parameter does not exist in $2' "${file_path}"
    exit 1
fi

# Only append requires a location and a enterprise name. We
# can delete a entry with just the module name and instance name

location_name=`getValue $file_path location`
if [ ! -n "$location_name" ] && [ $append -eq 1 ]; then
    echolog 'The location parameter does not exist in $2' "${file_path}"
    exit 1
fi
enterprise_name=`getValue $file_path enterprise`
if [ ! -n "$enterprise_name" ] && [ $append -eq 1 ]; then
    echolog 'The enterprise parameter does not exist in $2' "${file_path}"
    exit 1
fi
instance_name=`getValue $file_path instance`

var_list=`$CAT $file_path | $GREP -v "^[ 	]*#" | $SED -e "s/^/@/"`
oldIFS=$IFS
IFS=@
param_list=""
for every_line in $var_list; do
    if [ -n "$every_line" ]; then
        key=`$ECHO "$every_line" | $AWK 'BEGIN{FS="=" } {print $1}' | $SED -e "s/^[ 	]*//" | $SED -e "s/[ 	]*$//"`
        value=`$ECHO "$every_line" | $AWK 'BEGIN{FS="=" } {print $2}'| $SED -e "s/^[ 	]*//" | $SED -e "s/[ 	]*$//"`
        one_value=`$ECHO "$key = \\\\\"$value\\\\\";"`
	param_list=`$ECHO $param_list $one_value`
    fi
done
IFS=$oldIFS

# replace any "+" in module_name with "\+" so that correct string is searched in# existing base-modules-d.dat

if [ -n "$instance_name" ]; then #This is multi-instance module
    module_args=`$ECHO "$module_name+$instance_name = \\".contexts.$instance_name$location_name $enterprise_name $module_name {$param_list }\\"`
    search_name=`$ECHO ${module_name}+${instance_name} | /usr/bin/sed s/'+'/'\\\\+'/g`
    actual_name=`$ECHO ${module_name}+${instance_name}`
else #This is a single instance module
    module_args=`$ECHO "$module_name = \\"$location_name $enterprise_name $module_name {$param_list }\\"`
    search_name=`$ECHO ${module_name} | /usr/bin/sed s/'+'/'\\\\+'/g`
    actual_name=`$ECHO ${module_name}`
fi

# Create a copy in VARDIR if one is not present

if [ ! -f $VARDIR/base-modules-d.dat ] ; then
	$CP -f ${BASEDIR}/base/cfg/base-modules-d.dat $VARDIR/base-modules-d.dat
	$CHMOD 600 $VARDIR/base-modules-d.dat
fi

$EGREP "^[ 	]*${search_name}[ 	]*=" $VARDIR/base-modules-d.dat > /dev/null 2>&1
status=$?
if [ $status -eq 0 ] && [ $append -eq 1  ]; then
	echolog 'module entry for $2 is already present in base-modules-d.dat' "${actual_name}"
	exit 2
fi

if [ $status -ne 0 ] && [ $delete -eq 1 ]; then
	echolog 'module entry for $2 is not present in base-modules-d.dat' "${actual_name}"
	exit 2
fi

if [ $append -eq 1  ]; then

   $ECHO "${module_args}" >> $VARDIR/base-modules-d.dat
   if [ $? -eq 0 ]; then
	echolog 'Entry for $2 successfully added' "${actual_name}"
   fi

elif [ $delete -eq 1 ]; then

   $EGREP -v "^[ 	]*${search_name}[ 	]*=" $VARDIR/base-modules-d.dat > /tmp/deleted_base-modules-d.dat$$
   $MV /tmp/deleted_base-modules-d.dat$$ $VARDIR/base-modules-d.dat
   if [ $? -eq 0 ]; then
	echolog 'Entry for $2 successfully deleted' "${actual_name}"
   fi


fi

exit 0
