#!/bin/sh
PATH=/bin:/usr/bin:/sbin:/usr/sbin

# Configure Dynamic IP addressing for PPP

###############################################################################
# 
# Helper functions
# 
###############################################################################

getipaddr()
{
	RESP=$1
	shift

	ans=
	while [ X"$ans" = X ] ; do
		echo -n "$@"
		read ans
		case X"$ans" in
		X)		ans=default;;
		Xdefault)	ans=default;;
		X*[^A--/:]*)	echo "Invalid entry: $ans"; ans= ;;
		X*.*.*.*.*)	echo "Invalid entry: $ans"; ans= ;;
		X*[0-9][0-9][0-9][0-9]*)
				echo "Invalid entry: $ans"; ans= ;;
		X*[3-9][0-9][0-9]*)
				echo "Invalid entry: $ans"; ans= ;;
		X*2[6-9][0-9]*)	echo "Invalid entry: $ans"; ans= ;;
		X*25[6-9]*)	echo "Invalid entry: $ans"; ans= ;;
		X[0-9]*.[0-9]*.[0-9]*.[0-9]*)
				;;
		*)      	echo "Invalid entry: $ans"; ans= ;;
		esac
	done
	if [ X"$ans" = Xdefault ] ; then
		ans=
	fi
	eval $RESP=$ans
}

getnumber()
{
	RESP=$1
	shift

	while true ; do
		echo -n "$@"
		read ans
		case X"$ans" in
		X)	return 1 ;;
		X*[!0-9]*)
		    echo Invalid response, must be a number. ;;
		X[0-9]*)
			eval $RESP=$ans
			return 0
			;;
		esac
	done
}

getyesno()
{
        while true ; do
                echo -n $@ "[yes] " ; read ans
                case X"$ans" in
                X | XY* | Xy*)  return 0 ;;
                XN* | Xn*)      return 1 ;;
                *)              ;;
                esac
        done
}

###############################################################################
#
# Check to see if PPP has been configured, if so, how many interfaces
# there are.
#
###############################################################################

echo -n Checking to see if PPP has been configured...
if ifconfig ppp0 > /dev/null 2>&1 ; then
	:
else
	cat <<!


PPP has not been configured into your kernel.  See "PPP Configuration"
under "Dialup IP Services: PPP and Slip" in the BSD/OS release notes.
Follow the instructions for configuring your kernel with PPP.   Once
you have booted a system with PPP configured, rerun config_ppp.
!
	exit 1
fi

NIF=7
while ifconfig ppp$NIF > /dev/null 2>&1 ; do
	NIF=$(expr $NIF + 8)
done
until ifconfig ppp$NIF > /dev/null 2>&1 ; do
	NIF=$(expr $NIF - 1)
done

NIF=$(expr $NIF + 1)
echo " found $NIF interfaces."
echo

cat <<!
config_ppp can patch /bsd to support more (or less interfaces).  If you
would like to change the number of interfaces, please enter the new
number now.  Press <ENTER> without entering a number to not modify the
number of interfaces.

!

while getnumber NNIF "New number of PPP interfaces? [dont change] " ; do
	if getyesno "Modify to use $NNIF interfaces?" ; then
		if bpatch napppif $NNIF ; then
			NIF=$NNIF
			cat <<!

/bsd has now been patched.  You will need to reboot your system for this
change to take place.  (You may finish with config_ppp before rebooting)

!
		else
			cat <<!

/bsd could not be patched.  Make sure you are running the 2.0.1 kernel
or have applied the latest PPP kernel patches.

!
		fi
		echo -n "Press <ENTER> to continue "
		line
		break
	fi
done

###############################################################################
#
# Check to see if we should set up for dynamic IP addressing
#
###############################################################################

cat <<!

PPP supports dynamic IP addressing as well as dynamic interface
assignment.  PPP has been installed to use dynamic interface assignmet
which implies that PPP will automatically choose a free PPP interface
(ppp0, ppp1, etc).

PPP may also be configured to use dynamic IP addressing.  This implies
that the client may be given a different IP address each time they
connect.  It is nearly always better to assign IP addresses (this can
be done with config_dialin), though dynamic IP addressing may be appropriate
in some situations.  Configuring dynamic IP addressing does not imply
that only dynamic IP addressing can be used.  Dynamic IP addressing will
only be used for systems which do not have an assigned IP address.

config_ppp can install one of two methods for dynamic IP addresses.  The
first method is to associate an IP address with each PPP interface, the
second is to associate an IP address with each tty device.

!

TYPE=
while [ X"$TYPE" = X ] ; do
	echo -n "Which method should be used? (port, interface) [interface] "
	read TYPE
	case X"$TYPE" in
	X | XI* | Xi*)	TYPE=interface ;;
	XP* | Xp*)	TYPE=port ;;
	*)		TYPE= ;;
	esac
done

if [ $TYPE = interface ] ; then

###############################################################################

    	cat <<!

There are $NIF interfaces.  You may assign blocks of IP addresses to
blocks of PPP interfaces.  You must ensure that the IP addresses used
are not assigned to any other host.

!

	LIFA=0
	NIFL=$NIF

	getfirstinterface() {
		IFA=
		NIFA=
		while [ X"$IFA" = X ] ; do
			echo -n "First PPP interface of block (- implies done)? [$LIFA] "
			read IFA
			case X"$IFA" in
			X-) return 1;;
			X)  if [ X$LIFA = X- ] ; then
				    return 1;
			    fi
			    IFA=$LIFA
			    ;;
			X*[!0-9]*)
			    echo Invalid response, must be a number. ; IFA= ;;
			X[0-9]*) ;;
		    *)	echo Invalid response, must be a number. ; IFA= ;;
			esac
		done
		return 0
	}

	while getfirstinterface ; do
		while [ X"$NIFA" = X ] ; do
			echo -n "Number of interfaces to assign? [$NIFL] "
			read NIFA
			case X"$NIFA" in
			X)	NIFA=$NIFL;;
			X[0-9]*) ;;
			*)	echo Invalid response. ; NIFA= ;;
			esac
		done

		getipaddr IPA "What is the first IP address? "

		IPN=$(echo $IPA | sed 's/[0-9]*$//')
		IPH=$(echo $IPA | sed s/^$IPN//)

		if [ $NIFA -eq 0 ] ; then
			return 1
		fi

		if [ $NIFL -le $NIFA ] ; then
			NIFL=0
			LIFA=-
		else
			NIFL=$(expr $NIFL - $NIFA)
			LIFA=$(expr $LIFA + $NIFA)
		fi


		while [ $NIFA -gt 0 ] ; do
			echo "Assigning ppp$IFA to $IPN$IPH"
			echo "ppp$IFA	$IPN$IPH" >> /etc/netscripts/addr-map
			NIFA=$(expr $NIFA - 1)
			IFA=$(expr $IFA + 1)
			IPH=$(expr $IPH + 1)
		done
		cat <<!

To change any of these assignments you will need to edit the
/etc/netscripts/addr-map file.

Press <ENTER> to continue.
!
		read FOO
	done
else

###############################################################################

	cat <<!

You must now provide mappings between devices and IP addresses.
You may alter these mappings later by editing the file
/etc/netscripts/addr-map.

!
	gettty() {
		TTY=
		while [ X"$TTY" = X ] ; do
			echo -n "Device to assign (i.e. /dev/tty01) ? [done] "
			read TTY
			case X"$TTY" in
			X | Xdone)	return 1 ;;
			X/dev/tty*)	;;
			*)	echo Invalid response. ; TTY= ;;
			esac
		done
		return 0
	}

	while gettty ; do
		getipaddr IPA "What is the IP address for $TTY? "
		echo "Assigning $TTY to $IPA"
		echo "$TTY	$IPA" >> /etc/netscripts/addr-map
	done
fi

###############################################################################

cat <<!

Configuration complete.  Please use the config_dialin program to configure
individual PPP and/or SLIP accounts.

!
exit 0
