#help#  Selecting this menu item will configure the



#ident "@(#)pnp    1/25/95"

PRONAME="Advanced NE2000 Compatible Card"
BRDNAME="Advanced NE2000 Compatible Card"
BASENAME="pnp"
OS="SCO"

BROADCAST=
NETMASK=
IP=
NEWCONFIG=0
NEWKERNEL=0
MAXBOARDS=4

# Configurable resources

# SCOsetupdevice DRVNAME IP NETMASK BROADCAST
SCOsetupdevice() {

        DRVNAME=$1
        IP=$2
        NETMASK=$3
        BROADCAST=$4
        #
        # Modify streams configuration file
        #
        echo "Modifying /etc/strcf ..."
        cp /etc/strcf /etc/strcf.old
        grep -v "cenetb ip /dev/${DRVNAME} " /etc/strcf.old >/etc/strcf
        ed -s /etc/strcf << PROST > /dev/null
/       loopback/
i
        cenetb ip /dev/${DRVNAME} ${DRVNAME}
.
w
q
PROST



        #
        # Modify TCP/IP startup file /etc/tcp
        #
        echo "Modifying /etc/tcp ..."
        cp /etc/tcp /etc/tcp.old
        grep -v "ifconfig ${DRVNAME} " /etc/tcp.old >/etc/tcp
        ed -s /etc/tcp << PROST > /dev/null
/ifconfig/
a
        ifconfig ${DRVNAME} ${IP} -trailers netmask ${NETMASK} broadcast ${BROADCAST}
.
w
q
PROST

}

# ISCsetupdevice DRVNAME IP NETMASK BROADCAST
ISCsetupdevice() {

        DRVNAME=$1
        IP=$2
        NETMASK=$3
        BROADCAST=$4
        #if [ "${DRVNAME}" != "${BASENAME}" ]
        #then
                #echo ""
                #echo "Host-Based TCP/IP V1.X for ISC Unix does"
                #echo "currently only support one network board."
                #echo "The device /dev/${DRVNAME} is, however, func-"
                #echo "tional.          Press Enter to continue."
                #read dummy
                #return
        #fi

        #
        #  change /etc/netd.cf to use ${DRVNAME}
        #  set the hostname in /etc/netd.cf
        #
        if [ -f /etc/netd.cf ]
        then
                echo "Saving old /etc/netd.cf as /etc/netd.cf.old"
                mv /etc/netd.cf /etc/netd.cf.old
        fi
        echo "Customizing network configuration file /etc/netd.cf ..."
        LOCNAME=`uname -n`
        cp /usr/admin/menu/packagemgmt/tcpipmgmt/hbtcpmgmt/_netd.cf /etc/netd.cf
        chmod  755 /etc/netd.cf
        ed - /etc/netd.cf > /dev/null <<-EOF
                g/DEV/s//${DRVNAME}/g
                g/local_site_name/s//$LOCNAME/
                w
                q
                EOF

        echo "\nConfiguration of TCP/IP for device ${DRVNAME} is complete."
}


#setupstartup
setupstartup() {

   if [ "${OS}" = "SCO" ]
   then
        # Make links to rc directories
        echo "Setting links for automatic TCP/IP startup ..."
        rm -f /etc/rc2.d/S85tcp /etc/rc0.d/K85tcp
        ln /etc/tcp /etc/rc2.d/S85tcp
        ln /etc/tcp /etc/rc0.d/K85tcp
   else
        echo "Raising default runlevel to 3 ...\c"
        chmod  644 /etc/inittab /etc/conf/cf.d/init.base
        ed - /etc/inittab > /dev/null <<-EOF
                1,$ s/is:2:initdefault/is:3:initdefault/g
                w
                q
                EOF
        ed - /etc/conf/cf.d/init.base > /dev/null <<-EOF
                1,$ s/is:2:initdefault/is:3:initdefault/g
                w
                q
                EOF
        chmod  444 /etc/inittab
        chmod  644 /etc/conf/cf.d/init.base
        echo " Done."
   fi

}
#=====================================================================


# addhex(a,b) : add two hexadecimal numbers using awk.
# a and b are ascii strings of digits and the letters a-f
#
# Taken from TCPRT, but works now (try addhex(1234 1234) with
# the one from the TCPRT...)

addhex() {
echo "$1 $2" | nawk ' {
        printf("%x", atoh($1) + atoh($2));
    }
    function atoh(number) {
        i = 0
        j = length(number)
        result = 0
        while ( i < j ) {
            v = index("123456789abcdef", substr(number,j-i,1)) * (16 ^ i)
            result = result + int(v + .1)
            i = i + 1
        }
        return result
    }'
}


# Check whether elem is contained in list
# Return elem on success, "" else
# Format : checklist elem list default

checklist() {
        if [ "$1" = "" ] ; then
                echo $3 ; return 0
        fi
        for opt in $2
        do
                if [ "$opt" = "$1" ] ; then echo $1 ; return 0 ; fi
        done
        return 1
}


# Choose among positions
# Returns choice in variable CHOICE
# Format: choose what list default

choose() {
while [ 1 ]
do
        echo "Valid choices for the $1 are :"
        echo "  $2"

        default=$3
        if [ "${default}" = "" -o "${default}" = "0" ] ; then
                echo "Enter $1 : \c"
                default=""
        else
                echo "Enter $1 [${default}] : \c"
        fi
        read answer
        answer2=`checklist "${answer}" "$2" ${default}`
        case $answer2 in
                "")     if [ "${answer}" = "" ] ; then
                        echo "No default - you must enter a value.\n"
                        else
                        echo "\n${answer} is not a valid choice for the $1.\n"
                        fi
                        ;;
                *)      CHOICE=${answer2}
                        NEWCONFIG=1
                        break
                        ;;
        esac

        if [ "${default}" != "${CHOICE}" ] ; then
                NEWCONFIG=1
        fi
done
}

# Get ordinal number
getordinal() {
        case "$1" in
                0)      if [ "${MAXBOARDS}" = "1" ]
                        then
                                ORDINAL=""
                        else
                                ORDINAL="first "
                        fi
                        ;;
                1)      ORDINAL="second "
                        ;;
                2)      ORDINAL="third "
                        ;;
                3)      ORDINAL="fourth "
                        ;;
                *)      NUM=`expr $1 + 1`
                        ORDINAL="${NUM}th "
                        ;;
        esac

        echo "${ORDINAL}"
}



# Prompt for yes or no answer with message passed as argument -
# returns non-zero for no
getyn() {
        while   echo "\n$* (y/n/q) \c">&2
        do      read yn rest
                case $yn in
                        [yY]) return 0                          ;;
                        [nN]|[qQ]) return 1                     ;;
                *)      echo "Please answer y,n or q" >&2       ;;
                esac
        done
}



# Disable board
disableboard() {

        # Read current parameters, write them back with cfg=N
        read MOD CFG MINS PRI INF IRQVAL IS IE SHMEMS SHMEME \
                                </etc/conf/sdevice.d/${1}
        echo $MOD N $MINS $PRI $INF $IRQVAL $IS $IE $SHMEMS $SHMEME \
                                >/etc/conf/sdevice.d/${1}

}



#
# Complete configuration of all parameters in the System file
#
hwconfig() {

tput clear
ORDINAL=`getordinal "$1"`

clear
echo "Configuration of Hardware Parameters for the ${ORDINAL}\c"
echo "${PRONAME} board.\n\n"

#if [ "$1" = "0" ]
#then
#       DRVNAME="${BASENAME}"
#else
        DRVNAME="${BASENAME}$1"
#fi

if [ ! -f /etc/conf/sdevice.d/${DRVNAME} ]
then
        echo "\n\n"
        echo "The driver ${BRDNAME} for the ${ORDINAL}${BRDNAME} adapter"
        echo "is not installed. Please install the ${BRDNAME} driver from"
        echo "your ${PRONAME} driver diskette using installpkg and then"
        echo "run mkdev ${DRVNAME} again.\n\n"
        exit
fi

cp /etc/conf/sdevice.d/${DRVNAME} /tmp/${DRVNAME}.old

# Read current parameter
read MOD CFG MINS PRI INF IRQVAL IS IE SHMEMS SHMEME \
                                </etc/conf/sdevice.d/${DRVNAME}


# Check for i/o address range
if [ "${IOAVALS}" != "" ]
then
        # Edit io address
        choose "I/O Address" "${IOAVALS}" $IS
        IS=$CHOICE
        IE=${IS}
        #IE=`addhex ${IS} ${IOAWIN}`
        echo "\nSelected : ${IS} - ${IE}\n\n"
fi


# Check for interrupt options
if [ "${IRQVALS}" != "" ]
then
        # Edit interrupt channel
   #    if [ "${IRQVAL}" = "9" ] ; then
   #            IRQVAL=2
   #    fi
        choose "Interrupt Channel" "${IRQVALS}" ${IRQVAL}
        IRQVAL=${CHOICE}
        echo "\nSelected : ${IRQVAL}\c"
        if [ "${IRQVAL}" = "2" ] ; then
                IRQVAL=9
                echo " (this is translated automatically to IRQ 9)\c"
        fi
        echo "\n\n"
fi


# Update config, set flag to Y
echo "${DRVNAME}\tY\t4\t5\t0\t0\t0\t0\t0\t0" \
                                >/etc/conf/sdevice.d/${DRVNAME}

echo "Configuration of Hardware Parameters for the ${ORDINAL}${BRDNAME} board"
echo "driver ${DRVNAME} is complete.\n\n"
}



# Relink the kernel
relink() {
        if [ "${OS}" = "SCO" ]
        then
        #       echo "Executing /etc/conf/bin/idbuild ..."
                /etc/conf/bin/idbuild -y
        else
                /etc/kconfig -r /
        fi
        NEWKERNEL=1
}


# Install new kernel
reboot() {
        echo "\n"
        echo "In order to install the newly built kernel, the"
        echo "system must be shut down and rebooted.\n"
        if getyn "Do you want to reboot now ?"
        then
                # Standard
                cd /
                shutdown -g0
                echo "Shut down the system to reboot with the \c"
                echo "new kernel."
        fi
}

getbroadcast() {
        while   [ 1 ]
        do
                echo "Enter broadcast address [${BROADCAST}] : \c"
                read dummy
                case "${dummy}" in
                "")     break
                        ;;
                [0-9]*) BROADCAST=$dummy
                        break
                        ;;
                *)      echo "Address must be in IP format, e.g. ${BROADCAST}"
                        ;;
                esac
        done
}


#
# get netmask for interface
#
getnetmask() {
        while [ 1 ]
        do
                echo "Enter netmask [${NETMASK}] : \c"
                read dummy
                case "${dummy}" in
                [0-9]*) NETMASK=$dummy
                        break;
                        ;;
                "")     break
                        ;;
                *)      echo "Netmask must be in IP format, e.g. 0.0.255.255"
                        ;;
                esac
        done
}



# get ip address
#
getipaddr() {
    if [ "${IP}" = "" ]
    then
        while   [ 1 ]
        do      echo "Enter IP address : \c"
                read IP
                case "${IP}" in
                [0-9]*) break
                        ;;
                *)      echo "Address must be in IP format, e.g. 200.0.0.1"
                        ;;
                esac
        done
    else
        while   [ 1 ]
        do      echo "Enter IP address [${IP}] : \c"
                read ip
                case "${ip}" in
                [0-9]*) IP=${ip}
                        break
                        ;;
                "")     break;
                        ;;
                *)      echo "Address must be in IP format, e.g. ${IP}"
                        ;;
                esac
        done
    fi
}



make_addresses() {
        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/'`
    NETMASK=255.0.0.0
        elif expr $firstbyte \< 192 > /dev/null ; then
    BROADCAST=`/bin/echo ${IP} | sed -e 's/\(.*\..*\)\..*\..*$/\1.255.255/'`
    NETMASK=255.255.0.0
        else
    BROADCAST=`/bin/echo ${IP} | sed -e 's/\(.*\..*\..*\)\..*$/\1.255/'`
    NETMASK=255.255.255.0
        fi
}

installipcfg() {

        echo "\nInstalling ${DRVNAME} device for TCP/IP :"

        # Make sure that hosts file exists
        if [ ! -f /etc/hosts ] ; then
                echo "Creating host file /etc/hosts ..."
                echo "127.0.0.1\tlocalhost\tlocal\tloopback" > /etc/hosts
                chmod 644 /etc/hosts
                chgrp bin /etc/hosts
                chown bin /etc/hosts
        fi

        # Make sure the IP address we got is in the hosts file
        grep $IP /etc/hosts > /dev/null 2>&1 || {
                echo "Updating hostfile with IP number ${IP} ..."
                set `uname -n`
                echo "${IP}\t$1" >> /etc/hosts
                }


        # Setup device operation
        if [ "${OS}" = "SCO" ]
        then
             SCOsetupdevice $DRVNAME $IP $NETMASK $BROADCAST
        else
             ISCsetupdevice $DRVNAME $IP $NETMASK $BROADCAST
        fi

}

# TCP/IP configuration for board $1
dotcpipcfg() {

        BRDNUM=$1
#       if [ "${BRDNUM}" = "0" ]
#       then
#               DRVNAME=${BASENAME}
#       else
                DRVNAME="${BASENAME}${BRDNUM}"
#       fi

        ORDINAL=`getordinal "${BRDNUM}"`

        # Preset IP address if empty
        if [ -f /usr/options/cb.name ]
        then
                :
        else
                # SCO
                set `grep "ifconfig ${DRVNAME} " /etc/tcp` >/dev/null
                # /etc/tcp | read dummy dummy IP
                IP=$3
        fi

        while [ 1 ]
        do
        #
        # Get required information
        #

        clear
        echo "Please specify the Internet parameters for the \c"
        echo "${ORDINAL}${BRDNAME}"
        echo "driver ${DRVNAME} .\n"

        getipaddr
        # if [ "${BROADCAST}" = "" ] ; then make_addresses ; fi
        make_addresses
        getbroadcast
        getnetmask

        echo "\n\n"
        echo "The following parameters have been specified"
        echo "for the ${ORDINAL}${BRDNAME} board driver ${DRVNAME} :\n"
        echo "\tIP address        : ${IP}"
        echo "\tBroadcast address : ${BROADCAST}"
        echo "\tNetmask           : ${NETMASK}"
        echo "\n"

        echo "Do you want to (I)nstall, (R)evise or (S)kip the"
        echo "${DRVNAME} driver IP configuration ? (i,r,s) \c"
                while [ 1 ]
                do
                        read answer
                        case $answer in
                                [iI]*)
                                        installipcfg
                                        break
                                        ;;
                                [rRsS]*)
                                        break
                                        ;;
                                *)      echo "\nPlease answer i,r or s : \c"
                                        ;;
                        esac
                done

                case $answer in
                        [iIsS]*)
                                break
                                ;;
                esac
        done

}


# Installation routine
if [ -f /usr/options/cb.name ]
then
        OS="ISC"
else
        OS="SCO"
fi

clear
while   echo "Do you wish to install or delete the ${BRDNAME} driver (i/d/q) \c"
do      read ANSWER
        case $ANSWER in
        I|i)
                break
                ;;
        D|d)
#               disableboard "${BASENAME}"
                for board in 0 1 2 3 ; do
                        disableboard "${BASENAME}${board}"
                done
                cd /
            if [ "${OS}" = "SCO" ]
            then
                grep -v ${BASENAME} /etc/tcp > /tmp/tcp && cp /tmp/tcp /etc/tcp
                grep -v ${BASENAME} /etc/strcf > /tmp/strcf && cp /tmp/strcf /etc/strcf
                rm -f /tmp/tcp /tmp/strcf
            else
                grep -v ${BASENAME} /etc/netd.cf > /tmp/netd.cf && cp /tmp/netd.cf /etc/netd.cf
                grep -v ${BASENAME} /etc/conf/kconfig.d/description > /tmp/description
                cp /tmp/description /etc/conf/kconfig.d/description
                rm -f /tmp/netd.cf  /tmp/description
            fi
                # Relink the kernel with the new driver information
                if getyn "\nDo you wish to relink the kernel now?"
                then
                  relink
                else
                  echo "\nChanges will not take effect until the kernel is relinked." >&2
                fi
                exit 0
                ;;
        Q|q)
                exit 0
                ;;
        *)      echo "Please answer 'i', 'd' or 'q'."
                ;;
        esac
done

# Disable boards 1,2,3
 for board in 1 2 3 ; do
         disableboard "${BASENAME}${board}"
 done

# Hardware configuration
index=0
while expr ${index} \< ${MAXBOARDS} >/dev/null
do
        hwconfig $index
        index=`expr $index + 1`

        if [ "${index}" = "${MAXBOARDS}" ] ; then
                break
        fi

        if getyn "Do you want to install another ${BRDNAME} board ?"
        then
                :
        else
                break
        fi
done

numboards=$index

# Special warning...
if [ "${numboards}" != "1" ]
then
        tput clear
        echo "\n\n"
        echo "You have installed more than one ${BRDNAME} board into"
        echo "your system. In order to have TCP/IP use the proper"
        echo "network interface for the desired sub-networks, please"
        echo "make sure that you don't mix up your numbers...\n"
        echo "Things get confusing - please read the documentation"
        echo "that came with your TCP/IP runtime!\n"
fi

# Get nodename, read IP address from hosts file (if any)
system=`uname`
if [ "$system" != "" -a -f /etc/hosts ]
then
        # Get our IP number
        set `grep $system /etc/hosts` >/dev/null 2>&1
        IP=$1
else
        IP=""
fi

# Configure board(s)
index=0
while expr $index \< $numboards >/dev/null
do
        dotcpipcfg $index
        index=`expr $index + 1`
        IP=""
done


setupstartup


echo "\n\n\n\n"
echo "TCP/IP configuration for the ${BRDNAME} driver is complete.\n"

if [ ${NEWCONFIG} ]
then
        echo "In order to update the changed configuration, the"
        echo "UNIX kernel must be relinked and installed.\n"
        if getyn "Do you want to relink the kernel now ?"
        then
                relink
                reboot
        fi
fi

echo "Relink the kernel if necessary and restart the system in order"
echo "to use TCP/IP."
exit 0

