#!/bin/sh
#
#   define_etherlites
##########################################################################
# 
#  Copyright 2000 Digi International (www.digi.com)
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2, or (at your option)
#  any later version.
# 
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
#  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
#  PURPOSE.  See the GNU General Public License for more details.
#   
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
# 
##########################################################################

BOOTPD_BIN=/usr/sbin/bootpd

#
# Ask the user for all the important information about the unit
#
get_unit_info() {

# init all of the variables for this routine
HAVE_MODEL="no"
HAVE_GATEWAY="no"
HAVE_NETMASK="no"
HAVE_ELS_ETHER_ADDR="no"
HAVE_ELS_IP_ADDR="no"
HAVE_ELS_IP_ADDRA="no"
HAVE_ELS_IP_ADDRB="no"
ELS_IP_ADDR=""
ELS_ETHER_ADDR_END="none"
NETMASK_DEFAULT="255.255.255.0"
DRIVER="no"
BOOTP="no"
STARTUP="no"
GETTYS="no"
GENERATE=" "

if [ "$BOOTP_SERVER" = "yes" ]
then
    echo -e "Does this host run the driver for this EtherLite module? (Y/n): \c"
    read DRIVER
        if [ "$DRIVER" = "y" ] || [ "$DRIVER" = "Y" ] || [ "$DRIVER" = "" ]
    then
        DRIVER="yes"
    else 
        DRIVER="no"
    fi
else
    DRIVER="yes"
fi


if [ "$DRIVER" = "yes" ]
then
    
    get_driver_count    

    if [ "$DRIVER_COUNT" -gt 63 ]
    then
        echo 
        echo "The driver can handle a maximum of 63 EtherLite modules per"
        echo "host machine.  Exiting."
        echo
        exit 1
    fi
fi


if [ "$BOOTP_SERVER" = "yes" ]
then
    echo -e  "Is this host the BOOTP server for this EtherLite module? (Y/n): \c"
    read BOOTP
        if [ "$BOOTP" = "y" ] || [ "$BOOTP" = "Y" ] || [ "$BOOTP" = "" ]
    then
        BOOTP="yes"
    else 
        BOOTP="no"
    fi
fi


if [ "$BOOTP" = "yes" ]
then
    echo -e  "Is there a gateway between this module and this host? (N/y): \c"
    read HAVE_GATEWAY
    if [ "$HAVE_GATEWAY" = "y" ] || [ "$HAVE_GATEWAY" = "Y" ]
    then
        HAVE_GATEWAY="yes_need"
    fi
fi
        



if [ "$DRIVER" = "yes" ] || [ "$BOOTP" = "yes" ]
then
    while [ "$HAVE_MODEL" != "yes" ]
    do
        echo
        echo
        echo 
        echo "    1) EtherLite 2 module           (formerly EL-2)"
		echo "    2) EtherLite 2 EIA422 module    (formerly EL-2.2)"
		echo "    3) EtherLite 2 EIA485 module"
		echo "    4) EtherLite 8 module           (formerly EL-8+)"
		echo "    5) EtherLite 16 module          (formerly EL-16)"
		echo "    6) EtherLite 32 module          (formerly EL-32)"
		echo "    7) EtherLite 160 module         (formerly EL-160)"
		echo "    8) EtherLite 162 EIA422 module  (formerly EL-162)"

        echo 
        echo -e "This module is an (1-8): \c"
        read MODEL
        if [ "$MODEL" = "1" ] 
		then
			MODEL=EL-2
			HAVE_MODEL="yes"
		fi
        if [ "$MODEL" = "2" ] 
        then
            MODEL=EL-2.2
            HAVE_MODEL="yes"
        fi
        if [ "$MODEL" = "3" ] 
        then
            MODEL=EL-2-EIA485
            HAVE_MODEL="yes"
        fi
        if [ "$MODEL" = "4" ] 
        then
            MODEL=EL-8+
            HAVE_MODEL="yes"
        fi
        if [ "$MODEL" = "5" ] 
        then
            MODEL=EL-16
            HAVE_MODEL="yes"
        fi
        if [ "$MODEL" = "6" ] 
        then
            MODEL=EL-32
            HAVE_MODEL="yes"
        fi
        if [ "$MODEL" = "7" ] 
        then
            MODEL=EL-160
            HAVE_MODEL="yes"
        fi
        if [ "$MODEL" = "8" ] 
        then
            MODEL=EL-162
            HAVE_MODEL="yes"
        fi
    done
fi


if [ "$BOOTP" = "yes" ]
then
    while [ "$HAVE_ELS_ETHER_ADDR" != "yes" ]
    do
        echo
        echo 
        echo "The Ethernet address of the module is printed on a sticker on"
        echo "the underside of the module near the Ethernet port.  An example"
        echo "Ethernet address is '00:a0:e7:24:63:9e'"
        echo
        echo -e "The Ethernet address of this module is:   00:a0:e7:\c"
        read ELS_ETHER_ADDR_END

        if [ `echo "$ELS_ETHER_ADDR_END" | egrep -v ".*:.*:.*:.*" \
                | egrep -c ".*:.*:.*"` = 1 ]
        then
            ELS_ETHER_ADDR="00:a0:e7:$ELS_ETHER_ADDR_END"
            ETH_TEST=`echo "$ELS_ETHER_ADDR" | awk -F: '{printf("%02s%02s%02s%02s%02s%02s",$1,$2,$3,$4,$5,$6)}'`
            if [ `grep -v "^#" /etc/bootptab | grep -c "ha=$ETH_TEST:"` = 0 ]
            then
                HAVE_ELS_ETHER_ADDR="yes"
            else
                echo
                echo "$ELS_ETHER_ADDR is already assigned in /etc/bootptab"
            fi
        fi
    done
fi



if [ "$DRIVER" = "yes" ] || [ "$BOOTP" = "yes" ]
then
    while [ "$HAVE_ELS_IP_ADDR" != "yes" ]
    do
        echo
        echo 
        echo "Each EtherLite module must be assigned a unique IP address."
        echo "Consult with your network administrator to obtain an IP for"
        echo "this module.  An example IP address is '192.9.200.14'"
        echo
        echo -e "The IP address of this module is: \c"
        read ELS_IP_ADDR
        if [ `echo "$ELS_IP_ADDR" | egrep -v ".*\..*\..*\..*\..*" \
              | egrep -c ".*\..*\..*\..*"` = 1 ]
        then
            if [ "$DRIVER" = "yes" ]
            then
                if [ `grep -c "^els.*\<$ELS_IP_ADDR\>" /etc/els.conf` = 0 ]
                then
                    HAVE_ELS_IP_ADDRA="yes"
                else
                    echo
                    echo
                    echo $ELS_IP_ADDR has already been assigned to a module \
                         in /etc/els.conf
                    HAVE_ELS_IP_ADDRA="no"
                fi
            fi
            if [ "$BOOTP" = "yes" ]
            then
                if [ `grep -c "^[^#].*:ip=\<$ELS_IP_ADDR\>" /etc/bootptab` = 0 ]
                then
                    HAVE_ELS_IP_ADDRB="yes"
                else
                    echo
                    echo $ELS_IP_ADDR has already been assigned to a module in \
                         /etc/bootptab
                    HAVE_ELS_IP_ADDRB="no"
                fi
            fi

            case "$DRIVER $BOOTP" in
            "yes yes")
                    if [ "$HAVE_ELS_IP_ADDRA" = "yes" ] &&
                       [ "$HAVE_ELS_IP_ADDRB" = "yes" ] 
                    then
                        HAVE_ELS_IP_ADDR="yes"
                    fi
                    ;;
            "yes no")
                    if [ "$HAVE_ELS_IP_ADDRA" = "yes" ]
                    then
                        HAVE_ELS_IP_ADDR="yes"
                    fi
                    ;;
            "no yes")
                    if [ "$HAVE_ELS_IP_ADDRB" = "yes" ]
                    then
                        HAVE_ELS_IP_ADDR="yes"
                    fi
                    ;;
            "no no")
                    # shouldn't be able to get here
                    ;;
            esac
        else
            echo
            echo
            echo "$ELS_IP_ADDR is not a valid IP address."
        fi
    done
fi
    

if [ "$BOOTP" = "yes" ]
then
    while [ $HAVE_NETMASK != "yes" ]
    do
        echo -e "The netmask for this module (default is $NETMASK_DEFAULT) is: \c"
        read ELS_NETMASK
        if [ "$ELS_NETMASK" = "" ]
        then
            ELS_NETMASK=$NETMASK_DEFAULT
        fi
        if [ `echo "$ELS_NETMASK" | egrep -v ".*\..*\..*\..*\..*" \
              | egrep -c ".*\..*\..*\..*"` = 1 ]
        then
            HAVE_NETMASK="yes"
        else 
            HAVE_NETMASK="no"
        fi
    done

    while [ "$HAVE_GATEWAY" = "yes_need" ]
    do
        echo
        echo -e "The IP address of the gateway from this module is: \c"
        read ELS_GATEWAY_IP_ADDR
        if [ `echo "$ELS_GATEWAY_IP_ADDR" | egrep -v ".*\..*\..*\..*\..*" \
              | egrep -c ".*\..*\..*\..*"` = 1 ]
        then
            HAVE_GATEWAY="yes"
        fi
    done
fi


echo 
echo
echo "This host runs the driver for this module:      $DRIVER"
echo "This host is the BOOTP server for this module:  $BOOTP"
if [ "$DRIVER" = "yes" ] || [ "$BOOTP" = "yes" ]
then
echo "This module's IP address:                       $ELS_IP_ADDR"
if [ "$BOOTP" = "yes" ]
then
echo "This module's Ethernet address:                 $ELS_ETHER_ADDR"
echo "This module's netmask:                          $ELS_NETMASK"
if [ "$HAVE_GATEWAY" = "yes" ]
then
echo "This module's gateway:                          $ELS_GATEWAY_IP_ADDR"
fi
fi
fi
echo
echo -e "Is this information correct? (Y/n/q): \c"
read CORRECT
if [ "$CORRECT" = "y" ] || [ "$CORRECT" = "Y" ] || [ "$CORRECT" = "" ] 
then
    CORRECT="yes"
    if [ "$BOOTP" = "yes" ]
    then
        BOOTP_ENTRIES_ADDED="yes"
    fi
else
    if [ "$CORRECT" = "q" ] || [ "$CORRECT" = "Q" ]
    then
        CORRECT="quit"
    else
        CORRECT="no"
    fi
fi

}



#
# Convert a decimal number into base 16
#
base16() {

BASE16=`echo "obase=16; $1" | bc | tr "ABCDEF" "abcdef"`

}




#
# Determine which els number we should use next for /etc/els.conf
#
get_driver_count() {

DRIVER_COUNT=0

if [ -f /etc/els.conf ]
then
    base16 $DRIVER_COUNT    
    while [ "`fgrep -c els$BASE16 /etc/els.conf`" != 0 ]
    do
        DRIVER_COUNT=`expr $DRIVER_COUNT + 1`
        base16 $DRIVER_COUNT
    done
else
    touch /etc/els.conf
    chmod 644 /etc/els.conf
fi

}


#
# Determine which els number we should use next for bootptab
#
get_bootp_count() {

BOOTP_COUNT=0

if [ -f /etc/bootptab ]
then
    while [ "`fgrep -c els$BOOTP_COUNT /etc/bootptab`" != 0 ]
    do
        BOOTP_COUNT=`expr $BOOTP_COUNT + 1`
    done
else
    touch /etc/bootptab
    chmod 644 /etc/bootptab
fi

}



#
# Use the information gathered and put it in the proper places
#
apply_unit_info() {

if [ "$DRIVER" = "yes" ]
then
    get_driver_count
    base16 $DRIVER_COUNT
    if [ `grep -c "els " /proc/modules` -eq 0 ]
    then
		insmod els || {
			echo -e "\a Could not load els.o."
			echo "Hint: Install els.o or rebuild els.o with current kernel module defines."
			echo "Exiting..."
			exit 1
		}
	
    fi
    cdmknods -addunit els$BASE16 $MODEL $ELS_IP_ADDR
    sleep 5
#    echo els$BASE16 $MODEL $ELS_IP_ADDR\  >> /etc/els.conf
fi


if [ "$BOOTP" = "yes" ]
then
    get_bootp_count
    base16 $BOOTP_COUNT

    # remove ':' from Ethernet address
    ETH=`echo $ELS_ETHER_ADDR | awk -F: '{print $1$2$3$4$5$6}'`
    CNT=$BASE16
    GW=$ELS_GATEWAY_IP_ADDR
    SM=$ELS_NETMASK
    IP=$ELS_IP_ADDR

    if [ "$CNT" = 0 ]
    then
        echo >> /etc/bootptab
    fi

    if [ "$HAVE_GATEWAY" = "yes" ]
    then
        echo "els$CNT:sm=$SM:gw=$GW:ht=ether:ha=$ETH:ip=$IP" >>/etc/bootptab
    else
        echo "els$CNT:sm=$SM:ht=ether:ha=$ETH:ip=$IP" >>/etc/bootptab
    fi
fi

}




#
# Setup the host as a bootp server
#
setup_bootp_server() {

if [ `grep -c "bootps" /etc/inetd.conf` -gt 0 ]
then
    if [ `grep -c "^bootps" /etc/inetd.conf` -gt 0 ]
    then
        BOOTP_SERVER_ENABLED="enabled"
    else    
        if [ `grep -c "^#bootps" /etc/inetd.conf` = 1 ]
        then
            echo
            echo "Enabling bootpd in /etc/inetd.conf"
            echo
            cp /etc/inetd.conf /etc/inetd.conf.pre_els
            SPLICE=`grep -n bootps /etc/inetd.conf | awk -F: '{ print $1 }'`
            SPLICE=`expr ${SPLICE} - 1`
            head -${SPLICE} /etc/inetd.conf > /tmp/inetd.conf
            echo `grep bootps /etc/inetd.conf | cut -c 2-` >> /tmp/inetd.conf
            SPLICE=`expr ${SPLICE} + 2`
            tail +${SPLICE} /etc/inetd.conf >> /tmp/inetd.conf
            # replace the original file leaving ownership/perms intact
            cat /dev/null > /etc/inetd.conf
            cat /tmp/inetd.conf >> /etc/inetd.conf
            rm /tmp/inetd.conf
            BOOTP_SERVER_ENABLED="yes"
        else
            echo
            echo "Unable to automatically enable bootpd.  You will need to"
            echo "enable it manually."
            echo
            BOOTP_SERVER_ENABLED="no"
        fi
    fi
else
    # no bootp entry in /etc/inetd.conf so we add our own
    echo
    echo "Enabling bootpd in /etc/inetd.conf"
    echo
    cp /etc/inetd.conf /etc/inetd.conf.pre_els
    echo "#"                                             >>/etc/inetd.conf
    echo "# bootp added by Digi Install script"  >>/etc/inetd.conf
    echo "bootps dgram udp wait root $BOOTPD_BIN bootpd" >>/etc/inetd.conf
    BOOTP_SERVER_ENABLED="yes"
fi


if [ `fgrep -c "^bootps" /etc/services` = 0 ]
then
    # no bootp entry in /etc/services so we add our own
    echo "#"                                             >>/etc/services
    echo "# bootp added by Digi Install script"  >>/etc/services
    echo "#"                                             >>/etc/services
    echo "bootps        67/udp      bootp   # BOOTP Server  " >>/etc/services
    echo "bootpc        68/udp              # BOOTP Client  " >>/etc/services
fi
}






#####
##### main()
#####


if [ "`id | grep -c uid=0`" = "0" ]
then
    echo
    echo
    echo You must be superuser to run this utility.
    echo Use 'su' to become root and execute ./Install again
    exit 1
fi

INSTALL="no"
BOOTP_SERVER="no"
BOOTP_SERVER_ENABLED="no"
BOOTP_ENTRIES_ADDED="no"


echo
echo
echo -e "Do you have a Digi EtherLite(R) module to install? (N/y): \c"
read INSTALL
if [ "$INSTALL" = "n" ] || [ "$INSTALL" = "N" ] || [ "$INSTALL" = "" ]
then
    DONE="yes"
    INSTALL="no"
else
    DONE="no"
    INSTALL="yes"
    if [ ! -f /etc/els.conf ]
    then
        touch /etc/els.conf
        chmod 644 /etc/els.conf
    fi
fi

if [ "$INSTALL" = "yes" ]
then
    echo -e "Is this host now or will this host be a BOOTP server? (Y/n): \c"
    read BOOTP_SERVER
    if [ "$BOOTP_SERVER" = "" ] || [ "$BOOTP_SERVER" = "y" ] || \
       [ "$BOOTP_SERVER" = "Y" ]
    then
        BOOTP_SERVER="yes"
        setup_bootp_server
    else
        BOOTP_SERVER="no"
    fi


    while [ "$DONE" != "yes" ]
    do

        CORRECT="no"
        while [ "$CORRECT" = "no" ]
        do
            get_unit_info
        done

        if [ "$CORRECT" = "yes" ]
        then
            apply_unit_info
        fi

        echo
        echo -e "Do you have additional Digi EtherLite \c"
        echo -e "modules to install? (N/y): \c"
        read INSTALL
        if [ "$INSTALL" = "n" ] || [ "$INSTALL" = "N" ] || [ "$INSTALL" = "" ]
        then
            DONE="yes"
        fi

    done


    if [ "$BOOTP_SERVER_ENABLED" = "yes" ]
    then
        # find inetd's pid and kill -HUP it to get it to reload inetd.conf
        kill -HUP `ps -eaf | grep inetd | grep -v grep | awk '{print $2}'`
    fi


    if [ "$CORRECT" = "yes" ]  # they actually installed units
    then
        echo
        echo "At this point, you should attach your EtherLite module(s) to the"
        echo "network if you have not done so already."
        echo
    fi

    if [ ! -f .sts_install_dir ] && [ "$CORRECT" = "yes" ]
    then
        echo
        echo  "Do you want to generate a file  of getty commands"
	echo -e "that you can edit and add to /etc/inittab? (N/y):\c"
        read GETTYS
        if [ "$GETTYS" = "y" ] || [ "$GETTYS" = "Y" ] || [ "$GETTYS" = "YES" ]
        then
	    cdmknods -auto -dryrun -inittab els.getty
	    echo "Created ./els.getty with getty entries for inittab."
        fi
    fi

fi

echo 




