#!/bin/sh
#
# Interactively configure the PCMCIA network cards for Debian
#
# Copyright 1998-1999 Brian Mays <brian@debian.org>.
# This script is released under the GPL.

umask 022

conffile=/etc/pcmcia/network.opts
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
    INFO="Sample private network setup"
    IF_PORT=""
    BOOTP="n"
    DHCP="n"
    DHCP_HOSTNAME=""
    IPADDR=""
    NETMASK="255.255.255.0"
    NETWORK="10.0.1.0"
    BROADCAST="10.0.1.255"
    GATEWAY="10.0.1.1"
    DOMAIN=""
    SEARCH=""
    DNS_1=""
    DNS_2=""
    DNS_3=""
    MOUNTS=""
    MTU=""
    IPX_FRAME=""
    IPX_NETNUM=""
    IPMASQ="n"
    NO_CHECK=n
    NO_FUSER=n

cat <<END
This program will create a basic $conffile 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 DHCP 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)
    DHCP="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"
	elif [ "$IP1" -lt 192 ]; then
#	Network class B
	    NETMASK="255.255.0.0"
	elif [ "$IP1" -lt 224 ]; then
#	Network class C
	    NETMASK="255.255.255.0"
	else
#	Multicast
	    NETMASK="240.0.0.0"
	fi
    fi
    readstuff NETMASK "Enter the netmask:"
    if [ "$IPADDR" ]; then
	NMIP2=`expr "$NETMASK" : \
	    '[0-9][0-9]*\.\([0-9][0-9]*\)\.[0-9][0-9]*\.[0-9][0-9]*'`
	NMIP3=`expr "$NETMASK" : \
	    '[0-9][0-9]*\.[0-9][0-9]*\.\([0-9][0-9]*\)\.[0-9][0-9]*'`
	NMIP4=`expr "$NETMASK" : \
	    '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.\([0-9][0-9]*\)'`
	if [ "$NMIP2" -eq 0 ]; then
	    NETWORK="$IP1.0.0.0"
	    BROADCAST="$IP1.255.255.255"
	    GATEWAY="$IP1.0.0.1"
	elif [ "$NMIP3" -eq 0 ]; then
	    NETWORK="$IP1.$IP2.0.0"
	    BROADCAST="$IP1.$IP2.255.255"
	    GATEWAY="$IP1.$IP2.0.1"
	else
	    NETWORK="$IP1.$IP2.$IP3.0"
	    BROADCAST="$IP1.$IP2.$IP3.255"
	    GATEWAY="$IP1.$IP2.$IP3.1"
	fi
    fi
    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:"
    readstuff DOMAIN "Enter the local domain name:"
    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 $conffile?" || exit 0

cat > $conffile <<EOF
# ** This file was automatically generated by the pcnetconfig script **
#
EOF

cat > $conffile <<EOF
# NOTE: This file was generated automatically by the pcnetconfig script.
# Network adapter configuration
#
# 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
*,*,*,*)
    INFO="$INFO"
    # Transceiver selection, for some cards -- see 'man ifport'
    IF_PORT="$IF_PORT"
    # Use BOOTP (via /sbin/bootpc, or /sbin/pump)? [y/n]
    BOOTP="$BOOTP"
    # Use DHCP (via /sbin/dhcpcd, /sbin/dhclient, or /sbin/pump)? [y/n]
    DHCP="$DHCP"
    # If you need to explicitly specify a hostname for DHCP requests
    DHCP_HOSTNAME="$DHCP_HOSTNAME"
    # Host's IP address, netmask, network address, broadcast address
    IPADDR="$IPADDR"
    NETMASK="$NETMASK"
    NETWORK="$NETWORK"
    BROADCAST="$BROADCAST"
    # Gateway address for static routing
    GATEWAY="$GATEWAY"
    # Things to add to /etc/resolv.conf for this interface
    DOMAIN="$DOMAIN"
    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.
    DNS_1="$DNS_1"
    DNS_2="$DNS_2"
    DNS_3="$DNS_3"
    # NFS mounts, should be listed in /etc/fstab
    MOUNTS="$MOUNTS"
    # If you need to override the interface's MTU...
    MTU="$MTU"
    # For IPX interfaces, the frame type and network number
    IPX_FRAME="$IPX_FRAME"
    IPX_NETNUM="$IPX_NETNUM"
    # Run ipmasq? [y/n]  (see the Debian ipmasq package)
    IPMASQ="$IPMASQ"
    # Extra stuff to do after setting up the interface
    start_fn () { return; }
    # Extra stuff to do before shutting down the interface
    stop_fn () { return; }
    # Card eject policy options
    NO_CHECK="$NO_CHECK"
    NO_FUSER="$NO_FUSER"
    ;;
esac
EOF

if [ "$ipmethod" = netenv ]; then
    cat >> $conffile <<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
