#!/bin/sh
# ###############################################################
# Exceed Connection Server 2008
# Copyright (c) 1997-2008 Hummingbird Ltd. All Rights Reserved.
# ###############################################################
# ecsinstall2
# $Revision: 10408 $
# ###############################################################
 

# bash script to get the CM Port, FirstDisplayNumberfile and update node.cfg.


# the name of the log file to append information into is the 
# only argument

. ./sys/msg.txt

logfile=$1

pre="    "

getCMPort()
{
	portmin=1
	portmax=65535

	if [ -x /usr/bin/netstat ]; then
		nstat="/usr/bin/netstat"
	else
		nstat="/bin/netstat"
	fi

	tflag="0";

	thisFile=${admin_dir}/${eodcmporttmp}

	if [ -f ${thisFile} ] ; then
		cmport=`cat ${thisFile}`
	fi
	
	while [ "$tflag" = "0" ] 
	do
		${ECHO} "${pre}${TTXPM_ENTER_CM_PORT}"${cmport}${TTXPM_ENTER_CM_PORT2}
		read _cmport_
    
		if [ ! "${_cmport_}xxxx" = "xxxx" ]; then
		
# check port in range 

			if [  "$_cmport_" -gt "0" ] > /dev/null 2>&1 ; then 
				#null command 
				:	
			else
				${ECHO} "${pre}${TTXPM_BAD_PORT1} $portmin $TTXPM_BAD_PORT2 $portmax"
				continue
			fi
	
			if [ "$_cmport_" -lt "$portmin"  -o "$_cmport_" -gt "$portmax" ] ; then
				${ECHO} "${pre}${TTXPM_BAD_PORT1} $portmin $TTXPM_BAD_PORT2 $portmax"
				continue	
				
			elif [ -x "$nstat" ] && \
				( "$nstat" -na | grep "[.:]$_cmport_" > /dev/null ) ; then
				${ECHO} "${pre}${TTXPM_WARN_PORT_IN_USE1} $_cmport_ $TTXPM_WARN_PORT_IN_USE2"
				${ECHO} "${pre}${TTXPM_WARN_PORT_IN_USE3}"
	
				read resp

				if [ ! "${resp}" = "y" -a ! "${resp}" = "Y" ] ; then			
					continue
				fi
			fi

			cmport=${_cmport_}
		fi
		
		tflag="1"
			
	done
}

getFirstDisplay()
{
	firstdmin=1
	firstdmax=9999

	tflag=0
	
	thisFile=${admin_dir}/${eodfirstdisplaytmp}

	if [ -f ${thisFile} ] ; then
		num=`cat ${thisFile}`
	fi


	while [ "$tflag" = "0" ] 
	do
		${ECHO} "${pre}${TTXPM_ENTER_FIRST_DISPLAY}"${num}"${TTXPM_ENTER_FIRST_DISPLAY2}"
	
		read _num_
	
		if [ ! "${_num_}xxxx" = "xxxx" ]; then
			if [  "$_num_" -gt "0" ] > /dev/null 2>&1 ; then 
			#null command 
				:	
			else
				${ECHO} "${pre}${TTXPM_BAD_FIRSTD1} $firstdmin $TTXPM_BAD_FIRSTD2 $firstdmax"
				continue
			fi

			if [ "$_num_" -lt "$firstdmin" -o "$_num_" -gt "$firstdmax" ] ; then
				${ECHO} "${pre}${TTXPM_BAD_FIRSTD1} $firstdmin $TTXPM_BAD_FIRSTD2 $firstdmax"
				continue
			else
				num=${_num_}
			fi
		fi
		
	tflag=1
		
	done
}

update_logfile_again()
{
	cat >> $logfile << EOF
$INFO EoDCMPort=$cmport
$INFO EoDFirstDisplay=$num

EOF
}

updateConfigClusterLogFilesAgain()
{
	update_config_file_server_section_again
	update_logfile_again
}

update_config_file_server_section_again()
{
	cat >> $config_file << EOF
EoDFirstDisplay=$num

[CLUSTER_MANAGER]
EoDCMPort=$cmport

EOF
}

getServerSettingsThenConfirm()
{
	${ECHO} ""
	${ECHO} "$TITLE_BAR"
	${ECHO} $PROXY_MANAGER_OPTS_TITLE
	${ECHO} "$TITLE_BAR"
	${ECHO} ""

	getCMPort 
	getFirstDisplay

	${ECHO} ""
	${ECHO} "${pre}${TTXPM_OPTIONS}"
	${ECHO} "${pre}${TTXPM_CM_PORT_NUMBER} $cmport"
	${ECHO} "${pre}${TTXPM_FIRST_DISPLAY} $num"

	${ECHO} ""
	${ECHO} "${pre}${TTXPM_SAVE_OPTIONS0}"
	${ECHO} "${pre}${TTXPM_SAVE_OPTIONS}"

	read response

	if [ "${response}xxxx" = "yxxxx" -o "${response}xxxx" = "Yxxxx" ]; then
		updateConfigClusterLogFilesAgain
	elif [ "${response}xxx" = "qxxx" -o "${response}xxx" = "Qxxx" ] ; then
# exit with failure 
		exit 1
	else
		getServerSettingsThenConfirm
	fi
}

removeTmpFiles()
{
	eodTmpFiles="${eodfirstdisplaytmp} ${eodcmporttmp}"

	for tmpFile in ${eodTmpFiles} ; do
	{
		thisFile=${admin_dir}/${tmpFile}

		if [ -f ${thisFile} ] ; then
			rm -f ${thisFile}	> 	/dev/null 2>&1
		fi
	}
	done
}

getServerSettingsThenConfirm
removeTmpFiles

# exit with success
exit 0
