#!/bin/sh
#
# Interactively configure the PCMCIA network cards for Debian
#
# Brian Mays <brian@debian.org>

umask 022

readyesno()
{
    eval default=\$$1
    while :
    do 
    	echo -n "$2 [$default] "
    	read response
	case $response in
	[Yy]*)	eval "$1"'="y"'; return 0 ;;
	[Nn]*)	eval "$1"'="n"'; return 1 ;;
    	"")	test "$default" = y && return 0 || return 1 ;;
    	esac
    done
}

readstuff()
{
    eval default=\$$1
    echo "$2"
    echo -n "("
    test "$default" && echo -n "the default is '$default'; "
    echo "type 'none' to leave blank)"
    read response
    case "`echo $response | tr A-Z a-z`" in
    none)	eval "$1"="" ;;
    "")		;;
    *)		eval "$1"="$response" ;;
    esac
}

if [ "root" != "`whoami`" ]
then 
   echo "Sorry, only root can run this script.  Exiting."
   exit 1
fi

# Default values
BOOTP="n"
DHCPC="n"
IPADDR=""
NETMASK="255.255.255.0"
NETWORK="1.2.0.0"
BROADCAST="1.2.255.255"
GATEWAY="1.2.0.1"
DOMAIN=""
SEARCH=""
DNS_1=""
DNS_2=""
DNS_3=""
MOUNTS=""
IPX_FRAME=""
IPX_NETNUM=""

cat <<END
This program will create a basic /etc/pcmcia/network.opts file, the
pcmcia-cs package's network adapter configuration file, based on choices
that you make.

A sample network.opts file is supplied in pcmcia-cs; you can edit this
file to match your local network setup.  Refer to the PCMCIA-HOWTO
(usually in /usr/doc/HOWTO/PCMCIA-HOWTO.gz) for a detailed description
of this file's contents.

Before continuing with this program, ensure that you know your host's
network configuration.

END

cont="y"
readyesno cont "Do you want to continue?" || exit 0

echo
echo "Choose a method for obtaining the host's IP address and routing"
echo "information"
while :
do 
    echo
    echo "1) Use the BOOTP protocol"
    echo "2) Use the DHCPC protocol"
    echo "3) Use netenv (from the netenv package)"
    echo "4) Specify the IP address now (default)"
    read response
    case $response in
    1)	ipmethod=bootp; break ;;
    2)	ipmethod=dhcpc; break ;;
    3)	ipmethod=netenv; break ;;
    4|"")
	ipmethod=ipaddr; break ;;
    esac
    echo "Invalid response"
done

case "$ipmethod" in
netenv) ;;
bootp)
    BOOTP="y" ;;
dhcpc)
    DHCPC="y" ;;
ipaddr)
    while :
    do
	readstuff IPADDR "Enter the IP address for this interface:"
	if [ ! "$IPADDR" -o `expr "$IPADDR" : \
	    '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*'` \
	    -ne 0 ]; then break; fi
	echo "Invalid IP address"
    done
    if [ "$IPADDR" ]
    then
	IP1=`expr "$IPADDR" : \
	    '\([0-9][0-9]*\)\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*'`
	IP2=`expr "$IPADDR" : \
	    '[0-9][0-9]*\.\([0-9][0-9]*\)\.[0-9][0-9]*\.[0-9][0-9]*'`
	IP3=`expr "$IPADDR" : \
	    '[0-9][0-9]*\.[0-9][0-9]*\.\([0-9][0-9]*\)\.[0-9][0-9]*'`
	IP4=`expr "$IPADDR" : \
	    '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.\([0-9][0-9]*\)'`
	if [ "$IP1" -lt 128 ]
	then
#	Network class A
	    NETMASK="255.0.0.0"
	    NETWORK="$IP1.0.0.0"
	    BROADCAST="$IP1.255.255.255"
	    GATEWAY="$IP1.0.0.1"
	elif [ "$IP1" -lt 192 ]
	then
#	Network class B
	    NETMASK="255.255.0.0"
	    NETWORK="$IP1.$IP2.0.0"
	    BROADCAST="$IP1.$IP2.255.255"
	    GATEWAY="$IP1.$IP2.0.1"
	elif [ "$IP1" -lt 224 ]
	then
#	Network class C
	    NETMASK="255.255.255.0"
	    NETWORK="$IP1.$IP2.$IP3.0"
	    BROADCAST="$IP1.$IP2.$IP3.255"
	    GATEWAY="$IP1.$IP2.$IP3.1"
	else
#	Multicast
	    NETMASK="240.0.0.0"
	    NETWORK="$IP1.0.0.0"
	    BROADCAST="$IP1.255.255.255"
	    GATEWAY="$IP1.0.0.1"
	fi
    fi
    readstuff NETMASK "Enter the netmask:"
    cat <<END

Note: the "network address" here is NOT the same as the IP address.
See the Networking HOWTO.  In short, the network address is the IP
address masked by the netmask.

END
    readstuff NETWORK "Enter the network address:"
    readstuff BROADCAST "Enter the broadcast address:"
    readstuff GATEWAY "Enter the gateway address:"
    echo
    echo "Which do you wish to specify? a local domain name or a search"
    echo "list for host-name lookup"
    while :
    do 
	echo
	echo "1) Specify local domain name"
	echo "2) Specify a search list"
	read response
	case $response in
	1)  readstuff DOMAIN "Enter the local domain name:"; break ;;
	2)  readstuff SEARCH "Enter the search list:"; break ;;
	esac
	echo "Invalid response"
    done
    cat <<END

You may now specify up to three host names or IP addresses for
nameservers for this interface, to be added to /etc/resolv.conf.
The nameservers defined here complement the nameservers already defined
in /etc/resolv.conf.

END
    readstuff DNS_1 "The 1st nameserver:"
    test "$DNS_1" &&
	readstuff DNS_2 "The 2nd nameserver:"
    test "$DNS_1" -a "$DNS_2" &&
	readstuff DNS_3 "The 3rd nameserver:"
    cat <<END
To automatically mount and unmount NFS filesystems, first add all these
filesystems to /etc/fstab, but include noauto in the mount options.

WARNING:  It is especially important to use either cardctl or cardinfo
to shut down a network card when NFS mounts are configured this way.
It is not possible to cleanly unmount NFS filesystems if a network card
is simply ejected without warning.

END
    readstuff MOUNTS \
    	"Enter a list of NFS mount points to be mounted for this interface:"
    echo
    echo "The next two parameters are for IPX networks.  They are passed to"
    echo "the ipx_interface command."
    echo 
    readstuff IPX_FRAME \
    	"Enter the frame type (i.e., 802.2):"
    readstuff IPX_NETNUM \
	"Enter the IPX network number:"
    ;;
esac
cat <<END

In addition to the usual network configuration parameters, the
network.opts script can specify extra actions to be taken after
an interface is configured, or before an interface is shut down.
If network.opts defines a shell function called start_fn, it will be
invoked by the network script after the interface is configured, and the
interface name will be passed to the function as its first (and only)
argument.  Similarly, if it is defined, stop_fn will be invoked before
shutting down an interface.  Refer to the PCMCIA-HOWTO for more details.

This program will now write the network.opts file.  Please take care
not to overwrite a previously configured version of this file.

END

cont="y"
readyesno cont "Do you want to write /etc/pcmcia/network.opts?" || exit 0

cat > /etc/pcmcia/network.opts <<EOF
# Network adapter configuration
#
# ### This file was generated by the pcnetconfig script. ###
#
# The address format is "scheme,socket,instance,hwaddr".
#
# Note: the "network address" here is NOT the same as the IP address.
# See the Networking HOWTO.  In short, the network address is the IP
# address masked by the netmask.
#
case "\$ADDRESS" in
*,*,*,*)
    # Transceiver selection, for cards that need it -- see 'man ifport'
    IF_PORT=""
    # Use BOOTP [y/n]
    BOOTP="$BOOTP"
    # Use DHCPC [y/n] (in the dhcpcd package.)
    DHCPC="$DHCPC"
    # IP address
    IPADDR="$IPADDR"
    # Netmask
    NETMASK="$NETMASK"
    # Network address
    NETWORK="$NETWORK"
    # Broadcast address
    BROADCAST="$BROADCAST"
    # Gateway address
    GATEWAY="$GATEWAY"
    # Specify either DOMAIN or SEARCH, not both
    # Local domain name
    DOMAIN="$DOMAIN"
    # Search list for host lookup
    SEARCH="$SEARCH"
    # The nameserver IP addresses specified here complement the
    # nameservers already defined in /etc/resolv.conf.  These nameservers
    # will be added to /etc/resolv.conf automatically when the PCMCIA
    # network connection is established and removed from this file when
    # the connection is broken.
    # Nameserver #1
    DNS_1="$DNS_1"
    # Nameserver #2
    DNS_2="$DNS_2"
    # Nameserver #3
    DNS_3="$DNS_3"
    # NFS mounts, should be listed in /etc/fstab
    MOUNTS="$MOUNTS"
    # For IPX interfaces, the frame type (i.e., 802.2)
    IPX_FRAME="$IPX_FRAME"
    # For IPX interfaces, the network number
    IPX_NETNUM="$IPX_NETNUM"
    # Extra stuff to do after setting up the interface
    start_fn () { return; }
    # Extra stuff to do before shutting down the interface
    stop_fn () { return; }
    ;;
esac
EOF

if [ "$ipmethod" = netenv ]
then
    cat >> /etc/pcmcia/network.opts <<EOF

# For those, who want to use the netenv-Package, the file containing
# the network-setup has to be sourced.  This is not a problem if there
# is no such file.
# Note: in older versions of the netenv package, this file used to be
# located in /tmp.  This was a security risk, however, that has been
# fixed in more recent versions.
if [ -r /etc/netenv/netenv ]; then . /etc/netenv/netenv; fi
EOF
fi

exit 0
