#!/usr/bin/ksh
#
# Copyright 2002-2003 by Sun Microsystems, Inc.
# All rights reserved.
#
# @(#)configure	1.19 03/03/07
#
# NSM management or agent configuration script for:
# 		query for install input (called from install script)
# 		commit install queried input (called from install script)
# 		post install (invoked by the user to reconfigure product)
###############################################################################

# Set . in root user path
PATH=.:/bin:/usr/bin:/usr/sbin
export PATH

#
# Set SUNWnsm basedir
BASEDIR=`/usr/bin/pkginfo -r SUNWnsmbj 2>&1`
if [ $? -ne 0 ]; then
   BASEDIR=/opt
fi

#
# Check for root
user=`/bin/id | grep root | wc -l`
if [ $user -eq 0 ]; then
	echo "You must be root to execute this script."
	exit 1
fi

#
# NSM configuration file
NSMCONF_FILE=$BASEDIR/SUNWnsm/etc/sstr.properties

# Log file - also set in sstrfuncs
LOG=/var/sadm/install/logs/SUNWnsm.log

#
# Make sure sstr.properties exists
if [ ! -f $NSMCONF_FILE ]; then
    echo "$BASEDIR/SUNWnsm/etc/sstr.properties does not exist, creating one..."  >> $LOG 2>&1
    /usr/bin/touch $BASEDIR/SUNWnsm/etc/sstr.properties
	if [ $? -ne 0 ]; then
	    echo "Failed to /usr/bin/touch $BASEDIR/SUNWnsm/etc/sstr.properties"
	    echo "Exiting, unsuccessful."
	    exit 1
	else
	    echo "/usr/bin/touch $BASEDIR/SUNWnsm/etc/sstr.properties succeeded." >> $LOG 2>&1
	fi
else
    echo "$BASEDIR/SUNWnsm/etc/sstr.properties found, proceeding with configure..." >> $LOG 2>&1
fi

#
# Includes
. $BASEDIR/SUNWnsm/sbin/sstrconf
. $BASEDIR/SUNWnsm/sbin/sstrfuncs
. $BASEDIR/SUNWnsm/sbin/sstrrun
. $BASEDIR/SUNWnsm/sbin/sstrsetup

#
# Local Functions

function usage {

	echo "usage: configure [-hsma] [-S scope] [-t tomcat non-ssl port]"
	echo "                 [-T tomcat ssl port] [-A apache port]"
    echo "                 [-R rmi registry port] [-n hostname]"
	echo "                 [-e admin email] [-g postgre sql port] [-r registry host]"
	echo "                 [-E Expect file] [-M query] [-W write]"
	echo "where: -h(elp), -s(ilent), -m(anagement station), -a(gent station), -b(oth station)"

	exit -1
}

#
# Main

# Log access time
timestamp=`date`
echo "$PRODUCT_NAME Configuration $timestamp" >> $LOG

# Command line arguments
if [ $# -ne 0 ]; then
	found=0
	while getopts "hsS:t:T:A:n:e:g:E:R:amb" opt; do
		found=0
		case $opt in
			h) usage;;
			s) MODE=$SILENT
				found=1;;
			S) NSM_SCOPE=$OPTARG
				found=1;;
			t) NSM_TOMCAT_NONSSL_PORT=$OPTARG
				found=1;;
			T) NSM_TOMCAT_SSL_PORT=$OPTARG
				found=1;;
			A) NSM_APACHE_PORT=$OPTARG
				found=1;;
			n) NSM_APACHE_SERVERNAME=$OPTARG
				found=1;;
			e) NSM_APACHE_SERVERADMIN=$OPTARG
				found=1;;
			R) NSM_RMI_REGISTRY_PORT=$OPTARG
				found=1;;
			g) NSM_POSTGRESQL_PORT=$OPTARG
				found=1;;
                        r) REGISTRY_HOST=$OPTARG
                                found=1;;
			E) EXPECT=$OPTARG
				found=1;;
			a) STATION_TYPE=$STATION_AGENT
				STATION_TYPE_ARG=$YES
				found=1;;
			m) STATION_TYPE=$STATION_MGMT
				STATION_TYPE_ARG=$YES
				found=1;;
                        b) STATION_TYPE=$STATION_BOTH
                                STATION_TYPE_ARG=$YES
                                found=1;;
	  	esac

		if [ $found -eq 0 ]; then
			usage
		fi
	done

	if [ $found -eq 0 ]; then
		usage
	fi
fi

# Introduction
if [ $MODE -ne $SILENT ]; then
	echo "$PRODUCT_NAME"
	echo
	echo "Configuration log: $LOG"

	# Communicate which station is about to be configured
        if [ $STATION_TYPE = $STATION_MGMT ]; then
            echo ""
            echo "Configuring Management Station."
        elif [ $STATION_TYPE = $STATION_AGENT ]; then
            echo ""
            echo "Configuring Agent Station."
        elif [ $STATION_TYPE = $STATION_BOTH ]; then
            echo ""
            echo "Configuring both Management and Agent Stations."
        else
            logit "ERROR: Can't determine station type."
        fi

	# Permission to proceed
	query_user "Proceed with configuration"
	if [ $? -ne 0 ]; then
		exit 0
   	fi

	# Type of station to configure
	get_station_type

        # Make sure product is shutdown completely
        echo ""
        insureshutdown

        # Make sure slp is shutdown completely
        insureshutdownslp

fi

if [ $STATION_TYPE = $STATION_MGMT ]; then

	# Process sstr mgmt configuration values then
	conf_mgmt_station
	if [ $? -ne 0 ]; then
		bailout
	fi

	# Save to sstr config 
	conf_write

	# Push the values out to the sstr mgmt components
	setup_mgmt_station
	if [ $? -ne 0 ]; then
		bailout
	fi

elif [ $STATION_TYPE = $STATION_AGENT ]; then

	# Process sstr agent configuration values then
	conf_agent_station
	if [ $? -ne 0 ]; then
		bailout
	fi

	# Save to sstr config 
	conf_write

	# Push the values out to the sstr agent components
	setup_agent_station
	if [ $? -ne 0 ]; then
		bailout
	fi

elif [ $STATION_TYPE = $STATION_BOTH ]; then

        # Process sstr mgmt and agent configuration values then
        conf_both_station
        if [ $? -ne 0 ]; then
                bailout
        fi

        # Save to sstr config
        conf_write

        # Push the values out to the sstr mgmt components
        setup_both_station
        if [ $? -ne 0 ]; then
                bailout
        fi

else
	logit "ERROR: Station type must be management, agent or both." 
	return 1
fi

logit
logit "Configuration successful."

exit 0
