#!/usr/bin/bash
# Cisco Wireless Install for version 2.0 aironet utilities and drivers
# Cisco Wireless Install version 2.0 6/5 jmv
# Cisco Wireless Install version 2.1 3/21 raw
# this installation script assumes ALL installation files are in the same
# directory as this installation script or it will fail with an error.
# This script requires root privileges

# changes in this version:
# /opt/cisco/bin is now the required installation directory
# helpfiles are installed in /opt/cisco/helpml
# three different redhat sub-scripts maybe call if using rh7.1
# 
# raw changed minimally to setup acu 1.6.7 
# 11/29/2001 The only thing that has changed is ACU
# ACU is now at version 1.6.7 from 1.5.6 as in 1.6.7 
# the format of the config files have changed slightly
# a new default ethX.cfg file is needed. 
# 3/19/2002 New eth?.cfg for V.2 ACU

clear 

#check and see if we should jump to part 2...

if [ -f /tmp/._runpart2 ];then
		if [ -f cwinstall.2 ];then
				sh ./cwinstall.2
				exit 0
		else
			echo "Please run this from the directory where it was unpacked"
			exit 1
		fi
fi
# print welcoming text
echo 'Welcome to the Cisco Aironet Wireless Installation script!'
echo ''
echo 'This shell script will attempt to install the Cisco Aironet'
echo 'Linux driver and utilities.'

# check and see if we are running as root or not
if [ "$UID" != "0" ]; then
echo "This script needs to be executed as root to operate properly."
exit 1
fi

# ACU now requires the existence of /opt/cisco for saving it's
# preferences file, and it expects the help web pages to be located
# in /opt/cisco/helpml

echo "The utilities require a base directory location of /opt/cisco/bin."
location="/opt/cisco/bin"
 
# 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.

if test -d $location ;then 
	if test -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 test -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 cwinstall."
		exit 1
	fi
fi

# attempt to copy the utilities from the unpacked archive to /opt/cisco/bin 
# 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

echo ""
echo "All utilities installed."
echo ""
# the utilities are now installed and usable by all users.

# install help files

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
	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 "Please press Enter to continue"
read dummy
clear
echo ""
echo "ACU now has a help system that uses a html browser you specify."
echo "By default, the browser to use is set to NULL."
echo "You may now specify a browser to use, or specify one later via ACU."
echo "If the browser is not in your path, a complete path will be required."
echo "Either type in the browser to use - or simply press Enter for none."
read browser
./utilities/installer --browser=$browser > /dev/null
clear
echo "Once the driver is installed - normally any user can run the utilities and"
echo "configure the radio.  If this is NOT desired - chmod the permissions of"
echo "the utilities to 500:"
echo -n '"' ; echo -n "chmod 500 $location/*"
echo '"'
# attempt to determine how many Ethernet cards are installed currently.
echo ""
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 "ethX.cfg" ]; then
	cp 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 of the driver/utility archive."
	exit 1
fi
echo "The Aironet card will be Ethernet adapter eth$next_adapter."
touch /tmp/._runpart2
sh ./cwinstall.2
