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

#
# Set possible return codes for this script
#
OK=0;   FAIL=1; RELINK=2;

#
# Prompt the user for a hex value, it must be within a given range
# Usage:
#       prompt_range "Message" default min max [step]
#
prompt_range() {
        mesg=$1
        default=$2
        range_min=$3 range_max=$4
        step="1"
        if [ $# -eq 5 ]
        then
                step=$5
        fi

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

                return $OK
                if [ "$result" >= "$range_min" ]
                then
                        if [ "$result" <= "$rangme_max" ]
                        then
                                return $OK
                        else
                                return $FAIL
                        fi
                else
                        return $FAIL
                fi
        done
}

#
# 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
}

#
# system_840()
#
# Function to produce the info for the System file for the W89C840 boards.
# Function to produce the BurstLen="XXX", CacheAlign="XXX", TxThreshold="XXX",
# LineSpeed="XXX" and FullDuplex="XXX" for driver.conf file.
#
# The info produced :
#
# The Burst Length - BurstLen
# The Cache Alignment - CacheAlign
# The Transmit Threshold Value - TxThreshold
# The Line Speed - LineSpeed
# The Full Duplex Function - FullDuplex
#
system_840() {

        BurstLen=6;
        CacheAlign=3;
        TxThreshold=0;
        LineSpeed=0;
        FullDuplex=0;
        Nway=0;

        echo "Please choose the value of burst length that you want ..."
        echo ""
        echo "  1. 1 longword."
        echo "  2. 2 longwords."
        echo "  3. 4 longwords."
        echo "  4. 8 longwords."
        echo "  5. 16 longwords."
        echo "  6. 32 longwords(default value)."
        echo ""
        prompt_select "Enter number of burst length selection" $BurstLen "1 2 3 4 5 6"
        BurstLen=$result
        echo ""

        case $BurstLen in
          1) echo "Burst Length : 1 longword."
             BurstLen=256;
             ;;
          2) echo "Burst Length : 2 longword."
             BurstLen=512;
             ;;
          3) echo "Burst Length : 4 longword."
             BurstLen=1024;
             ;;
          4) echo "Burst Length : 8 longword."
             BurstLen=2048;
             ;;
          5) echo "Burst Length : 16 longword."
             BurstLen=4096;
             ;;
          6) echo "Burst Length : 32 longword."
             BurstLen=8192;
             ;;
        esac
        echo ""

        echo "Please choose the value of cache alignment that you want ..."
        echo ""
        echo "  1. 8-longword boundary alignment."
        echo "  2. 16-longword boundary alignment."
        echo "  3. 32-longword boundary alignment(default value)."
        echo ""
        prompt_select "Enter number of cache alignment selection" $CacheAlign "1 2 3"
                CacheAlign=$result
        echo ""

        case $CacheAlign in
          1) echo "Cache Alignment : 8-longword boundary alignment."
             CacheAlign=16384;
             ;;
          2) echo "Cache Alignment : 16-longword boundary alignment."
             CacheAlign=32768;
             ;;
          3) echo "Cache Alignment : 32-longword boundary alignment."
             CacheAlign=49152;
             ;;
        esac
        echo ""

        echo "Please choose the value of transmit threshold that you want ..."
        echo "The default value of transmit threshold is 0."
        echo ""
        prompt_range "Enter value of transmit threshold" $TxThreshold 0 127
                TxThreshold=$result
        echo ""
        echo "Transmit Threshold : "$TxThreshold
        echo ""

        echo "Please choose the nway function that you want..."
        echo ""
        echo "  0. Default value."
        echo "  1. Nway off."
        echo "  2. Nway on."
        echo ""
        prompt_select "Enter number of nway function selection" $Nway "0 1 2"
                Nway=$result
        echo ""

        case $Nway in
          1) echo "Nway Function : Default."
             ;;
          2) echo "Nway Function : Off."
             ;;
          3) echo "Nway Function : On."
             ;;
        esac
        echo ""

        echo "Please choose the line speed that you want..."
        echo ""
        echo "  0. Auto detect the line speed mode(default value)."
        echo "  1. 10 MB/sec mode."
        echo "  2. 100 MB/sec mode."
        echo ""
        prompt_select "Enter number of line speed selection" $LineSpeed "0 1 2"
                LineSpeed=$result
        echo ""

        case $LineSpeed in
          0) echo "Line Speed : Auto detect mode.";
             ;;
          1) echo "Line Speed : 10 MB/sec mode.";
             ;;
          2) echo "Line Speed : 100 MB/sec mode.";
             ;;
        esac
        echo ""

        echo "Please choose the full duplex function that you want..."
        echo ""
        echo "  0. Auto detect the full duplex function(default value)."
        echo "  1. disable full duplex function."
        echo "  2. enable full duplex function."
        echo ""
        prompt_select "Enter number of full duplex function selection" $FullDuplex "0 1 2"
                FullDuplex=$result
        echo ""

        case $FullDuplex in
          0) echo "Full Duplex Function : Auto detect mode.";
             ;;
          1) echo "Full Duplex Function : Disable full duplex function."
             ;;
          2) echo "Full Duplex Function : Enable full duplex function."
             ;;
        esac
        echo ""
}

##
##  Get the necessary information from users
##  Let user tell us info. about device driver and we will
##  prepare w840s.conf, hosts, hostname.w840s0 for users
##  copy w840s to /kernel/drv
##  copy w840s.conf to /kernel/drv
##  copy hosts to /etc
##  copy hostname.w840s0 to /etc
##
prompt_gethostname()
{
        command=`uname -n`
        hostname=`expr $command`
        hostname0=$hostname
        echo "Hostname of Board $Board: $hostname0"
        echo ""
}

prompt_getloghost()
{
        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.w840s$Board

        if [ -f /kernel/drv/w840s.conf ]
        then
            rm /kernel/drv/w840s.conf		
        fi
	
        echo ' BurstLen=\c' >> w840s.conf
        echo $BurstLen'\c' >> w840s.conf
        echo ' CacheAlign=\c' >> w840s.conf
        echo $CacheAlign'\c' >> w840s.conf
        echo ' TxThreshold=\c' >> w840s.conf
        echo $TxThreshold'\c' >> w840s.conf
        echo ' Nway=\c' >> w840s.conf
        echo $Nway'\c' >> w840s.conf
        echo ' LineSpeed=\c' >> w840s.conf
        echo $LineSpeed'\c' >> w840s.conf
        echo ' FullDuplex=\c' >> w840s.conf
        echo $FullDuplex'\c' >> w840s.conf
        echo ';' >> w840s.conf
        echo ""

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

        echo "" >> hosts        ## change line
        if [ $loghost ]
        then
		DelHosts
                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 w840s ====> /kernel/drv ....'
        echo ""
        cp w840s /kernel/drv/w840s
        echo 'copy w840s.conf ====> /kernel/drv ....'
        echo ""
        cp w840s.conf /kernel/drv
        rm w840s.conf
        echo 'copy hosts ====> /etc ....'
        echo ""
        cp hosts /etc
        rm hosts
        echo 'copy hostname.w840s\c'
        echo $Board'\c'
        echo ' ====> /etc ....'
        echo ""
        if [ -f /etc/hostname.* ]
        then
           rm /etc/hostname.*		
        fi
        cp hostname.w840s$Board /etc
        rm hostname.w840s$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
}

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

        create_files
        copy_files

        /usr/sbin/add_drv -i '"pci1050,840"' w840s

        echo "Reboot the system to activate driver."
}

DoAddDriver()
{
        if grep "pci1050,840" /etc/driver_aliases > /dev/null 2>&1
        then
		/usr/sbin/rem_drv w840s
        fi

        AddDriver
}

DoConfigDriver()
{
        if grep "pci1050,840" /etc/driver_aliases > /dev/null 2>&1
        then
		ConfigDriver
	else
		echo "This network driver was not installed!  Please install the driver first.\n"
        fi
}

ConfigDriver()
{
        while :
        do
           clear
           system_840
           prompt_gethostname
           prompt_getloghost
           prompt_getgateway
           if prompt_change
           then
                   break
           fi
        done

        create_files

        cp w840s.conf /kernel/drv
        rm w840s.conf
        echo 'copy hosts ====> /etc ....'
        echo ""
        cp hosts /etc
        rm host*

        echo "Reboot the system to activate driver."
}


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

DoDelDriver()
{
        echo ""
        prompt_gethostname
#        DelHosts

        rm /etc/hostname.w840s$Board

}


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

Board=0

while   echo "Do you wish to install, configure or remove the network board? (i/c/r/q) [q] \c"
do      read ANSWER
        case $ANSWER in
        I|i)
                DoAddDriver
               # clear
                ;;
        C|c)
		echo ""
                DoConfigDriver
                ;;
        R|r)
                DoDelDriver
                clear
                ;;
        Q|q|"")
                break;
                ;;
        *)      echo "Please answer 'i', 'c', 'r' or 'q'."
                ;;
        esac
break;
done
echo    " "
