:
PATH=/bin:/usr/bin:/etc
set -h
#set -x
#
#
# The following two functions create broadcast addresses 
# based upon the ip address placed in the variable IP
# the result is placed in BROADCAST
#
BROADCAST=
IP=
baddr0() {
	firstbyte=`/bin/echo $IP | sed -e 's/\..*$//'`
	if expr $firstbyte \< 128 > /dev/null
	then
		BROADCAST=`/bin/echo $IP | sed -e 's/\(.*\)\..*\..*\..*$/\1.0.0.0/'`
	elif expr $firstbyte \< 192 > /dev/null
	then
		BROADCAST=`/bin/echo $IP | sed -e 's/\(.*\..*\)\..*\..*$/\1.0.0/'`
	else
		BROADCAST=`/bin/echo $IP | sed -e 's/\(.*\..*\..*\)\..*$/\1.0/'`
	fi
}

baddr255() {
	firstbyte=`/bin/echo $IP | sed -e 's/\..*$//'`
	if expr $firstbyte \< 128 > /dev/null
	then
		BROADCAST=`/bin/echo $IP | sed -e 's/\(.*\)\..*\..*\..*$/\1.255.255.255/'`
	elif expr $firstbyte \< 192 > /dev/null
	then
		BROADCAST=`/bin/echo $IP | sed -e 's/\(.*\..*\)\..*\..*$/\1.255.255/'`
	else
		BROADCAST=`/bin/echo $IP | sed -e 's/\(.*\..*\..*\)\..*$/\1.255/'`
	fi
}

p() {
	set -f
	/bin/echo $*
	set +f
	return $?
}

# Prompt for yes or no answer - returns non-zero for no
getyn() {
	while	read yn
	do	case $yn in
		[yY]*)	return 0 			;;
		[nN]*)	return 1			;;
		*)	p "enter either y or n" 	;;
		esac
	done
}

#
# get a valid ethernet interface in IF?
#
IF_DLINK=
IF_WD=
IF_3COM=
IF_SLIP=
IF0=
IF1=
IP0=
IP1=
BROAD0=
BROAD1=
CONFIG_IF0=
CONFIG_IF1=
getenetif() {
	if [ -n "$IF1" ]
	then
		IF=$IF1
	else
		IF=$IF0
	fi
	while	p "Interface $IF IP address: \c"
	do	read ip
		case "$ip" in
		"$IP0"|"$IP1"|"$BROAD0"|"$BROAD1")
			p $ip is in use
			continue
			;;
		[0-9]*)	if [ -n "$IP0" ]
			then IP1=$ip
			else IP0=$ip
			fi
			break 2
			;;
		*)	p "Address must have n.n.n.n form, i.e., 192.9.200.9"
			;;
		esac
	done
	if [ -n "$IF1" ]
	then
		IP=$IP1
	else
		IP=$IP0
	fi
	p "Does Interface $IF use all 0's for its broadcast address: \c"
	if getyn
	then 
		baddr0
	else
		baddr255
	fi
	if [ -n "$IF1" ]
	then 
		BROAD1=$BROADCAST
	else 
		BROAD0=$BROADCAST
	fi
	if [ -n "$IF0" ]
	then	
		CONFIG_IF0="ifconfig ${IF0}0 $IP0 -trailers broadcast $BROAD0"
	fi
	if [ -n "$IF1" ]
	then	
		CONFIG_IF1="ifconfig ${IF1}0 $IP1 -trailers broadcast $BROAD1"
	fi
}

#
# get a valid slip interface
#
CONFIG_SLIP=
SLIP_SRC=
SLIP_DST=
SLIP_TTY=
getslipif() {
	while	p "Tty line: \c"
	do	read SLIP_TTY
		case "$SLIP_TTY" in
		t*) break 2;;
		*)	p 'Try ttyXX (where XX is a number)';;
		esac
	done
	while	p "Interface source (you) IP address: \c"
	do	read ip
		case "$ip" in
		"$IP0"|"$IP1"|"$BROAD0"|"$BROAD1")
			p $ip is in use
			continue
			;;
		[0-9]*)	SLIP_SRC=$ip
			break 2
			;;
		*)	p "Address must have n.n.n.n form, i.e., 192.9.200.9"
			;;
		esac
	done
	while	p "Interface destination (them) IP address: \c"
	do	read ip
		case "$ip" in
		"$IP0"|"$IP1"|"$BROAD0"|"$BROAD1"|"$SLIP_SRC")
			p $ip is in use
			continue
			;;
		[0-9]*)	SLIP_DST=$ip
			break 2
			;;
		*)	p "Address must have n.n.n.n form, i.e., 192.9.200.9"
			;;
		esac
	done
	p "Baud rate (default 9600): \c"
	read BAUD
	CONFIG_SLIP="/etc/slattach $SLIP_TTY $SLIP_SRC $SLIP_DST $BAUD"
}


#
# main()
#
cat - <<	EOF

			TCP SETUP

You'll be asked to supply values to a number of configuration parameters.
If there is a default, you may press 'Return' to use it.

Note: This shell script is designed to handle the most common subset of
tcp configuration needs.  It's possible that your needs may not be
addressed here (subnetting is an example).  If that is the case, the best
approach is to run through this anyway and then edit the resulting file.

EOF

ROOT=
TCPSCRIPT=${ROOT}/etc/tcp
HOSTSFILE=${ROOT}/etc/hosts
DEF_PATH="/bin:/usr/bin:/usr/local/bin:/etc"
DEF_HOST="host"
DEF_DOMAIN="UUCP"
DEF_IFLOOP="127.0.0.1"

#
# set path
#
CONFIG_PATH=$DEF_PATH
#
# set host
#
cat - <<	EOF

The host variable sets your host name.  This is the same as your uucp name.
The domain name comes next.

EOF

p "HOST: [$DEF_HOST] \c"
read x
if [ -n "$x" ]
then CONFIG_HOST=$x
else CONFIG_HOST=$DEF_HOST
fi
#
# set domain
#
cat - <<	EOF

The domain variable sets your domain name.  For most people this is "UUCP",
other examples: "sco.com" or "lachman.com".

EOF
p "DOMAIN: [$DEF_DOMAIN] \c"
read x
if [ -n "$x" ]
then CONFIG_DOMAIN=$x
else CONFIG_DOMAIN=$DEF_DOMAIN
fi
#
# set ethernet connection
#
if [ -f ${ROOT}/usr/lib/tcp/if.conf ]
then
	. ${ROOT}/usr/lib/tcp/if.conf
fi

if [ -n "$IF_WD" -o -n "$IF_3COM" -o -n "$IF_DLINK" ]
then 

cat - <<	EOF

		Ethernet Configuration

You must choose an IP addresses for each of your interfaces.  
The IP addresses must be in a n.n.n.n form. These values must 
coordinate with others on the same network.  

EOF

if [ -n "$IF_DLINK" ]
then
	IF0=$IF_DLINK
	getenetif
fi

if [ -n "$IF_3COM" ]
then
	IF0=$IF_3COM
	getenetif
fi

if [ -n "$IF_WD" ]
then
	if [ -n "$IF0" ]
	then
		IF1=$IF_WD
	else
		IF0=$IF_WD
	fi
	getenetif
fi
fi

if [ -n "$IF_SLIP" ]
then 	if [ -n "$IF0" ]
then
cat - <<	EOF

			SLIP Configuration

You must specify two IP addresses.  These address must reflect
a different network (i.e., the SLIP network) from your previous 
addresses.  Because SLIP is a point to point interface you must specify
both endpoints (you and them).  Suppose your ethernet network is
192.9.200 and your ethernet address is 192.9.200.100 (host = 100).
One possiblity is to choose 192.9.201 for the SLIP network. 
Supposing further (that's a lot of supposing) the other slip host is
192.9.201.200 (host = 200) then you could choose 192.9.201.100 for
your slip end point and 192.9.201.200 for their slip end point.

EOF
else
cat - <<	EOF

You must specify two more IP addresses.  Because SLIP is a point to 
point interface you must specify both endpoints (you and them).  
Suppose that the SLIP network is 192.9.201.  You need to know what
their host number is and choose a different one.  Something like
you: 192.9.201.100   and  them: 192.9.201.101.

EOF
fi
getslipif
fi

# Do they want tcp started at boot time
#
p "\nWould you like TCP started automatically when the system goes multiuser?"
if getyn
then 
	echo ":" > /etc/rc.d/6/tcp
	echo "/etc/tcp start" >> /etc/rc.d/6/tcp
	chmod 744 /etc/rc.d/6/tcp
fi

#
# localhost
#
CONFIG_IFLOOP="ifconfig lo0 $DEF_IFLOOP"

if [ -f "$HOSTSFILE" ]
then
	echo "Moving old ${HOSTSFILE} to ${HOSTSFILE}.OLD"
	mv ${HOSTSFILE} ${HOSTSFILE}.OLD
fi

echo "$DEF_IFLOOP		localhost" > $HOSTSFILE
if [ -n "$CONFIG_IF0" ]
then
	echo "$IP0		${CONFIG_HOST}" >> $HOSTSFILE
	echo "$IP0		${CONFIG_HOST}.${CONFIG_DOMAIN}" >> $HOSTSFILE
fi
if [ -n "$CONFIG_IF1" ]
then
	echo "$IP1		${CONFIG_HOST}_${IF1}" >> $HOSTSFILE
	echo "$IP1		${CONFIG_HOST}_${IF1}.${CONFIG_DOMAIN}" >> $HOSTSFILE
fi
if [ -n "$CONFIG_SLIP" ]
then
	echo "$SLIP_SRC		${CONFIG_HOST}_slip.${CONFIG_DOMAIN}" >> $HOSTSFILE
fi

if [ -f "$TCPSCRIPT" ]
then
	echo "Moving old ${TCPSCRIPT} to ${TCPSCRIPT}.OLD"
	mv ${TCPSCRIPT} ${TCPSCRIPT}.OLD
fi

sed \
    -e "s,CONFIG_PATH,$CONFIG_PATH," \
    -e "s,CONFIG_HOST,$CONFIG_HOST," \
    -e "s,CONFIG_DOMAIN,$CONFIG_DOMAIN," \
    -e "s,CONFIG_IF0,$CONFIG_IF0," \
    -e "s,CONFIG_IF1,$CONFIG_IF1," \
    -e "s,CONFIG_IFLOOP,$CONFIG_IFLOOP," \
    -e "s,CONFIG_SLIP,$CONFIG_SLIP," < /usr/lib/tcp/tcp.conf > $TCPSCRIPT

chmod 711 $TCPSCRIPT
