#!/usr/bin/ksh
#
# Copyright 2002-2003 by Sun Microsystems, Inc.
# All rights reserved.
#
# @(#)sstrconf	1.19 03/03/07
#
# Read/Write NSM configuration values found in the sstr.properties file.
#
# All functions in this file should be treated as private except for
# the three public entry points:
#    	conf_mgmt_station  - configure management station
#    	conf_agent_station - configure agent station
#    	conf_both_station - configure both management and agent station
#    	conf_write - save management or agent station configuration values
#
# When sstr.properties is sourced, the sstr config is read.
###############################################################################

#
# Defines

# NSM registry host
NSM_REGISTRY_HOST=`uname -n`

# Includes
. /opt/SUNWnsm/etc/sstr.properties

#
# Function Primitives

NSMCONF_PORT=
function conf_user_verify_port {
	port=$1
	default_port=$2
	name="$3"
	used_ports="$4"

	once=0
	done=0
	while [ $done -eq 0 ]; do
		validate_port_format $port
		if [ $? -ne 0 ]; then
			tmp=`find_avail_port_not_in_list $default_port "$used_ports"`
			if [ $once -ne 0 ]; then
				logit "ERROR: $port is not a valid port number, setting port to $tmp."
			fi
			port=$tmp
		fi

		tmp=`find_avail_port_not_in_list $port "$used_ports"`
		if [ $tmp -ne $port ]; then
			if [ $once -ne 0 ]; then
				logit "ERROR: Port $port is already in use, the first available port is $tmp."
			fi
			port=$tmp
		fi

		query_user "Run $name server on port $port"
		if [ $? -eq 0 ]; then
			done=1
		else
			port=
			while [ -z "$port" ]; do
				echo
				read port?"Please enter the $name server port: "
			done
		fi

		once=1
	done

	NSMCONF_PORT=$port
}

function conf_silent_verify_port {
	port="$1"
	default_port="$2"
	name="$3"
	used_ports="$4"

	validate_port_format $port
	if [ $? -ne 0 ]; then
		logit "ERROR: $name port $port is not a valid port number."
		return 1
	fi

	tmp=`find_avail_port_not_in_list $port "$used_ports"`
	if [ $tmp -ne $port ]; then
		logit "ERROR: $name port $port is already in use, the first available port is $tmp."
		return 1
	fi

	NSMCONF_PORT=$port
	return 0
}

function conf_verify_port {
	port="$1"
	default_port="$2"
	name="$3"
	used_ports="$4"

	# Assign function pointer based on mode
	if [ $MODE -eq $SILENT ]; then
		conf_silent_verify_port "$port" "$default_port" "$name" "$used_ports"
	else
		conf_user_verify_port "$port" "$default_port" "$name" "$used_ports"
	fi

	return $?
}

function conf_mgmt_agent_verify_input {

	# Get rid of crummy command line port numbers or a bad data in sstr.properties
	validate_port_format $NSM_TOMCAT_NONSSL_PORT
	if [ $? -ne 0 ]; then
		NSM_TOMCAT_NONSSL_PORT=
	fi
	validate_port_format $NSM_TOMCAT_SSL_PORT
	if [ $? -ne 0 ]; then
		NSM_TOMCAT_SSL_PORT=
	fi
	validate_port_format $NSM_APACHE_PORT
	if [ $? -ne 0 ]; then
		NSM_APACHE_PORT=
	fi
	validate_port_format $NSM_POSTGRESQL_PORT
	if [ $? -ne 0 ]; then
		NSM_POSTGRESQL_PORT=
	fi
	validate_port_format $NSM_RMI_REGISTRY_PORT
	if [ $? -ne 0 ]; then
		NSM_RMI_REGISTRY_PORT=
	fi

	# Device Scope
        # This scope is needed for the Indy/Porsche box.
	if [ -z "$NSM_DEVICE_SCOPE" ]; then
		NSM_DEVICE_SCOPE=sun-sp
	fi

	# Scope
	if [ -z "$NSM_SCOPE" ]; then
		NSM_SCOPE=nsmscope
	fi

	#if [ $MODE -ne $SILENT ]; then
	#	finished=0
	#	while [ $finished -eq 0 ]; do
	#		query_user "Do you want to use the SLP scope $NSM_SCOPE"
	#		if [ $? -eq 0 ]; then
	#			finished=1
	#		else
	#			NSM_SCOPE=
	#			while [ -z "$NSM_SCOPE" ]; do
	#				echo
	#				read NSM_SCOPE?"Please enter the SLP scope: "
	#			done
	#		fi
	#	done
	#FI
}

function verify_scope_input {
	if [ $MODE -ne $SILENT ]; then
		finished=0
		while [ $finished -eq 0 ]; do
			query_user "Do you want to use the SLP scope $NSM_SCOPE"
			if [ $? -eq 0 ]; then
				finished=1
			else
				NSM_SCOPE=
				while [ -z "$NSM_SCOPE" ]; do
					echo
					read NSM_SCOPE?"Please enter the SLP scope: "
				done
			fi
		done
	fi
}

function conf_mgmt_station {

	# Common mgmt agent values
	conf_mgmt_agent_verify_input

	# PostgreSQL port
	conf_verify_port "$NSM_POSTGRESQL_PORT" "5437" "PostgreSQL" "$NSM_TOMCAT_NONSSL_PORT $NSM_TOMCAT_SSL_PORT $NSM_APACHE_PORT"
	if [ $? -ne 0 ] [ $MODE -eq $SILENT ]; then
		return 1
	fi
	NSM_POSTGRESQL_PORT=$NSMCONF_PORT

	# Tomcat non-SSL port
	conf_verify_port "$NSM_TOMCAT_NONSSL_PORT" "8180" "Tomcat non-SSL" "$NSM_TOMCAT_SSL_PORT $NSM_APACHE_PORT $NSM_POSTGRESQL_PORT"
	if [ $? -ne 0 ] [ $MODE -eq $SILENT ]; then
		return 1
	fi
	NSM_TOMCAT_NONSSL_PORT=$NSMCONF_PORT

	# Tomcat SSL port
	conf_verify_port "$NSM_TOMCAT_SSL_PORT" "8543" "Tomcat SSL" "$NSM_TOMCAT_NONSSL_PORT $NSM_APACHE_PORT $NSM_POSTGRESQL_PORT"
	if [ $? -ne 0 ] [ $MODE -eq $SILENT ]; then
		return 1
	fi
	NSM_TOMCAT_SSL_PORT=$NSMCONF_PORT

	# RMI Registry port
	conf_verify_port "$NSM_RMI_REGISTRY_PORT" "1099" "RMI Registry" "$NSM_TOMCAT_NONSSL_PORT $NSM_TOMCAT_SSL_PORT $NSM_POSTGRESQL_PORT $NSM_APACHE_PORT"
	if [ $? -ne 0 ] [ $MODE -eq $SILENT ]; then
		return 1
	fi
	NSM_RMI_REGISTRY_PORT=$NSMCONF_PORT

	verify_scope_input

	return 0
}

function conf_agent_station {

	# Common mgmt agent values
	conf_mgmt_agent_verify_input

	# Apache port
	conf_verify_port "$NSM_APACHE_PORT" "1024" "Apache" "$NSM_TOMCAT_NONSSL_PORT $NSM_TOMCAT_SSL_PORT $NSM_POSTGRESQL_PORT"
	if [ $? -ne 0 ] [ $MODE -eq $SILENT ]; then
		return 1
	fi
	NSM_APACHE_PORT=$NSMCONF_PORT

	# Apache hostname
	host=`get_host_name`
	if [ $MODE -eq $SILENT ]; then
		if [ -z "$host" ] && [ -z "NSM_APACHE_SERVERNAME" ]; then
			logit "ERROR: Unable to set Apache hostname."
			return 1
		elif [ -n "$host" ] && [ -z "$NSM_APACHE_SERVERNAME" ]; then
			NSM_APACHE_SERVERNAME=$host
		elif [ "$host" != "$NSM_APACHE_SERVERNAME" ]; then
			logit "WARNING: Supplied Apache hostname does not equal found hostname."
		fi
	else
		if [ -z "$host" ] && [ -z "$NSM_APACHE_SERVERNAME" ]; then
			echo
			while [ -z "$NSM_APACHE_SERVERNAME" ]; do
				read NSM_APACHE_SERVERNAME?"Please enter the fully qualified Apache hostname: "
			done
		elif [ -n "$host" ] && [ -n "$NSM_APACHE_SERVERNAME" ] &&
			  [ "$host" != "$NSM_APACHE_SERVERNAME" ]; then
			echo
			logit "WARNING: Found hostname $host does not equal $NSMCONF hostname $NSM_APACHE_SERVERNAME for Apache."
		elif [ -n "$host" ] && [ -z "$NSM_APACHE_SERVERNAME" ]; then
			NSM_APACHE_SERVERNAME=$host
		fi

		finished=0
#		while [ $finished -eq 0 ]; do
#			query_user "Do you want to use the Apache hostname $NSM_APACHE_SERVERNAME"
#			if [ $? -eq 0 ]; then
#				finished=1
#			else
#				NSM_APACHE_SERVERNAME=
#				while [ -z "$NSM_APACHE_SERVERNAME" ]; do
#					echo
#					read NSM_APACHE_SERVERNAME?"Please enter the fully qualified Apache hostname: "
#				done
#			fi
#		done
	fi

	# Apache email
	if [ -z "$NSM_APACHE_SERVERADMIN" ]; then
		NSM_APACHE_SERVERADMIN="root@$NSM_APACHE_SERVERNAME"
	fi
	if [ $MODE -ne $SILENT ]; then
		finished=0
		while [ $finished -eq 0 ]; do
			query_user "Do you want to use the Apache email address $NSM_APACHE_SERVERADMIN"
			if [ $? -eq 0 ]; then
				finished=1
			else
				NSM_APACHE_SERVERADMIN=
				while [ -z "$NSM_APACHE_SERVERADMIN" ]; do
					echo
					read NSM_APACHE_SERVERADMIN?"Please enter the Apache server email address for notification: "
				done
			fi
		done
	fi

	verify_scope_input
	return 0
}

function conf_both_station {

        # Common mgmt agent values
        conf_mgmt_agent_verify_input

        # PostgreSQL port
        conf_verify_port "$NSM_POSTGRESQL_PORT" "5437" "PostgreSQL" "$NSM_TOMCAT_NONSSL_PORT $NSM_TOMCAT_SSL_PORT $NSM_APACHE_PORT"
        if [ $? -ne 0 ] [ $MODE -eq $SILENT ]; then
                return 1
        fi
        NSM_POSTGRESQL_PORT=$NSMCONF_PORT

        # Tomcat non-SSL port
        conf_verify_port "$NSM_TOMCAT_NONSSL_PORT" "8180" "Tomcat non-SSL" "$NSM_TOMCAT_SSL_PORT $NSM_APACHE_PORT $NSM_POSTGRESQL_PORT"
        if [ $? -ne 0 ] [ $MODE -eq $SILENT ]; then
                return 1
        fi
        NSM_TOMCAT_NONSSL_PORT=$NSMCONF_PORT

        # Tomcat SSL port
        conf_verify_port "$NSM_TOMCAT_SSL_PORT" "8543" "Tomcat SSL" "$NSM_TOMCAT_NONSSL_PORT $NSM_APACHE_PORT $NSM_POSTGRESQL_PORT"
        if [ $? -ne 0 ] [ $MODE -eq $SILENT ]; then
                return 1
        fi
        NSM_TOMCAT_SSL_PORT=$NSMCONF_PORT

        # RMI Registry port
        conf_verify_port "$NSM_RMI_REGISTRY_PORT" "1099" "RMI Registry" "$NSM_TOMCAT_NONSSL_PORT $NSM_TOMCAT_SSL_PORT $NSM_POSTGRESQL_PORT $NSM_APACHE_PORT"
        if [ $? -ne 0 ] [ $MODE -eq $SILENT ]; then
        	return 1
        fi
        NSM_RMI_REGISTRY_PORT=$NSMCONF_PORT

        # Apache port
        conf_verify_port "$NSM_APACHE_PORT" "1024" "Apache" "$NSM_TOMCAT_NONSSL_PORT $NSM_TOMCAT_SSL_PORT $NSM_POSTGRESQL_PORT"
        if [ $? -ne 0 ] [ $MODE -eq $SILENT ]; then
                return 1
        fi
        NSM_APACHE_PORT=$NSMCONF_PORT

        # Apache hostname
        host=`get_host_name`
        if [ $MODE -eq $SILENT ]; then
                if [ -z "$host" ] && [ -z "NSM_APACHE_SERVERNAME" ]; then
                        logit "ERROR: Unable to set Apache hostname."
                        return 1
                elif [ -n "$host" ] && [ -z "$NSM_APACHE_SERVERNAME" ]; then
                        NSM_APACHE_SERVERNAME=$host
                elif [ "$host" != "$NSM_APACHE_SERVERNAME" ]; then
                        logit "WARNING: Supplied Apache hostname does not equal found hostname."
                fi
        else
                if [ -z "$host" ] && [ -z "$NSM_APACHE_SERVERNAME" ]; then
                        echo
                        while [ -z "$NSM_APACHE_SERVERNAME" ]; do
                                read NSM_APACHE_SERVERNAME?"Please enter the fully qualified Apache hostname: "
                        done
                elif [ -n "$host" ] && [ -n "$NSM_APACHE_SERVERNAME" ] &&
                          [ "$host" != "$NSM_APACHE_SERVERNAME" ]; then
                        echo
                        logit "WARNING: Found hostname $host does not equal $NSMCONF hostname $NSM_APACHE_SERVERNAME for Apache."
                elif [ -n "$host" ] && [ -z "$NSM_APACHE_SERVERNAME" ]; then
                        NSM_APACHE_SERVERNAME=$host
                fi

                finished=0
#                while [ $finished -eq 0 ]; do
#                        query_user "Do you want to use the Apache hostname $NSM_APACHE_SERVERNAME"
#                        if [ $? -eq 0 ]; then
#                                finished=1
#                        else
#                                NSM_APACHE_SERVERNAME=
#                                while [ -z "$NSM_APACHE_SERVERNAME" ]; do
#                                        echo
#                                        read NSM_APACHE_SERVERNAME?"Please enter the fully qualified Apache hostname: "
#                                done
#                        fi
#                done
        fi

        # Apache email
        if [ -z "$NSM_APACHE_SERVERADMIN" ]; then
                NSM_APACHE_SERVERADMIN="root@$NSM_APACHE_SERVERNAME"
        fi
        if [ $MODE -ne $SILENT ]; then
                finished=0
                while [ $finished -eq 0 ]; do
                        query_user "Do you want to use the Apache email address $NSM_APACHE_SERVERADMIN"
                        if [ $? -eq 0 ]; then
                                finished=1
                        else
                                NSM_APACHE_SERVERADMIN=
                                while [ -z "$NSM_APACHE_SERVERADMIN" ]; do
                                        echo
                                        read NSM_APACHE_SERVERADMIN?"Please enter the Apache server email address for notification: "
                                done
                        fi
                done
        fi

	verify_scope_input
        return 0
}

function conf_write {
	# Write mgmt and agent key value pairs to sstr.properties file. Both mgmt
	# and agent values are written because both mgmt and agent values
	# are read.
	echo "# Copyright (c) 2002-2003 by Sun Microsystems, Inc." > $NSMCONF_FILE
	echo "# All rights reserved." >> $NSMCONF_FILE
	echo "#" >> $NSMCONF_FILE
	echo "#ident  \"@(#)sstr.properties       1.0     2002/01/01 SMI\"" >> $NSMCONF_FILE
	echo "#" >> $NSMCONF_FILE
	echo "# ***WARNING***" >> $NSMCONF_FILE
	echo "# DO NOT MANUALLY EDIT THIS FILE." >> $NSMCONF_FILE
	echo "# PLEASE USE THE SSTR CONFIGURE TOOL." >> $NSMCONF_FILE
	echo "#" >> $NSMCONF_FILE
	echo "NSM_SCOPE=$NSM_SCOPE" >> $NSMCONF_FILE
	echo "NSM_DEVICE_SCOPE=$NSM_DEVICE_SCOPE" >> $NSMCONF_FILE
	echo "NSM_TOMCAT_NONSSL_PORT=$NSM_TOMCAT_NONSSL_PORT" >> $NSMCONF_FILE
	echo "NSM_TOMCAT_SSL_PORT=$NSM_TOMCAT_SSL_PORT" >> $NSMCONF_FILE
	echo "NSM_APACHE_PORT=$NSM_APACHE_PORT" >> $NSMCONF_FILE
	echo "NSM_APACHE_SERVERNAME=$NSM_APACHE_SERVERNAME" >> $NSMCONF_FILE
	echo "NSM_APACHE_SERVERADMIN=$NSM_APACHE_SERVERADMIN" >> $NSMCONF_FILE
	echo "NSM_POSTGRESQL_PORT=$NSM_POSTGRESQL_PORT" >> $NSMCONF_FILE
	echo "NSM_RMI_REGISTRY_PORT=$NSM_RMI_REGISTRY_PORT" >> $NSMCONF_FILE
	echo "REGISTRY_HOST=$NSM_REGISTRY_HOST" >> $NSMCONF_FILE
	echo "PAM_VERIFIER_DIR=/etc/opt/SUNWnsm/bin/" >> $NSMCONF_FILE
}

function conf_update {
        # The NSM_SCOPE has changed so update the NSM configuration file to match.
        cp $NSMCONF_FILE /tmp/nsmconf_file.tmp
        sed -e "s/NSM_SCOPE=.*/NSM_SCOPE=$NSM_SCOPE/" /tmp/nsmconf_file.tmp >$NSMCONF_FILE
        rm /tmp/nsmconf_file.tmp
}
