prompt_Dlink()
{
clear
echo "                                                                            "
echo "                                                                            "
echo "                                                                            "
echo "                                                                            "
echo "                                                                            "
echo "                                                                            "
echo "                                                                            "
echo "                                                                            "
echo "                                                                            "
echo "                         Install Program for the                            "
echo "                                                                            "
echo "                        Ether16 driver for Solaris                           "
echo "                                                                            "
echo "                                                                            "
echo "                                                                            "
echo "                                                                            "
echo "                                                                            "
echo "                        Press [Enter] key to continue                       "
echo "                                                                            "
echo "                                                                            "
echo "                                                                            "
read anykey
}

#
# prompt the user to answer a yes no question or 'q' to quit
# Usage:
#       prompt_yn "Message" default
prompt_yn() {
        mesg=$1
        default=$2

        while :
        do
                echo "${mesg} (y/n) [${default}] or 'q' to quit: \c"
                read result

                case $result in
                q|Q) return $FAIL;;
                y|Y) result="Y"; return $OK;;
                n|N) result="N"; return $OK;;
                "") result=`echo $default | tr "yn" "YN"`; return $OK;;
                esac

                echo "Illegal value, please type 'y' 'n' or 'q'"
        done
}

#
# prompt the user to answer a yes no question
# Usage:
#       prompt_yn1 "Message" default
prompt_yn1() {
        mesg=$1
        default=$2

        while :
        do
                echo "${mesg} (y/n) [${default}]: \c"
                read result

                case $result in
##                q|Q) return $FAIL;;
                y|Y) result="Y"; return $OK;;
                n|N) result="N"; return $OK;;
                "") result=`echo $default | tr "yn" "YN"`; return $OK;;
                esac

                echo "Illegal value, please type 'y' or 'n'"
        done
}

##
## Prompt the user to make a selection a list of values
## Usage:
##       prompt_select "Message" default "value_list"
prompt_select() {
        mesg=$1
        default=$2
        values=$3

        while :
        do
                if [ "$default" = "" ]
                then
                        echo "${mesg} (${values}) or 'q' to quit: \c"
                else
                        echo "${mesg} (${values}) [${default}] or 'q' to quit: \c"
                fi
                read result
                case $result in
                Q|q)
                        return $FAIL
                        ;;
                "")
                        result=$default
                        ;;
                esac

                for i in $values
                do
                        if [ "$i" = "$result" ]
                        then
                                return $OK
                        fi
                done
                echo "Illegal value, must be one of (${values})"
        done
}

##
## function to produce the PnP="XXX" for driver.conf file
##
prompt_IsPnPbios() {

        PnPbios=0

        prompt_yn1 "Does your machine support PnP bios ? " n
        if [ "$result" = "Y" ]
        then
              PnPbios=1
        else
              PnPbios=0
        fi
}

##
## function to produce the media="XXX" for driver.conf file
##
prompt_media() {

        MediaType=3

        echo "Note:"
        echo "  If the board of Ether16 Family is a non-plug and play card,"
        echo "  please use setup program in DOS to change I/O base address,"
        echo "  and IRQ number if conflict."
        echo ""
        echo ""
        echo "Please choose the media mode that you want....................."
        echo ""
        echo "  0. Twisted pair connection."
        echo "  1. BNC connection."
        echo "  2. AUI connection."
        echo "  3. Automatic detection between TP BNC and AUI."
        echo "  4. Use EEPROM setting."
        echo ""
        prompt_select " Enter number of media mode selection" $MediaType "0 1 2 3 4"
        MediaType=$result
        echo ""
        case $MediaType in
        0) MediaTypeTXT="UTP";
           echo " Media mode : Twisted pair. ";
           ;;
        1) MediaTypeTXT="BNC";
           echo " Media mode : BNC.";
           ;;
        2) MediaTypeTXT="AUI";
           echo " Media mode : AUI.";
           ;;
        3) MediaTypeTXT="AUTO";
           echo " Media mode : Automatic detection between TP BNC and AUI.";
           ;;
        4) MediaTypeTXT="DEFAULT";
           echo " Media mode : Use EEPROM setting";
           ;;
        esac
        echo ""
}

##
##  Get the necessary information from users
##  Let user tell us info. about device driver and we will
##  prepare d22e.conf, hosts, hostname.d22e0 for users
##  copy d22e to /kernel/drv
##  copy d22e.conf to /kernel/drv
##  copy hosts to /etc
##  copy hostname.d22e0 to /etc
##
prompt_gethostname()
{
##        echo "Input Hostname of Your Machine: \c"
##        read  hostname
##        echo ""
        command=`uname -n`
        hostname=`expr $command`

        if [ $Board -eq 0 ]
        then
                hostname0=$hostname
        else
                hostname0=$hostname$Board
        fi
        echo "Hostname of Board $Board: $hostname0"
        echo ""
}

prompt_getloghost()
{
###        if ((grep 'loghost' /etc/hosts > /dev/null 2>&1) | \
###            (grep $hostname /etc/hosts > /dev/null 2>&1))
        if ((grep '\($hostname0\).*loghost' /etc/hosts > /dev/null 2>&1))
        then
                loghost=""
        else
                echo "Input IP Address of Log Host: \c"
                read  loghost
                echo ""
        fi
}

prompt_getgateway()
{
        if grep "defaultGateway" /etc/hosts > /dev/null 2>&1
        then
                gateway=""
        else
                echo "Input IP Address of Default Gateway: \c"
                read  gateway
                echo ""
        fi
}

prompt_change()
{
        prompt_yn1 "Do you want to change the above setting ? " n
        if [ "$result" = "Y" ]
        then
            return 1
        else
            return 0
        fi
}

create_files()
{
        echo $hostname0 > hostname.d22e$Board

        if [ -f /kernel/drv/d22e.conf ]
        then
            cp /kernel/drv/d22e.conf d22e.conf
            echo 'name="d22e" class="sysbus"\c' >> d22e.conf
        else
            echo 'name="d22e" class="sysbus"\c'  > d22e.conf
        fi
        echo ' PnP=\c' >> d22e.conf
        echo $PnPbios'\c' >> d22e.conf
        echo ' media=\c' >> d22e.conf
        echo '"'$MediaTypeTXT'"\c' >> d22e.conf
        echo ' intr=5,3,5,4,5,5,5,9,5,10,5,11,5,12,5,15\c' >> d22e.conf
        echo ' reg=0x\c' >> d22e.conf
        echo $Board'\c' >> d22e.conf
        echo ',0,0 multisize=20;' >> d22e.conf
        echo ""

        if [ -f /etc/hosts ]
        then
            cp /etc/hosts hosts
        fi

        echo "" >> hosts        ## change line
        if [ $loghost ]
        then
                echo $loghost'\c' >> hosts
                echo '       \c' >> hosts
                echo $hostname0'\c' >> hosts
                echo ' loghost' >> hosts
        fi

        if [ $gateway ]
        then
                echo $gateway'\c' >> hosts
                echo '       \c' >> hosts
                echo 'defaultGateway router' >> hosts
                echo ""
        fi
}

copy_files()
{
        echo 'copy d22e ====> /kernel/drv ....'
        echo ""
        cp d22e /kernel/drv
        echo 'copy d22e.conf ====> /kernel/drv ....'
        echo ""
        cp d22e.conf /kernel/drv
        rm d22e.conf
        echo 'copy hosts ====> /etc ....'
        echo ""
        cp hosts /etc
        rm hosts
        echo 'copy hostname.d22e\c'
        echo $Board'\c'
        echo ' ====> /etc ....'
        echo ""
        cp hostname.d22e$Board /etc
        rm hostname.d22e$Board
}

getyn() {
        while   echo "\n$* (y/n/q) \c"
        do      read yn rest
                case $yn in
                        [yY]) return 0                          ;;
                        [nN]|[qQ]) return 1                     ;;
                *)      echo "Please answer y,n or q\n"           ;;
                esac
        done
}

GetBoardNo()
{
        Board=-1
        if [ -f /etc/hostname.d22e0 ]
        then
            Board=`expr $Board + 1`
        fi
        if [ -f /etc/hostname.d22e1 ]
        then
            Board=`expr $Board + 1`
        fi
        if [ -f /etc/hostname.d22e2 ]
        then
            Board=`expr $Board + 1`
        fi
        if [ -f /etc/hostname.d22e3 ]
        then
            Board=`expr $Board + 1`
        fi
}

AddDriver()
{
        while :
        do
           clear
           prompt_IsPnPbios
           prompt_media
           prompt_gethostname
           prompt_getloghost
           prompt_getgateway
           if prompt_change
           then
                   break
           fi
        done

        create_files
        copy_files
}

DoAddDriver()
{
        GetBoardNo
        Board=`expr $Board + 1`

        if [ $Board -gt 3 ]
        then
                echo "There is no more Board can be added !\n"
        else
                if prompt_yn  "Do you wish to add Board $Board ? " y
                then
                     if [ "$result" = "Y" ]
                     then
                             AddDriver
                     fi
                fi
        fi
}

DelBoard0()
{
                        ed -s /kernel/drv/d22e.conf << TOAST > /dev/null
/name="d22e"/
d
w
q
TOAST
}

DelBoard1()
{
                ed -s /kernel/drv/d22e.conf << TOAST > /dev/null
/name="d22e"/
//
d
w
q
TOAST
}

DelBoard2()
{
                ed -s /kernel/drv/d22e.conf << TOAST > /dev/null
/name="d22e"/
/name="d22e"/
//
d
w
q
TOAST
}

DelBoard3()
{
                ed -s /kernel/drv/d22e.conf << TOAST > /dev/null
/name="d22e"/
/name="d22e"/
/name="d22e"/
//
d
w
q
TOAST
}

DelHosts()
{
                ed -s /etc/hosts << TOAST > /dev/null
/$hostname0/
d
w
q
TOAST
                grep -v "^ *$" /etc/hosts > hosts
                mv hosts /etc/hosts
}

DelDriver()
{
        echo ""
        prompt_gethostname
        DelHosts

        rm /etc/hostname.d22e$Board

        case $Board in
                0)
                        DelBoard0
                        break;
                        ;;
                1)
                        DelBoard1
                        break;
                        ;;
                2)
                        DelBoard2
                        break;
                        ;;
                3)
                        DelBoard3
                        break;
                        ;;
                *)      echo "Error Board Number \n"
                        ;;
        esac


}

DoDelDriver()
{
        GetBoardNo

        if [ $Board -lt 0 ]
        then
                echo "There is no more Board can be deleted !\n"
                echo "Press Any key to continue...."
                read anykey
        else
                if prompt_yn "Do you wish to delete Board $Board ? " n
                then
                       if [ "$result" = "Y" ]
                       then
                               DelDriver
                       fi
                fi
        fi
}

##
## This is the main part of script file, the OS start implement here.
##
prompt_Dlink

while   echo "Do you wish to add or delete the network board ? (a/d/q) [q] \c"
do      read ANSWER
        case $ANSWER in
        A|a)
                DoAddDriver
                clear
                ;;
        D|d)
                DoDelDriver
                clear
                ;;
        Q|q|"")
                break;
                ;;
        *)      echo "Please answer 'a', 'd' or 'q'."
                ;;
        esac
done
echo    " "

