#!/bin/sh
# ####################################################################
# Exceed Connection Server 13.7
# Copyright (c) 1997-2009 Open Text Corporation. All Rights Reserved.
# ####################################################################
# ecsinstall2
# $Revision: 14422 $
# ####################################################################
 

# 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="    "

getThisPort()
{
	${ECHO} "${pre}${TTXPM_ENTER_CM_PORT}"${cmport}${TTXPM_ENTER_CM_PORT2}
	read _cmport_
    
	if [ ! "${_cmport_}xxxx" = "xxxx" ]; then
		this_port=${_cmport_}
	else
		this_port=${cmport}
	fi
}

get_netstat()
{
	if [ -x /usr/bin/netstat ]; then
		nstat="/usr/bin/netstat"
	else
		nstat="/bin/netstat"
	fi
}

get_cmport()
{
	thisFile=${admin_dir}/${eodcmporttmp}

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

getCMPort()
{
	get_netstat
	get_cmport
	
	portmin=1
	portmax=65535

	tflag="0";

	while [ "$tflag" = "0" ] 
	do
		getThisPort
		
# check_port_in_range		
		if [ ! "$this_port" -gt "0" ] > /dev/null 2>&1 ; then 
			${ECHO} "${pre}${TTXPM_BAD_PORT1} $portmin $TTXPM_BAD_PORT2 $portmax"
			continue
		fi

		if [ "$this_port" -lt "$portmin"  -o "$this_port" -gt "$portmax" ] ; then
			${ECHO} "${pre}${TTXPM_BAD_PORT1} $portmin $TTXPM_BAD_PORT2 $portmax"
			continue	

		elif [ -x "$nstat" ] && ( "$nstat" -na | grep "[.:]$this_port" > /dev/null ) ; then
			${ECHO} "${pre}${TTXPM_WARN_PORT_IN_USE1} $this_port $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=${this_port}		
		tflag="1"

# check_port_in_range		
			
	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
}

removeTmpFiles2()
{
	rm -f ${ssh_available}	> 	/dev/null 2>&1
	rm -f ${ssl_available}	> 	/dev/null 2>&1
}

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 
		removeTmpFiles
		removeTmpFiles2
		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
