#!/bin/sh
#################################################################
#
#  SpiderX25 - Configuration Utilities
#
#  Copyright 1991  Spider Systems Limited
#
#  NETCREATE
#
#    Script to create $NETDFILE(netconf), $IXETFILE, $STARTSLIPFILE
#    and call to create $NBFILE(brdconf) and $ETHFILE(ethconf).
#
#################################################################

#
# Modified 11th December 1994 S. McKinty
#
# major changes to cope with new config file system. The file x25conf
# no longer exists, so the section of this file which used to parse
# it has been replaced by a program which reads the new files and
# produces a similar output.
# All validation is now done by that program, so most of the checking
# has been dropped from this script.
#
# Can cope with either or both of SpiderTCP and SpiderX.25,
# as well as on-board/in kernel LLC2/LAPB.

# Include related definitions
. /etc/snet_info

# Uses the following information files created during netconfig option
# in sysadm:
#		$LIPFILE
#	Files in $X25CONFIGFILE/config

# Resulting netconf file will be created from scratch,
# and copied to $NETDFILE, when new file is complete.


# The netconf file is produced from two sub-files, the "modules" section 
# ($TMPF2), where device aliases and types are set up, and the "streams" 
# section ($TMPF3+$TMPF6) where the network topology is defined and various
# module calls are set up.  In both cases, appropriate line entries are 
# produced for:
# ii)  SunLinkX.25 only,
# iii) Both SpiderTCP and SpiderX.25,
#
# NB, for Sun it is Sun TCP and not Spider TCP, variable SUN_TCP defines this

# Set up temporary files
TMPF1=/tmp/netcreate.A$$
TMPF2=/tmp/netcreate.B$$
TMPF3=/tmp/netcreate.C$$
TMPF4=/tmp/netcreate.D$$
TMPF5=/tmp/netcreate.E$$
TMPF6=/tmp/netcreate.F$$
TMPF7=/tmp/netcreate.G$$
TMPF8=/tmp/netcreate.H$$
# Don't create this $TMPF8 yet, its existence 
# indicates ip-ixe links are present.
TMPF9=/tmp/netcreate.I$$
# Don't create this $TMPF9 yet, its existence
# indicates that at least one x25 line has been configured.
TMPF10=/tmp/netcreate.J$$
# Don't create this $TMPF10 yet, its existence
# indicates that PVC's have been configured at X.25 level.
TMPF11=/tmp/netcreate.K$$
TMPF12=/tmp/netcreate.L$$

>$TMPF1
>$TMPF2 
>$TMPF3
>$TMPF4
>$TMPF5
>$TMPF6
>$TMPF7


# remove temporary files and get out
trap "tidy_up; exit 1" 1 2 3 15

#
# Function to check dates of all the files we might use, and and
# determine if any of them are newer than the netconf file. If any
# of them # newer they will appear at the head of this list,
# otherwise netconf will be there, in which case we do nothing.
#
date_check()
{
    echo "Checking dates..."
    set -- `ls -t $NETDFILE $X25CONFIGFILES/config/link_config_*.cfg \
$X25CONFIGFILES/nuimapconf $X25CONFIGFILES/ixemapconf \
$X25CONFIGFILES/pvcmapconf $X25CONFIGFILES/config/IXE_Network_*.ixe \
/etc/system $LIPFILE 2>/dev/null`
    test "x$1" = "x$NETDFILE"
}


# Function to tidy up temporary files.
tidy_up()
{
rm -f $TMPF1 $TMPF2 $TMPF3 $TMPF4 $TMPF5 $TMPF6 $TMPF7 $TMPF8 $TMPF9 \
$TMPF10 $TMPF11 $TMPF12
echo "====== Done ======\n"
}


if date_check
then
    echo "$NETDFILE is up to date" >&2
    exit 0
else
    echo "Creating $NETDFILE..." >&2
fi

################## Spider TCP only ##################

if [ "$SUN_TCP" = "1" ]
then
	################## $LIPFILE check ###################
	if [ ! -f $LIPFILE ]
	then
		echo "Error in $0: $LIPFILE does not exist." >&2
		echo "Please select ipconfig option." >&2
		tidy_up
		exit 1
	fi


	################## IP address check #################
	# Flag any repetition of an IP address (4th field) in $LIPFILE
	non_un_ip=""
# NCPIX bugid 12311796  esc 503909  ES 12/20/95
	#	non_un_ip=`cuts 0 4 $LIPFILE | grep -v '?' | sort | uniq -d`
# end of fix
	# If non_un_ip > 0 tokens then error
	if [ "$non_un_ip" != "" ]
	then
		echo "Error in $LIPFILE: repeated IP addresses:$non_un_ip" >&2
		echo "You may correct this using the ipconfig option." >&2
		tidy_up
		exit 1
	fi


fi

rm -f $TMPF9 $TMPF10

################## Spider X.25 only ##################

if [ "$SX25" = "1" ]
then
# Generate LAPB, LLC2, X25 entries

# Use new parse program, wan entries will change with full 9.0 system
	$X25LIBFILES/clist |
	while read prefix board_no line_no link_id reg x25addr board_type lpc hpc lan_conns cfile junk
	do
		MO=
		# indicate that one X.25 line has been configured
		>$TMPF9

		# clear wantune and wanmap string (wanmap not currently used)
		WT=
		WM=

		# Clear device command string
		DC=

		# Deduce full prefix from board type
		# e.g. "wansA" from "MCP"
		# Next line should be defined in x_info.
		prefix_choices="$wanchoice $ethrchoice wloopA mlpA"
		got_full_prefix=0
		
		for poss_p in $prefix_choices
		do
			eval pchoice=\$${poss_p}_type	# MCP, HSI etc
			if [ "$pchoice" = "$board_type" ]
			then
				prefix=$poss_p
				got_full_prefix=1
				break
			fi	# test pchoice
		done	# poss_p in prefix_choices	
		
		if [ $got_full_prefix -ne 1 ]
		then
			# Can't find provided board_type in snet_info.
			echo "Error in $0: Board type $board_type unrecognised." >&2
			continue	# process next entry in $LX25FILE
		fi	# test got_full_prefix

		# By this point, we've deduced the prefix from the board type,
		# and use it to determine board-specific constants from
		# the snet_info files.	

		eval dev=\$${prefix}_dev
		eval mod=\$${prefix}_modname

	# Cater for llc2/ethr on-board/in kernel

		if [ "$dev" = "llc2" ]
		then
			eval eth_onb=\$${prefix}_onb
			if [ "$llc2_onb" = "1" ]	# ethr, llc2 on board
			then
				token1=$dev$board_no
				token2=`expr $line_no + 1`
				token3=$token1
			elif [ "$eth_onb" = "1" ]	# ethr on board, llc2 in kernel
			then
				token1="${dev}"
				token2="$prefix$board_no"
				token3=$dev
				expl_mux=`expr $line_no + 1`
				echo "$token2	$expl_mux" >> $TMPF2
				echo "$token2	d	/dev/$token2" >> $TMPF1
				echo "echo:$board_no	$token2	ECHO_TYPE" >> $TMPF7
			elif [ "$eth_onb" != "1" ]	# ethr, llc2 in kernel 
			then
				eval tokdev=\$${prefix}_device
				# next line probably generic.
				eval num_lines=\$${prefix}_lines
				line=`expr $board_no \* $num_lines + $line_no`

				token1="${dev}"
				token2="$prefix$board_no"
				token3=$dev
				token4=$tokdev
				echo "$token2	dc	$token4" >> $TMPF1
				DC="$token2\t-\tDL_ATTACH=${line}\\n"
			else
				echo "$0: Failure outputting ethr/llc2 entries" >&2
				tidy_up
				exit 1
			fi
			token6=$token2
		fi

	# Cater for lapb/wans on-board/in kernel
		if [ "$dev" = "lapb" ]
		then
			eval wan_onb=\$${prefix}_onb
			if [ "$lapb_onb" = "1" ]	# wans, lapb on board (never happens in Sun)
			then
				token1=$dev$board_no
				token2=`expr $line_no + 1`
				token3=$token1
				tokdev=/dev/$token1
#
# wans on board, lapb in kernel (never happens in Sun)
#
			elif [ "$wan_onb" = "1" ]
			then
				token1="${dev}"
				token2="wan$line"
				token3=$dev
				tokdev=/dev/$token2
				expl_mux=`expr $line_no + 1`
				echo "$token2	$expl_mux" >> $TMPF2
				echo "$token2	d	/dev/$token2" >> $TMPF1
				echo "$token3	d	/dev/$token3" >> $TMPF1
#
# wans, lapb in kernel (Sun is always like this)
#
			elif [ "$wan_onb" != "1" ]
			then
				# next line probably generic.
				eval num_lines=\$${prefix}_lines
				line=`expr $board_no \* $num_lines + $line_no`

				board_id=`expr "$prefix" : "....\(.\)"`
				eval tokdev=\$${prefix}_device

				token1="${dev}"
				token2="wan${board_id}$line"
				token3=$dev
				token4="$tokdev$line"
#
# New support for translation module between wan and lapb if specified
#
				if [ "$mod" != "" ]
				then
					token5="mod${board_id}$line"
					echo "$token5	m	$mod" >> $TMPF1
					MO="$token5	$token2\\n"
					token6=$token5
				else
					token6=$token2
				fi

				echo "$token2	d	$token4" >> $TMPF1

			else
				echo "$0: Failure outputting wans/lapb entries" >&2
				tidy_up
				exit 1
			fi
#
# set up wantune command for this device
#
			if [ $mod != "" ]
			then
				WT="\t\tSHELL=\"wantune -P -l $link_id -d $tokdev -m $mod $cfile\" \\\\\n"
			else
				WT="\t\tSHELL=\"wantune -P -l $link_id -d $tokdev $cfile\" \\\\\n"
			fi
		fi

#
# cater for MLP
#
		tokenm=
		if [ "$dev" = "mlp" ]
		then
			token3=lapb
			tokenm=mlp
			eval mlpdev=\$${prefix}_device
			echo "$tokenm	dc	$mlpdev" >> $TMPF1

# assume clist takes care of basic checking. Next section is
# the same as for basic lapb/llc2 items (see above)
			$X25LIBFILES/clist -m $link_id |
			while read link_id prefix board_no line_no ll_pfx ll_linkid ll_reg priority board_type junk
			do
				# Deduce full prefix from board type
				# e.g. "wansA" from "MCP"
				# Next line should be defined in x_info.
				prefix_choices="$wanchoice $ethrchoice wloopA"
				got_full_prefix=0
		
				for poss_p in $prefix_choices
				do
					eval pchoice=\$${poss_p}_type	# MCP, HSI etc
					if [ "$pchoice" = "$board_type" ]
					then
						prefix=$poss_p
						got_full_prefix=1
						break
					fi	# test pchoice
				done	# poss_p in prefix_choices	
		
				if [ $got_full_prefix -ne 1 ]
				then
					# Can't find provided board_type in snet_info.
					echo "Error in $0: Board type $board_type unrecognised." >&2
					continue	# process next entry
				fi	# test got_full_prefix

				# By this point, we've deduced the prefix from the board type,
				# and use it to determine board-specific constants from
				# the snet_info files.	

				eval dev=\$${prefix}_dev
				eval mod=\$${prefix}_modname

				if [ "$wans_onb" != "1" ]
				then
					eval num_lines=\$${prefix}_lines
					line=`expr $board_no \* $num_lines + $line_no`

					board_id=`expr "$prefix" : "....\(.\)"`
					eval tokdev=\$${prefix}_device

					token1="${dev}"
					token2="wan${board_id}$line"
					token3=$dev
					token4="$tokdev$line"

				#
				# New support for translation module between wan and lapb if specified
				#
					MO=
					if [ "$mod" != "" ]
					then
						token5="mod${board_id}$line"
						echo "$token5	m	$mod" >> $TMPF1
						MO="$token5	$token2\\n"
						token6=$token5
					else
						token6=$token2
					fi

					echo "$token2	d	$token4" >> $TMPF1
				else
					echo "$0: Failure outputing MLP wans/lapb entries" >$2
					tidy_up
					exit 1
				fi
                                # Create the STREAMS section

				if [ $mod != "" ]
				then
					WT="\t\tSHELL=\"wantune -P -l $ll_linkid -d $tokdev -m $mod\" \\\\\n"
				else
					WT="\t\tSHELL=\"wantune -P -l $ll_linkid -d $tokdev\" \\\\\n"
				fi
				MSS="MLP_SET_LINKID={$ll_linkid, $link_id, $priority}"

				echo "${MO}$token1\t$token6\tLL_SET_LINKID={$ll_linkid, $ll_reg}\\\\\n${WT}${WM}\t\tSHELL=\"lltune -P -p $dev -l $ll_linkid -d /dev/$token3\"\n$tokenm\t$token1\t${MSS}\n" >> $TMPF2
			done
		fi

#
# and for LLC2
#
		if [ "$dev" = "llc2" ]
		then
			# Split X.25 address section into:
			# MAC, local SAP, loopback SAP.
			# Assume "clist" checked for non-zero, even SAPs.

			# MAC = first 12 digits
			MAC=`expr "$x25addr" : "^\(............\)"`

			# local SAP = digits 13, 14
			local_SAP=`expr "$x25addr" : "............\(..\).."`
			
			# loopback SAP = digits 15, 16
			loop_SAP=`expr "$x25addr" : "..............\(..\)$"`

			# must check here
			# that they are not the same value.
			if test "$local_SAP" = "$loop_SAP"
			then
				echo "$0: Error:local-SAP($local_SAP) same as loop-SAP($loop_SAP)" >&2
				tidy_up
				exit 1
			fi
			
			fullx25addr="-a $MAC$local_SAP"

		elif [ "$dev" = "lapb" -o "$dev" = "mlp" ]
		then
			fullx25addr="-a $x25addr"
			local_SAP=""
			loop_SAP=""
		fi

		# Check if X.32 configured and not on a LAN.
		# If so, add x32map line to netconf file
		# 
		if [ -s "$X25CONFIGFILES/x32mapconf" -a "$reg" != "LC_LLC2" ]
		then
			X32="SHELL=\"x32map -P -l $link_id\" \\\\\n\t\t"
		else
			X32=
		fi

		#
		# Similar check for the nuimapconf file
		#
		if [ -s "$X25CONFIGFILES/nuimapconf" ]
		then
			NUI="SHELL=\"nuimap -Z -d /dev/x25\" \\\\\n\t\tSHELL=\"nuimap -P -f $X25CONFIGFILES/nuimapconf -d /dev/x25\" \\\\\n\t\t"
		else
			NUI=
		fi

		XSS="X25_SET_LINKID={$link_id,, 1}"

		if [ "$tokenm" = "mlp" ]
		then
			echo "x25\t$tokenm\tSHELL=\"mlptune -P -l $link_id -d /dev/mlp $cfile\"\t\\\\\n\t\tSHELL=\"x25tune -P -l $link_id -d /dev/x25 $fullx25addr $cfile\" \\\\\n\t\t${X32}${NUI}${XSS}\n" >> $TMPF2
		else
			echo "${DC}${MO}$token1\t$token6\tLL_SET_LINKID={$link_id, $reg} \\\\\n${WT}${WM}\t\tSHELL=\"lltune -P -p $dev -l $link_id -d /dev/$token3 $cfile\"" >> $TMPF2
			if [ "$reg" = "LC_LLC2" ]
			then
				echo "x25\t-\tSHELL=\"x25tune -P -l $link_id -d /dev/x25 $fullx25addr $cfile\"" >> $TMPF2
			else
				echo "x25\t$token1\tSHELL=\"x25tune -P -l $link_id -d /dev/x25 $fullx25addr $cfile\" \\\\\n\t\t${X32}${NUI}${XSS}\n" >> $TMPF2
			fi
		fi
                if [ "$reg" = "LC_LLC2" ]
                then
#
# Calculate correct number of "x25 llc2" entries. Always add two to
# whatever the user specified, to allow for the two listening
# streams on SAPs 70 and 7E
#
			NO_LLC2=`expr $lan_conns + 2`
                        
                        if [ $NO_LLC2 -gt 0 ]
                        then
				echo "x25\t$token1\tX25_SET_LINKID={$link_id, $local_SAP, 1}" >> $TMPF2
                        fi
                          
                        if [ $NO_LLC2 -gt 1 ]
                        then
				echo "x25\t$token1\tX25_SET_LINKID={$link_id, $loop_SAP, 1}" >> $TMPF2
                        fi
                        loopcount=2
                        until
                                test $loopcount -ge $NO_LLC2
                        do
				echo "x25\t$token1\tX25_SET_LINKID={$link_id}" >> $TMPF2
                                loopcount=`expr $loopcount + 1`
                        done
                        echo >> $TMPF2
                fi
                  

#
# Now check if we have any pvcs configured. Parse program will not
# permit any PVCs to be specified on a LAN
#
		if [ $lpc -ne 0 -a $hpc -ne 0 ]
		then
			>$TMPF10
		fi

		cat << !! >> $TMPF1
$token3	dc	/dev/$token3
x25	dc	/dev/x25
!!

	done	

#
# The following lines deal with the setting up of re-configuration
# of X25's PVC's. This line will only be added if we have one or more X.25
# line configured ie. TMPF9 exists and we have PVCs configured.
#
	if [ -f $TMPF9 -a -f $TMPF10 ]
	then
		cat << !! >> $TMPF2
x25	-	SHELL="$pvc_map -P"

!!
	fi

fi

################## Spider TCP AND Spider X.25 ##################


if test "$SX25" = "1" -a "$SUN_TCP" = "1"
then

	if test -f $TMPF8
	then
		rm $TMPF8
	fi
	
	# Produce appropriate "ip	ixe" lines
	ipline=0 
	ixeline=0
	
	# Calculate no of IP lines.
	echo "0" > $TMPF8
	$X25LIBFILES/selects $LIPFILE |	
	while read prefix numb line_no IPaddr mask forwb \
		dev_s1 dev_s2 dev_s3 kalive mtu
	do
		# cope with unconfigured lines
		if test "$prefix" = "?" -o "$IPaddr" = "?" \
			-o "$mask" = "?" -o "$forwb" = "?"
		then
			continue
		fi
		nips=`cat < $TMPF8`
		nips=`expr $nips + 1`
		echo $nips > $TMPF8
	done
	nips=`cat < $TMPF8`
	nips=`expr $nips - 1`
	rm -f $TMPF8

	$X25LIBFILES/selects $LIPFILE |	
	while read prefix numb line_no IPaddr mask forwb \
		dev_s1 dev_s2 dev_s3 kalive mtu
	do
		# cope with unconfigured lines
		if test "$prefix" = "?" -o "$IPaddr" = "?" \
			-o "$mask" = "?" -o "$forwb" = "?"
		then
			continue	
		fi

		rest=
		if [ X$kalive != X ]
		then
			if [ "X$mtu" != X ]
			then
				rest=" mtu $mtu"
			fi
		fi

 		if test "$prefix" = "ixe" -a -f $TMPF9
		then
			if [ "$dev_s3" = "PTP" ]
			then
ifline="ip$ipline	ixp	SHELL=\"ifconfig ixe$ixeline $IPaddr $mask up$rest\""
 
			else
ifline="ip$ipline	ixe	SHELL=\"ifconfig ixe$ixeline $IPaddr netmask $mask up$rest\""
 
			fi
			ixeline=`expr $ixeline + 1`
			if [ "$ipline" -eq "$nips" ]
			then
				cat << !! >> $TMPF12
				
$ifline \\
		SHELL="$ixe_map -Z" \\
		SHELL="$ixe_map -P" \\
		SHELL="/usr/sbin/ndd -set /dev/ip ip_forwarding 1 ; exit 0" \\
		SHELL="$ixe_tune"
		
!!
			else
				cat << !! >> $TMPF12
				
$ifline			 
!!
			fi

			if [ -f $TMPF11 ]
			then
				cat $TMPF11 >> $TMPF12
			fi
			mv $TMPF12 $TMPF11
			#  Create "ip1  m  ip" lines here while we
			#  have track of line numbers
			cat << !! >> $TMPF1
ip$ipline	m	ip
!!
		ipline=`expr $ipline + 1`
		
		# existence of $TMPF8 shows ip-ixe links exist.
		# (I would have used a variable, but it loses its value
		# when this spawned shell is finished.)
		> $TMPF8

# N.B. x25 address(dev_s1) and config file(dev_s2) in next section
# are stored in a file to be used by ixetune(1M) to configure these lines.

# Test $dev_s2 exists in the config directory,
# read its contents into one line of ixetuneconf(7).
			ixeconentries=""
			if test -f $X25CONFIGFILES/config/${dev_s2}.ixe
			then
				ixefile=$X25CONFIGFILES/config/${dev_s2}.ixe
			else
				echo "IXE configuration file:${dev_s2}.ixe does not exist." >&2
				tidy_up	
				exit 1
			fi
			if [ "$dev_s3" = "PTP" ]
			then
				rest=/$mask
			else
				# Use a TAB, to maintain nice format in ixetconf
				rest="	"
			fi  
			set -- `$X25LIBFILES/selects $ixefile`
			cat << !! >> $TMPF4
${IPaddr}$rest	$dev_s1	$*
!!
		

		fi
	done	
	if [ -f "$TMPF11" ]
	then
		cat $TMPF11 >> $TMPF2
	fi
	if test -f $TMPF8 -a -f $TMPF9
	then
	# Include info on IXE to X.25 links from /etc/system if there
	# is any. Normally there will not be, and the default of
	# 16 will be used if necessary.
		max_ixcons=16
		eval `fgrep 'ixe:max_ixcons' /etc/system | sed -e 's/set ixe://'`

# SSP fix for esc #507268, bug #4008609, SD, 11 Oct 96
# max value in kernel is 128
# SSP fix, esc 508197, bug 4024892; raise max from 128 to 512
		if [ $max_ixcons -gt 512 ] || [ $max_ixcons -lt 0 ]
		then
			echo "max_ixcons value of $max_ixcons is out of range, 512 assumed"
			max_ixcons=512
		fi
# end SSP fix for esc #507268, bug #4008609, SD, 11 Oct 96
# end SSP fix, esc 508197, bug 4024892; raise max from 128 to 256 

	# Determine if there are any point-to-multipoint IP nets configured
	# (netmask only valid for this type of net).  If there aren't any
	# pt-to-multipoint nets, then the "ixe x25" entries should all
	# be "ixp x25".
		IXE_DEF=`fgrep 'netmask' $TMPF2`
		loopcount=0
		until
			test $loopcount -eq $max_ixcons
		do
			if [ "$IXE_DEF" ]
			then
				echo "ixe	x25" >> $TMPF2	
			else
				echo "ixp	x25" >> $TMPF2	
			fi
			cat << !! >> $TMPF1
ixe	dc	/dev/ixe
x25	dc	/dev/x25
!!
			cat << !! >> $TMPF1
ixp	dc	/dev/ixp
!!
			loopcount=`expr $loopcount + 1`
		done
				
# SSP fix for esc #507268, bug #4008609, SD, 11 Oct 96
# WRONG: this has been badly report from 8.0.2 to 9.0
#  not necessary any more
#		IXP_DEF=`fgrep ixp $TMPF2`
#		if [ "$IXP_DEF" ]
#		then
#			echo "ixp	x25" >> $TMPF2
#		else
#			echo "ixe	x25" >> $TMPF2
#		fi
# end SSP fix for esc #507268, bug #4008609, SD, 11 Oct 96

	fi

fi

##################### Tidy up and exit ##########################

# Remove non-unique entries from "modules" sections
cat $TMPF1|sort|uniq > $TMPF3
cat $TMPF7|sort|uniq > $TMPF6

# Add modules, streams sections seperator
cat << !! >> $TMPF3

%%

!!

# Add streams section to module sections
cat $TMPF2 >> $TMPF3

# Add IXE registration calls and echos to streams section
cat $TMPF6 >> $TMPF3




# If in check mode, then don't create new netconf(7) and new
# ixetuneconf(7) or startslipfile.
if test "$1" = "-c"
then
	:
else
	# Everything looks good, save the old netconf (suffix ".old")
	# and save created file in $NETDFILE
	if test -f $NETDFILE
	then
		mv $NETDFILE ${NETDFILE}.old
	fi
	mv $TMPF3 $NETDFILE


	if test "$SX25" = "1" -a "$SUN_TCP" = "1"
	then
		# ixetuneconf(7) file looks good, move created file into proper place
		if test -s $IXETFILE
		then
			# Save old ixetuneconf (suffix ".old")
			mv $IXETFILE ${IXETFILE}.old
		fi

		echo "Creating $IXETFILE..."
		mv $TMPF4 $IXETFILE
	fi

	# Create check file to indicate that good $NETDFILE has been created
	touch $NCHECKFILE
fi


# Remove temporary files
tidy_up

exit 0
