#!/bin/sh
#
# ident	"@(#)es-platform	1.3	03/03/03 SMI"
#
# Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

print_usage ()
{
    echo ""
    eval echo `/usr/bin/gettext 'Usage: $PROGNAME -ad instanceName'`
    /usr/bin/gettext '       Where the options represent:\n'
    /usr/bin/gettext '  a : to add an instance of platform agent\n'
    /usr/bin/gettext '  d : to delete an instance of platform agent\n'
    exit 1
}

validate_option ()
{
    if [ $ADD -eq 1 -a $DELETE -eq 1 ] ; then
	echolog "You can not specify both a and d option in the same command"
	print_usage
    fi
    if [ -z "$INSTANCENAME" ] ; then
	exit 0
    fi

    #INSTANCENAME=`/usr/bin/echo "$INSTANCENAME" | /usr/bin/sed -e "s/^\s*//"`
    #INSTANCENAME=`/usr/bin/echo "$INSTANCENAME" | /usr/bin/sed -e "s/\s*$//"`

    #
    #Instance name must not have spaces in between
    #

    echo $INSTANCENAME | /usr/bin/egrep " " > /dev/null 2>&1
    if [ $? -eq 0 ] ; then
        echolog "You can not have spaces in the instance name."
        exit 1
    fi

}
set_server_name() {
    
    while [ 1 -eq 1 ] 
    do
	get_input_from_user "Please enter the Sun Management Center Server Hostname: "
	if [ ! -z "$answer" ] ; then
	    ServerName="$answer"
            break
	fi
    done
}

configure_server() {

    ServerName=`$XGET ${VARDIR}/domain-config.x agent.agentServer`

    if [ -n "$ServerName" ] ; then
        return
    else
        set_server_name
    fi
}
get_port_number () 
{
    agentport=""
    agentport=`$XGET ${VARDIR}/domain-config.x agent.snmpPort`
    if [ -z "$agentport" ] ; then
        agentport=161
    fi

    port_list=""
    port_list=`$XGET $CFGFILE ControlBlock.portsused`

    tmpPort=1100
    port_list=`echo $port_list | /usr/bin/sed -e "s/,/ /g"`
    while [ 1 -eq 1 ] 
    do

	get_input_from_user "Please enter any port between 1100 and 65535 : "
	tmpPort="$answer"
        #checking for the validity of the port number
        if [ "$answer" = "" ] ; then
            continue
        fi

	/usr/bin/expr $tmpPort + 1  1> /dev/null  2>&1
	if [ $? -ne 0 ] ; then
	    echolog 'Port $2 is not a valid port number, try another number : ' "$tmpPort"
	    continue
	fi
	if [ $tmpPort -lt 1100 ] ; then
	    continue
	fi
	if [ $tmpPort -gt 65535 ] ; then
	    continue
	fi
        /usr/bin/netstat -an -P udp | /usr/bin/grep "\*\.$tmpPort " >/dev/null
	if [ $? -ne 0 ] ; then
            if [ $tmpPort -eq $agentport ] ; then
                echolog "This port is being used by Sun Management Center agent, try another number : "
                continue
            fi
            echo "$port_list" | /usr/bin/grep -w $tmpPort > /dev/nul 2>&1
            if [ $? -eq 0 ] ; then
                echolog "This port is being used by some instance, try another number : "
                continue
            fi
            InstancePort=$tmpPort
            break
        else
            echolog "This port is being used by some another process, try another number : " 
            continue
        fi 
    done 
}

patch_allinstances ()
{
    #
    # Only base-config.x file is creating the problem, so simply transffer the
    # content from base-config.x to user-config.x file.
    #

    #
    # If there is no base-config.x, this means either user-config.x was used or
    # no instance was created. So, simply return.
    #
    if [ ! -f "$VARDIR/base-config.x" ] ; then
        return
    fi

    #
    # If there were no instances created, simply return.
    #
    list_of_instances=""
    list_of_instances=`$XGET $CFGFILE ControlBlock.instances`
    if [ -z "$list_of_instances" ] ; then
        return
    fi

    /usr/bin/rm -f "$VARDIR/base-config.x"

    #
    #Update user-config.x file
    #
    if [ ! -f "$VARDIR/user-config.x" ] ; then
        /usr/bin/cp -f "$BASEDIR"/base/cfg/user-config.x "$VARDIR/user-config.x"
    fi

    list_of_instances=`echo $list_of_instances | /usr/bin/sed -e "s/,/ /g"`
    for each_instance in $list_of_instances
    do
        /usr/bin/sed -e "/^#Instance $each_instance Start/,/^#Instance $each_instance End/d" "$VARDIR"/user-config.x > /tmp/user-config.x.${PID}
        /usr/bin/mv /tmp/user-config.x.${PID} "$VARDIR"/user-config.x
        echo "#Instance $each_instance Start" >> "$VARDIR"/user-config.x
        echo "    $each_instance = { [ inherit security ]" >> "$VARDIR"/user-config.x
        echo "    }" >> "$VARDIR"/user-config.x
        echo "#Instance $each_instance End" >> "$VARDIR"/user-config.x
    done
}

add_instance () 
{
    list_of_instances=""
    list_of_instances=`$XGET $CFGFILE ControlBlock.instances`
    if [ !  -z "$list_of_instances" ] ; then
	echo "$list_of_instances" | /usr/bin/sed -e "s/,/ /g" | /usr/bin/grep -w $INSTANCENAME > /dev/null 2>&1
	if [ $? -eq 0 ] ; then
            echolog "This instance is already present."
	    return 0
	fi
    fi
    
    #
    #Update user-config.x file
    #

    if [ ! -f "$VARDIR/user-config.x" ] ; then
        /usr/bin/cp -f "$BASEDIR"/base/cfg/user-config.x "$VARDIR/user-config.x"
    fi

    /usr/bin/sed -e "/^#Instance $INSTANCENAME Start/,/^#Instance $INSTANCENAME End/d" "$VARDIR"/user-config.x > /tmp/user-config.x.${PID}
    /usr/bin/mv /tmp/user-config.x.${PID} "$VARDIR"/user-config.x
    echo "#Instance $INSTANCENAME Start" >> "$VARDIR"/user-config.x
    echo "    $INSTANCENAME = { [ inherit security ]" >> "$VARDIR"/user-config.x
    echo "    }" >> "$VARDIR"/user-config.x
    echo "#Instance $INSTANCENAME End" >> "$VARDIR"/user-config.x

    #
    #Update domain-config.x file
    #

    if [ ! -f "$VARDIR/domain-config.x" ] ; then
        /usr/bin/touch "$VARDIR/domain-config.x"
    fi
    configure_server
    get_port_number
    $XPUT ${VARDIR}/domain-config.x ${INSTANCENAME}.${INSTANCENAME}Server $ServerName
    $XPUT ${VARDIR}/domain-config.x ${INSTANCENAME}.snmpPort $InstancePort
    #check_port "${INSTANCENAME}.snmpPort" "$INSTANCENAME"
    $XPUT ${VARDIR}/domain-config.x ${INSTANCENAME}.description "$INSTANCENAME instance of platform agent"
    $XPUT ${VARDIR}/domain-config.x ${INSTANCENAME}.activeChannels "error warning info status history syslog"
    $XPUT ${VARDIR}/domain-config.x ${INSTANCENAME}.defaultOutput "clog://localhost/../log/${INSTANCENAME}.log;lines=1000;width=200;flags=rw+;mode=644"
    $XPUT ${VARDIR}/domain-config.x ${INSTANCENAME}.statusOutput "clog://localhost/../log/${INSTANCENAME}Status.log;lines=250;width=200;flags=rw+;mode=644"
    $XPUT ${VARDIR}/domain-config.x ${INSTANCENAME}.syslogOutput "syslog://localhost/LOG_ALERT;app=${INSTANCENAME};facility=LOG_DAEMON;logopt=LOG_PID"
    $XPUT ${VARDIR}/domain-config.x ${INSTANCENAME}.moduleFile module:${INSTANCENAME}-modules-d.dat
    $XPUT ${VARDIR}/domain-config.x ${INSTANCENAME}.randomSeed 2509
    $XPUT ${VARDIR}/domain-config.x ${INSTANCENAME}.niceValue 10
    $XPUT ${VARDIR}/domain-config.x ${INSTANCENAME}.numFileLimit max
    $XPUT ${VARDIR}/domain-config.x ${INSTANCENAME}.usmUserFiles "value:${INSTANCENAME}-usmuser-d.dat data:${INSTANCENAME}-usmusertbl-d.dat"
    $XPUT ${VARDIR}/domain-config.x ${INSTANCENAME}.usmAclFile "aclgrps:${INSTANCENAME}-usmaclgrps-d.dat"
    $XPUT ${VARDIR}/domain-config.x ${INSTANCENAME}.agentStatsFile "value:${INSTANCENAME}-agent-stats.dat alarmlimit:${INSTANCENAME}-agent-stats.dat"
    $XPUT ${VARDIR}/domain-config.x ${INSTANCENAME}.mib2SystemFile "def:mib2-system-d.def dat:${INSTANCENAME}-mib2-system.dat value:mib2-system.dat"
    $XPUT ${VARDIR}/domain-config.x ${INSTANCENAME}.enterprise sun

    #
    #Generate security keys for this instance
    #

    get_security_key 0
    move_datfile "${VARDIR}/${INSTANCENAME}-usmusertbl-d.dat"
    "$BASEDIR"/base/sbin/base-usm-seed.sh -s "$seed" -c $INSTANCENAME

    #
    #Copy other required files
    #
 
    /usr/bin/cat $BASEDIR/base/cfg/base-${COMPONENT}.x | /usr/bin/sed -e "s/$COMPONENT/$INSTANCENAME/g" > $VARDIR/base-${INSTANCENAME}.x
    /usr/bin/cp $BASEDIR/base/cfg/${COMPONENT}-acls-d.dat  $VARDIR/${INSTANCENAME}-acls-d.dat 
    /usr/bin/chmod 644 $VARDIR/${INSTANCENAME}-acls-d.dat
    /usr/bin/cp $BASEDIR/base/cfg/${COMPONENT}-modules-d.dat  $VARDIR/${INSTANCENAME}-modules-d.dat 
    /usr/bin/chmod 644  $VARDIR/${INSTANCENAME}-modules-d.dat
    /usr/bin/cp $BASEDIR/base/cfg/${COMPONENT}-oids-d.dat  $VARDIR/${INSTANCENAME}-oids-d.dat 
    /usr/bin/chmod 644  $VARDIR/${INSTANCENAME}-oids-d.dat
    /usr/bin/cp $BASEDIR/base/cfg/${COMPONENT}-snmp-cache-d.dat  $VARDIR/${INSTANCENAME}-snmp-cache-d.dat 
    /usr/bin/chmod 644  $VARDIR/${INSTANCENAME}-snmp-cache-d.dat
    /usr/bin/cp $BASEDIR/base/cfg/${COMPONENT}-usmaclgrps-d.dat  $VARDIR/${INSTANCENAME}-usmaclgrps-d.dat 
    /usr/bin/chmod 644  $VARDIR/${INSTANCENAME}-usmaclgrps-d.dat
    /usr/bin/cp $BASEDIR/base/cfg/${COMPONENT}-usmuser-d.dat  $VARDIR/${INSTANCENAME}-usmuser-d.dat 
    /usr/bin/chmod 644  $VARDIR/${INSTANCENAME}-usmuser-d.dat
    
    #
    #Put this instance in configuration file
    #

    list_of_instances=""
    list_of_instances=`$XGET $CFGFILE ControlBlock.instances`
    if [ -z "$list_of_instances" ] ; then
        list_of_instances="$INSTANCENAME"
    else
	list_of_instances="$list_of_instances,$INSTANCENAME"
    fi
    list_of_ports=""
    list_of_ports=`$XGET $CFGFILE ControlBlock.portsused`
    if [ -z "$list_of_ports" ] ; then
        list_of_ports="$InstancePort"
    else
	list_of_ports="$list_of_ports,$InstancePort"
    fi
    $XPUT $CFGFILE "ControlBlock.instances" "$list_of_instances"
    $XPUT $CFGFILE "ControlBlock.portsused" "$list_of_ports"
    $XPUT $CFGFILE ${INSTANCENAME}.availability 1
    $XPUT $CFGFILE ${INSTANCENAME}.snmpPort $InstancePort
}

delete_instance () 
{
    if [ -f "$VARDIR/user-config.x" ] ; then

	/usr/bin/sed -e "/^#Instance $INSTANCENAME Start/,/^#Instance $INSTANCENAME End/d" "$VARDIR"/user-config.x > /tmp/user-config.x.${PID}
	/usr/bin/mv /tmp/user-config.x.${PID} "$VARDIR"/user-config.x
    fi

    /usr/bin/rm -f $VARDIR/base-${INSTANCENAME}.x
    /usr/bin/rm -f $VARDIR/${INSTANCENAME}-acls-d.dat 
    /usr/bin/rm -f $VARDIR/${INSTANCENAME}-modules-d.dat 
    /usr/bin/rm -f $VARDIR/${INSTANCENAME}-oids-d.dat 
    /usr/bin/rm -f $VARDIR/${INSTANCENAME}-snmp-cache-d.dat 
    /usr/bin/rm -f $VARDIR/${INSTANCENAME}-usmaclgrps-d.dat 
    /usr/bin/rm -f $VARDIR/${INSTANCENAME}-usmuser-d.dat 
    /usr/bin/rm -f $VARDIR/${INSTANCENAME}-engine-d.dat
    /usr/bin/rm -f $VARDIR/${INSTANCENAME}-usmusertbl-d.dat
    /usr/bin/rm -f $VARDIR/${INSTANCENAME}-engine-d.dat.old
    /usr/bin/rm -f $VARDIR/${INSTANCENAME}-usmusertbl-d.dat.old

    list_of_instances=""
    list_of_instances=`$XGET $CFGFILE ControlBlock.instances`
    list_of_instances=`echo $list_of_instances | /usr/bin/sed -e "s/$INSTANCENAME,//"`
    list_of_instances=`echo $list_of_instances | /usr/bin/sed -e "s/$INSTANCENAME\$//"`

    port_used=""
    port_used=`$XGET $CFGFILE ${INSTANCENAME}.snmpPort`
    list_of_ports=""
    list_of_ports=`$XGET $CFGFILE ControlBlock.portsused`
    list_of_ports=`echo $list_of_ports | /usr/bin/sed -e "s/$port_used,//"`
    list_of_ports=`echo $list_of_ports | /usr/bin/sed -e "s/$port_used\$//"`

    $XPUT $CFGFILE ${INSTANCENAME}.availability 0
    $XPUT $CFGFILE "ControlBlock.instances" "$list_of_instances"
    $XPUT $CFGFILE "ControlBlock.portsused" "$list_of_ports"
}


#
#Starting Main Of The Script
#

umask 22
PID=$$
TMP_DIR=/tmp
PROGNAME=$0; export PROGNAME
BASEDIR=`/usr/bin/pkgparam SUNWescom BASEDIR`
if [ -z "${BASEDIR}" ]; then
    echo "No Components are installed..."
    exit 1
fi

. ${BASEDIR}/SUNWsymon/sbin/es-common.sh

set_basedir
check_root
LOGFILE="$VAROPTDIR/install/platform-instance.log$PID"
check_logfile
#check_logfile setup
check_osver
set_xput
ADD=0
DELETE=0
COMPONENT="platform"
INSTANCENAME=""
PATCHINSTANCES=0

CFGFILE="$VARDIR/platform-instances.dat"
/usr/bin/touch $CFGFILE

while getopts pa:d:c: OPT
do
    case $OPT in
	a)
	    ADD=1
	    INSTANCENAME="$OPTARG"
	    ;;
	c)
	    COMPONENT="$OPTARG"
	    ;;
	d)
	    DELETE=1
	    INSTANCENAME="$OPTARG"
	    ;;
	p)
	    PATCHINSTANCES=1
	    ;;
	\?)
            print_usage
	    ;;
    esac
done

if [ $PATCHINSTANCES -eq 1 ] ; then
    patch_allinstances 
    exit 0
fi

validate_option

if [ $ADD -eq 1 ] ; then
    add_instance 
fi
if [ $DELETE -eq 1 ] ; then
    delete_instance 
fi
exit 0
