#!/bin/sh
###############################################################
# kpciinstall 2.0.1 raw  4/17/02                              #
# kpciinstall 2.0.2 raw  4/17/02 Changes for RH 6.2           #
# kpciinstall 2.0.3 raw  4/17/02 Changes for user auth in wep #
# setkey functions.
# kpciinstall 2.0.4 taw  4/18/02 Fix compiler breakage        #
# Script to build/install the 2.0 version PCMCIA MIC and      #
# the miniPCI drivers and utilities. With -R argument         #
# utilities and drivers are removed                           #
# install 2.0.5          4/05/03 change the world             #
###############################################################

KERNEL_RELEASE=`uname -r`
SRCDIR=/usr/src/linux-`uname -r`
MODBASE="/lib/modules/`uname -r`"
SRCDIRS=`echo /usr/src/*`
ARCHDIR=`pwd`

KPCMCIA=""
LINUX24=`uname -r|grep 2\.4`>/dev/null
CSS=""
ALWAYSCOMPILE="n"
ALLINSTALL="n"
ALLREMOVE="n"
DRVREM="n"
UTLREM="n"
DRVINS="n"
UTLINS="n"
CURWD=""
CC=""           # Broken compiler fix for redhat 7.0

########################################################
# Command lines for compiling the drivers              #
########################################################
MPICMP='${CC} -MD  -O2 -Wall -Wstrict-prototypes -pipe -I${SRCDIR}/include -D__KERNEL__ -DMODULE -c mpi350.c'
AIROCMP='${CC} -MD -O2 -Wall -Wstrict-prototypes -pipe -I${SRCDIR}/include -D__KERNEL__ -DMODULE -c airo.c'
AIROCSCMP='${CC} -MD -O2 -Wall -Wstrict-prototypes -pipe -I${SRCDIR}/include -D__KERNEL__ -DMODULE -c airo_cs.c'
CBCMP='${CC} -MD  -O2 -Wall -Wstrict-prototypes -pipe -I${SRCDIR}/include -D__KERNEL__ -DMODULE -c cb20_cb.c'


#############################################################
# Usage: install -options
# Note: dash (-) necessary
# -C always compile drivers 
# -R remove all 
# -rd remove drivers
# -ru remove utilities
# -iu install utilities
# -id install drivers
# No arguments installs everything overwriting previous 
# install if it exists.
#############################################################

if [ "$UID" != "0" ]
then
  echo "This script needs to be executed as root to operate properly."
  exit 1
fi

if [ $# -ne 0 ]
then     
  while getopts ":i:du:r:du:RC" Option
  do
     case $Option in
      C ) ALWAYSCOMPILE="y";;
      R ) ALLREMOVE="y";;
      r ) 
	case $OPTARG in 
		d) echo "Remove drivers";DRVREM="y" ;; 
		u) echo "Remove utils";UTLREM="y" ;;
		*) echo "Unrecognized remove command \"$OPTARG\" exiting.";exit 1;;
	esac
	;;
      i ) echo  "Install #4: option -i-, with argument \"$OPTARG\""
	case $OPTARG in 
		d) echo "Install drivers";DRVINS="y";;
		u) echo "Install utils";UTLINS="y";;
		*) echo "Unrecognized install command \"$OPTARG\" exiting.";exit 1;;
	esac
	;;
      * ) echo "Unrecognized Option $OPTARG  exiting";exit 1;;   
     esac
  done
  shift $(($OPTIND - 1))  
else
   ALLINSTALL="y"
fi
if [[ ${DRVINS} = "n" && ${DRVREM} = "n" && ${UTLINS} = "n" && ${UTLREM} = "n" && ${ALLREMOVE} = "n" ]]
then
    ALLINSTALL="y"
fi

if [[ ${ALLINSTALL} = "y" || ${UTLINS} = "y" || ${DRVINS} = "y" ]]
then
    more EULA.txt
    echo
    echo "Do you accept this license (y/n)?"
    read ans
    if [[ ${ans} != "y" && ${ans} != "Y" ]]
    then
        exit 1
    fi
fi

bad="n"
if [[ ${ALLINSTALL} = "y" || ${UTLINS} = "y" ]]
then
    ldcmd="ldconfig"
    cmdout=`type -p $ldcmd`
    if [ -z $cmdout ]
    then
        ldcmd="/sbin/ldconfig"
        cmdout=`type -p $ldcmd`
        if [ -z $cmdout ]
        then
            ldcmd="/usr/sbin/ldconfig"
            cmdout=`test -p $ldcmd`
            if [ -z $cmdout ]
            then
                echo "Cannot find ldconfig. Cannot test for necessary libraries."
                ldcmd=""
            fi
        fi
    fi
echo ldcmd $ldcmd
    if [ -n "$ldcmd" ]
    then
        echo "Checking for libsigc++ ..."
        lib=`$ldcmd -N -X --print-cache | grep "libsigc-1.0.so.0"`
        if [ -z "$lib" ]
        then
            echo "libsigc++ not installed."
            bad="y"
        else
            echo "libsigc++ found."
        fi
        echo "Checking for gtkmm ..."
        lib=`$ldcmd -N -X --print-cache | grep "libgtkmm-1.2.so.0"`
        if [ -z "$lib" ]
        then
            echo "libgtkmm not installed."
            bad="y"
        else
            echo "libgtkmm found."
        fi
    fi
fi

if [ $bad = "y" ]
then
    echo
    echo "The libraries can be downloaded from the web."
    echo
    echo "Sources are available from sourceforge.net (projects gtkmm and"
    echo "libsigc++). Be sure that you download the correct versions (1.2.10"
    echo "of gtkmm and 1.0.4 of libsigc++). You must install libsigc++ before"
    echo "you can install gtkmm."
    echo
    echo "RPM packages of the libraries can be obtained from freshrpms.net."
    exit 1
fi

echo
echo "Continuing ..."

####################################################
# Broken compiler FIX  avoid using broken compiler #
# to build drivers                                 #
####################################################
fixrh70() {
    if [ -x /usr/bin/kgcc ]
    then
	CC=kgcc
    else
	CC=cc
    fi
}

##################################################
# Find any ethernet adapters, originally written #
# by Jim Venesky adapted by Roland Wilcher       #
##################################################

find_ethernets(){
    echo "Now attempting to determine how many Ethernet cards you have installed."
    # first check and see if we already have an Aironet card installed with
    # another (Ben Reeds) driver
    mytest=`ifconfig 2> /dev/NULL`
    if [ -z "$mytest" ]
    then
      cmd="/sbin/ifconfig"
    else
      cmd="ifconfig"
    fi
    adapter=`${cmd} -a | egrep "00:40:96|00:07:50|00:01:64" | tail --lines=1 | cut -c 4`
    if [ -z "$adapter" ]
    then
	# no Aironet cards detected - look for other Ethernet cards...
        adapter=`${cmd} -a | grep eth | tail --lines=1 | cut -c 4`
    if [ "1$adapter" -eq "1" ]
    then
	adapter="-1"
        echo "You currently have no Ethernet adapters installed."
    else
	echo "You already have a non-Aironet Ethernet adapter."
    fi
	next_adapter=`expr $adapter + 1`
    else
        echo "You already have an Aironet card installed as eth$adapter."
	next_adapter=$adapter
    fi
    
    # copy over a new version of the default config file
    # otherwise acu may get upset if it finds an old style one

    if [ -f ${ARCHDIR}/ethX.cfg ]
    then
        cp ${ARCHDIR}/ethX.cfg /etc/eth$next_adapter.cfg
	chmod a+rw /etc/eth$next_adapter.cfg
    else
	echo "ERROR - ethX.cfg was not found."
        echo "Please run the install from the directory containing the contents o
f the driver/utility archive."
        exit 1
    fi
}



########################################
# Find kernel sources                  #
########################################
find_source(){
    for i in ${SRCDIRS} 
    do
	if [ -d $i ]
	then 
	    LDIR=$i
	    if [ -d ${i}/Documentation ]
	    then
		SRCDIR=${LDIR}
		break 
	    fi
	else
	    SRCDIR=""
	fi
    done
}

# Function to test for kernel PCMCIA

check_kernel_pcc()
{
    KCSS=`/sbin/lsmod|grep ^yenta|cut -d' ' -f1` >/dev/null
    if [ ${KCSS}X = X  ]
    then 
       KPCMCIA="N"
    else
       KPCMCIA="Y"
    fi
}

# function to test for card and socket
# services.

check_cs_pcc()
{
    KCSS=`/sbin/lsmod|grep ^i82365|cut -d' ' -f1` >/dev/null

    if [ ${KCSS}X = X  ]
    then 
       CSS="N"
    else
       CSS="Y"
    fi
}

#############################################
# Ask for the source directory, Just hitting# 
# return will abort the install             #
#############################################

ask4src(){
    echo -n "Please enter the path to your kernel sources: "
    read SRCDIR
    if [ ${SRCDIR}H = "H" ]
    then
      echo "Module compilation aborted."
      exit 1;
    fi
}

# Remove everything

remove(){

    echo "Removing Cisco/Aironet drivers and utilities"
    rm -rf /opt/cisco         # Remove utilities
    rm -f /etc/eth?.cfg       # remove config files.

    if [ ${LINUX24}X != X ]
    then
	rm -f ${MODBASE}/kernel/drivers/net/mpi350.o
	rm -f ${MODBASE}/kernel/drivers/net/pcmcia/airo.o
	rm -f ${MODBASE}/kernel/drivers/net/airo.o
    else
	rm -f ${MODBASE}/net/mpi350.o
	rm -f ${MODBASE}/net/mpi350.o
	rm -f ${MODBASE}/pcmcia/airo.o
	rm -f ${MODBASE}/net/airo.o
	echo "Cisco/Aironet utilities and drivers have been removed"
    fi
	exit 0

}
 
remove_utils(){
    echo "Removing Cisco/Aironet utilities only"
    rm -rf /opt/cisco
    rm -f /etc/eth?.cfg
    echo "Cisco/Aironet utilities have been removed"
    exit 0;
}


remove_drivers(){

    if [ ${LINUX24}X != X ]
    then
	rm -f ${MODBASE}/kernel/drivers/net/mpi350.o
	rm -f ${MODBASE}/kernel/drivers/net/pcmcia/airo.o
	rm -f ${MODBASE}/kernel/drivers/net/airo.o
    else
	rm -f ${MODBASE}/net/mpi350.o
	rm -f ${MODBASE}/pcmcia/airo.o
	rm -f ${MODBASE}/net/airo.o
    fi	

    echo "Cisco/Aironet drivers have been removed"
    exit 0
}
   
remove_all(){
    echo "Removing Cico/Aironet drivers and utilities"

    rm -rf /opt/cisco         # Remove utilities
    rm -f /etc/eth?.cfg       # remove config files.
    rm -f ${MODBASE}/kernel/drivers/net/mpi350.o
    rm -f ${MODBASE}/kernel/drivers/net/pcmcia/airo.o
    rm -f ${MODBASE}/kernel/drivers/net/airo.o
    
    echo "Cisco/Aironet utilities and drivers have been removed"
    exit 0
}

#################################################
# Install device drivers                
#################################################

install_drivers(){
    cd ${CURWD}
    if [ ! -d driver ]
    then
	echo "Please run the install from the directory containing the "
	echo "contents of the driver and utility archive."
	exit 1
    fi
    
    fixrh70  # fix cc breakage for RH7.0

    GOOD="n"
    if [ ${GOOD} = "n" ]
    then
        cd driver

        find_source

        if [ ${SRCDIR}X = X ]
        then
            while [ ! -d ${SRCDIR}/Documentation ]
            do
                echo "Cannot locate source in ${SRCDIR} "
                ask4src
            done
        fi

        echo "Please wait , Compiling driver modules using sources in ${SRCDIR}"

        fixrh70
        eval ${MPICMP}
        eval ${AIROCMP}
        eval ${AIROCSCMP}
#        eval ${CBCMP}
        copy_drivers

    fi
}

copy_drivers(){
    find_ethernets;

    if [ ${KPCMCIA} = "Y" ]
    then
        echo "Your system is configured with kernel PCMCIA services"
        cp airo.o ${MODBASE}/kernel/drivers/net/pcmcia/airo.o
        cp airo_cs.o ${MODBASE}/kernel/drivers/net/pcmcia/airo_cs.o
#       cp cb20_cb.o ${MODBASE}/kernel/drivers/net/cb20_cb.o
    fi

    if [ ${CSS} = "Y" ]
    then
        cp airo.o ${MODBASE}/pcmcia/
    fi
    #############################
    # PCI only install          #
    #############################
    if [ ${CSS} = "N" ]
    then
        if [ ${KPCMCIA} = "N" ]
        then
            if [  ${LINUX24}X != X ]
            then
                cp airo.o ${MODBASE}/kernel/drivers/net
            else
                cp airo.o ${MODBASE}/net
            fi
        fi
    fi

    if [  ${LINUX24}X != X ]
    then
        cp mpi350.o ${MODBASE}/kernel/drivers/net
    else
        cp mpi350.o ${MODBASE}/net
    fi

    cd ..

    /sbin/depmod
}

##############################################################################
# Install itilities                                                          #
# check and see if this directory exists - if it does see if it is writable  #
# if it does not exist - attempt to create it.                               #
# if we fail - exit out with an error message.                               #
##############################################################################

install_utils(){

location="/opt/cisco/bin"

cd ${CURWD}

if [ -d $location ]
then 
    if [ -w $location ]
    then
	echo ""
    else
	echo "ERROR - the utilities can not be installed in /opt/cisco/bin"
	echo "Exiting installation"
	exit 1
    fi
    else
	mkdir -p $location 2> /dev/null
	if [ -d $location ]
	then
	    echo "/opt/cisco/bin successfully created"
	else
	    echo "ERROR - unable to create /opt/cisco/bin"
	    echo "try to create it by hand and re-run install."
	    exit 1
	fi
   fi

   ############################################################
   # attempt to copy the utilities and help files from the    #
   # unpacked archive to /opt/cisco/{bin/helpml} utilities to #
   # install:  acu,bcard,leapset, leapscript,leaplogin        #
   ############################################################

    echo -n "Installing the utilities: "

#    if [ -z `uname -r | grep 2.4.18` ] 
#    then
#        release="redhat90"
#    else
#        release="redhat73"
#    fi
    if [ -z "`ldd -d -r utilities/redhat73/acu 2>&1 | grep undefined`" ]
    then
        release="redhat73"
    else
        if [ -z "`ldd -d -r utilities/redhat90/acu 2>&1 | grep undefined`" ]
        then
            release="redhat90"
        else
            echo ""
            echo "There is one or more problems with running the ACU."
            echo "If you have not installed the correct versions of the"
            echo "libraries, please install them and re-run this script."
            echo ""
            echo "If this is not the case, you might be using an unsupported configuration."
            echo "RedHat 7.3, RedHat 9.0 and Suse 9.0 are supported."
            echo ""
            exit 1
        fi
    fi
    for utility in acu bcard leapset leapscript leaplogin
    do
	if [ -f "utilities/$release/$utility" ]
	then
	    cp utilities/$release/$utility $location
	    chmod a+x $location/$utility
	    echo -n "$utility "
	else
	    echo ""
	    echo "ERROR - $utility was not found."
	    echo "Please run the install from the directory containing the contents of the driver/utility    archive."
	    exit 1
	fi
    done

    if [ -f ACU.PRFS ]
    then
	cp ACU.PRFS /opt/cisco
	chmod a+rw /opt/cisco/ACU.PRFS
    fi

    echo ""
    if [ -f "helpml.tar.gz" ]
    then
	echo "Installing Help Files..."
	cp helpml.tar.gz /opt/cisco
	pushd /opt/cisco > /dev/null
	tar zxf helpml.tar.gz
	rm -f helpml.tar.gz
	popd > /dev/null
	echo "Help Files installed."
    else
	echo "ERROR - the help file archive was not found: helpml.tar.gz"
	echo "Please run the install from the directory containing the contents of the driver/utility archive."
	exit 1
    fi
 echo ""
}

# Begin

CURWD=`pwd`
check_kernel_pcc
check_cs_pcc

#################################################
# Giving an -R argument to this script will     #
# remove all  utilities and drivers.            #
#################################################


if [ ${ALLREMOVE} = "y" ]
then 
    clear 
    echo -n "Remove drivers and utilities from the system are you sure (y/n):"
    read ANS
    
    if [ $ANS = "y" ]
    then 
	remove
    elif [ $ANS = "Y" ]
    then 
	remove
    else
	echo "Not verified no action taken."
    fi
    exit 0
fi

#################################################
# -rd removes the drivers from the system       #
#################################################
if [ ${DRVREM} = "y" ]
then
   clear
   echo -n "Remove Aironet drivers from the system are you sure (y/n):"
   read ANS

    if [ $ANS = "y" ]
    then 
	remove_drivers
    elif [ $ANS = "Y" ]
    then 
	remove_drivers
    else    
	echo "Not verified no action taken."
    fi
    exit 0
fi

####################################################
# -ru removes the utilities from system.           #
####################################################
if [ ${UTLREM} = "y" ]
then
   clear
   echo -n "Remove Aironet utilities are you sure (y/n):"
   read ANS

   if [ $ANS = "y" ]
   then 
       remove_utils
   elif [ $ANS = "Y" ]
   then 
       remove_utils
   else
	echo "Not verified no action taken."
   fi
   exit 0
fi

####################################################
# -id install drivers                              #
####################################################
if [ ${DRVINS} = "y" ]
then
    echo
    echo "Installing Cisco/Aironet drivers"

    install_drivers;
    echo "Drivers installed"
    exit 0
fi

####################################################
# -iu install utils                                #
####################################################
if [ ${UTLINS} = "y" ]
then
    echo
    install_utils
    echo "Utilities installed"
    exit 0
fi

####################################################
# no args install everything                       #
####################################################

if [ ${ALLINSTALL} = "y" ]
then
  echo
  install_drivers
  install_utils
  echo "Drivers and utilities installed"
  exit 0
fi




