#!/bin/csh -f
# (c) Copyright 1993-1997 Check Point Software Technologies Ltd.
# All rights reserved.
# 
# This is proprietary information of Check Point Software Technologies
# Ltd., which is provided for informational purposes only and for use
# solely in conjunction with the authorized use of Check Point Software
# Technologies Ltd. products.  The viewing and use of this information is
# subject, to the extent appropriate, to the terms and conditions of the
# license agreement that authorizes the use of the relevant product.
#
# $Header: /fw/cvs/fw-1/fwbalance/load_agent/install_agent.csh,v 1.1.2.13 1997/08/03 16:08:53 idan Exp $
#

alias E echo
alias get 'set \!^ = $<'
alias NL 'echo "  "'


set inetd = "/etc/inetd.conf"
set services = "/etc/services"

set path = (/usr/ucb /usr/bin /usr/sbin /bin $path)
unset port
unset services_answer
unset inet_answer

set hpux = "HP-UX"
set i386 = "i386"

set D0 = '$0'
set D1 = '$1'
set D2 = '$2'
set QUOTE = '"'


check_env:
	E "checking environment..."
	set OS = `uname -a`
	set res = `echo $OS | grep "$i386" | wc -l`
	if ( $res == 1 ) then		#  is solaris i386
		set path = (/bin $path)
	endif

	set ypserver = `ypwhich`

	if ( `whoami` != "root" ) then
		E "must be root to install load agent. aborting..."
		exit 1
	endif

	if ( ! -f "./load_agent" ) then
		E "can't find load agent's executable file for the installation."
		E "please run installation script from the executable file's directory."
		E "aborting..."
		exit 1
	endif

	set res = `ps -ef |& sed 's/$/ /' | grep '[/ 	]inetd[ 	]' | grep -v grep | awk "{print $D2};"`
	if ( "X$res" == "X" || $res <= 0 ) then
		set res = `ps -auxww |& sed 's/$/ /' | grep '[/ 	]inetd[ 	]' | grep -v grep | awk "{print $D2};"`
	endif
	if ( "X$res" == "X" || $res <= 0 ) then
			E "'inetd' daemon is not running. aborting..."
			E "you can run the load agent manualy, without 'inetd', but"
			E "pay attention that unless you will find a proper way to"
			E "install the agent, whenever the server will be"
			E "restarted, the agent wont start automatically."
			exit 1
	else
		@ inetd_pid = $res
	endif

	while (! -f "$services")
		NL
		E "can't find file '$services'. Warning : this may cause problems with inetd daemon"
		E -n "please enter your 'services' directory : "
		get services
		if ( "X${services:t}" == "X" ) then
			set services = "${services}services"
		else
			set services = "${services}/services"
		endif
	end

	while (! -f "$inetd")
		NL
		E "can't find file '$inetd'."
		E -n "please enter your 'inetd.conf' directory : "
		get inetd
		if ( "X${inetd:t}" == "X" ) then
			set inetd = "${inetd}inetd.conf"
		else
			set inetd = "${inetd}/inetd.conf"
		endif
	end

	set inst_date = `date '+%d/%m/%y %H:%M:%S'`




get_name:
	E "starting installation..."
	set load_agent = "load_agent"
	NL
	E -n "please enter a name for the load agent service [$load_agent] : "
	get name
	if ( "X$name" != "X" ) then
		set OK = `echo "X${name}X" | grep '[# 	]' | wc -l`		# the 'tab' char is here also
		if ($OK == 1) then
			E "'$name' must be a proper service name (one word with no '#')"
			goto get_name
		endif

		set load_agent = $name
	endif

	set checkedyp = 0
	set find_services = `grep "^[^#]*$load_agent" $services | grep -v "[_A-Za-z0-9]$load_agent" | grep -v "${load_agent}[_A-Za-z0-9]" | wc -l`

	if ( $find_services == 0 && "X$ypserver" != "X" ) then
		set checkedyp = 1
		set find_services = `ypcat services | grep "^[^#]*$load_agent" | grep -v "[_A-Za-z0-9]$load_agent" | grep -v "${load_agent}[_A-Za-z0-9]" | wc -l`
	endif

	if ( $find_services != 0 ) then
		E -n "service '$load_agent' already exists. use this service?[y/n] : "
		get services_answer
		if ( "X$services_answer" != "Xy" ) then
			goto get_name
		endif
	endif

	if ( $?services_answer == 1 ) then
		if ( "X$services_answer" == "Xy" && $checkedyp == 0 ) then
			set cont = 1
			goto ypcheck
		endif
return1:
		goto get_inst_dir
	endif



get_port:
	NL
	E -n "please enter a udp port number for the load agent : "
	get port

	set OK = `echo $port | grep '^[0-9]*[1-9][0-9]*$' | wc -l`
	if ( $OK != 1 ) then
		E port must be positive integer value smaller than 65536
		goto get_port
	endif

	@ port = $port	# remove leading zeros
	if ( $port >= 65536 ) then
		E port must be positive integer value smaller than 65536
		goto get_port
	endif

	set find_port = `grep "^[^#]*[ 	]${port}[ 	]*/[	]*udp[ 	]" $services | wc -l`
	if ( $find_port != 0 ) then
		E "this port is already used by another service"
		goto get_port
	endif



get_inst_dir:
	set inst_dir = "/bin"
	NL
	E -n "please enter installation directory for $load_agent [$inst_dir] : "
	get name
	if ( "X$name" != "X" ) then
		set inst_dir = $name
	endif

	if( ! -e "$inst_dir" ) then
		E "directory '$name' doesn't exist"
		goto get_inst_dir
	endif
	if( ! -d "$inst_dir" ) then
		E "'$name' is not a proper directory"
		goto get_inst_dir
	endif
	if( ! -r "$inst_dir" ) then
		E "'$name' has no read permission"
		goto get_inst_dir
	endif
	if( ! -w "$inst_dir" ) then
		E "'$name' has no write permission"
		goto get_inst_dir
	endif

	if ( "X${inst_dir:t}" != "X" ) then
		set inst_dir = "${inst_dir}/"
	endif

	set OK = `echo $inst_dir | grep "^/" | wc -l`
	if ( $OK != 1 ) then
		set static_path = `pwd`
		set inst_dir = "$static_path/$inst_dir"
	endif

	if( -e "${inst_dir}$load_agent" ) then
		E -n "file ${inst_dir}$load_agent exists. overwrite?[y/n] : "
		get answer
		if ("X$answer" != "Xy") then
			goto get_inst_dir
		endif
	endif



check_inet:
	set write_inetd = 1
	set find_inet = `grep "^[ 	]*${load_agent}[ 	]" $inetd | wc -l`
	if ( $find_inet != 0 ) then
		NL
		E "service '$load_agent' is already set in '$inetd'."
		E -n "do you want to remove the previous setting?[y/n] : "
		get inet_answer
		if ( "X$inet_answer" != "Xy" ) then
			set write_inetd = 0
		endif
	endif
	


unset_inet:
	if ( $write_inetd == 1 ) then
		cp $inetd $inetd.old
		E "saving '$inetd' in '$inetd.old'"
	endif
	if ( $?inet_answer == 1 ) then
		if ( "X$inet_answer" == "Xy" ) then
			awk "($D1 != ${QUOTE}${load_agent}${QUOTE}){print $D0;}" $inetd.old >! $inetd
			NL
		endif
	endif



install_service:
	if ( $?port != 0 ) then
		cp $services $services.old
		E "saving '$services' in '$services.old'"
		E "$load_agent	$port/udp	# Firewall 1  load agent  $inst_date"  >> $services
		set cont = 0
		goto ypcheck
	endif



install_inet:
	if ( $write_inetd == 1 ) then
		E "#" >> $inetd
		E "# Firewall-1  load agent - $inst_date" >> $inetd
		E "#" >> $inetd
		E "$load_agent	dgram	udp	wait	root	${inst_dir}$load_agent	$load_agent -1 0" >> $inetd
	endif

	cp "./load_agent" "${inst_dir}$load_agent"
	chmod 555 "${inst_dir}$load_agent"

	kill -1 $inetd_pid

	exit 0


ypcheck:
	if ( "X$ypserver" != "X" ) then
		set find_services = `ypcat services | grep "^[^#]*$load_agent" | grep -v "[_A-Za-z0-9]$load_agent" | grep -v "${load_agent}[_A-Za-z0-9]" | wc -l`

		if ( $find_services == 0 ) then
			E " "
			E "Attention:"
			E "This server is using the YP services. "
			E "The load agent service you chose is not on"
			E "the YP services list."
			E "Please add the  '$load_agent'  service to the"
			E "services list on  '$ypserver' , which is your"
			E "YP server, so that the 'inetd' daemon could"
			E "identify the load agent service."
			E "Please run this installation script again after that."

			E " "
			E " "
			E -n "do you want to continue this installation anyway?[y/n] : "
			get OK
			if ( "X$OK" != "Xy" ) then
				E "aborting installation..."
				exit 1
			endif

			E " "
			E "Warning : the 'inetd' daemon might not recognize the load agent service"
		endif
	endif

	if ( $cont == 1 ) then
		goto return1
	else
		goto install_inet
	endif
