#!/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                           #
###############################################################

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=""
ALLINSTALL="n"
ALLREMOVE="n"
DRVREM="n"
UTLREM="n"
DRVINS="n"
UTLINS="n"
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'

#############################################################
# Usage: kpciinstall -options
# Note: dash (-) necessary
# -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 [ $# -ne 0 ]
then     
  while getopts ":i:du:r:du:R" Option
  do
     case $Option in
      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

####################################################
# 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
    adapter=`ifconfig -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=`ifconfig -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(){
    
    if [ ! -d driver ]
    then
	echo "Please run the kpciinstall from the directory containing the "
	echo "contents of the driver/utility archive."
	exit 1
    fi
    
    fixrh70  # fix cc breakage for RH7.0

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

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 kpciinstall."
	    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: "

    for utility in acu bcard leapset leapscript leaplogin
    do
	if [ -f "utilities/$utility" ]
	then
	    cp utilities/$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

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
    clear
    echo "Installing Cisco/Aironet drivers"

    install_drivers;
    echo "Drivers installed"
    exit 0
fi

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

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

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




