#! /bin/sh
#
# Installation of CAPI4Linux drivers & tools, v20030612
# (PCI/PnP/ISA/PCMCIA/USB, ISDN/DSL)

# INIT: Common definitions
#
ESC=`echo -en "\033"`
RED="${ESC}[1;31m"
NORM=`echo -en "${ESC}[m\017"`
NOT[0]=" "
NOT[1]="not "
TGZVER1=2.4.20-4GB
TGZVER2=2.4.20-64GB-SMP
INITDIR=/etc/init.d
LIBMOD=/lib/modules/`uname -r`
DOCDIR=/usr/share/doc
SBINDIR=/usr/sbin
FWDIR=/usr/lib/isdn
EPDIR=/etc/pcmcia
MODDIR=$LIBMOD/misc
PCMCIADIR=$LIBMOD/misc
BLACKLIST=/etc/hotplug/blacklist
HPUSBDIR=/etc/hotplug/usb
ICONFIG=/etc/sysconfig/isdn
HPCONFIG=/etc/sysconfig/hotplug
PCCONFIG=/etc/sysconfig/pcmcia
LOGFILE=install.log
BLACKFLAG=0

# INIT: Define some return codes for this script
#
no_error=0		# Documentation follows... ;-)
err_hisax=1		# Running on hisax
err_check=1		# Invalid CHECK1 value
err_ready=2		# Exit for YaST preparation
err_pver=3		# pppd version != 2.4.1
err_init=4		# card initializer failed
err_inst=5		# card install failed
err_final=6		# card finalizer failed
err_kver=7		# invalid kernel version
err_root=8		# Invalid user id (!= root)
		
# INIT: Include card specific stuff
#
. ./install.card
kern=`uname -r`
cardname=`head -n 1 install.card | cut -b 3-`
name="${card#x}"
if [ "$name" != "$card" ]; then
	prefix=fx
else
	prefix=fc
fi
module=$prefix$name

# INIT: Setting up log file (see: variable LOGFILE)
#
opt_debug=0		# Run script in debug mode

echo1() {
	# Normal log message. Copy to console in debug mode...
	echo -e "$1" >> $LOGFILE
	if [ $opt_debug -eq 1 ]; then
		echo -e "$1"
	fi
}

echo2() {
	# Normal message, written to log and console
	echo -n "console: " >> $LOGFILE
	echo -e "$1" | tee -a $LOGFILE
}

echo3() {
	# Warning message, written to log and console (in red)
	echo "warning: $1" >> $LOGFILE
	echo -e "\a${RED}$1${NORM}"
}

echos() {
	# Normal log messages, step info.
	echo -e "STEP $STEP: $1" >> $LOGFILE
	if [ $opt_debug -eq 1 ]; then
		echo -e "STEP $STEP: $1"
	fi
}	

user=`id -u`
if [ "$user" != "0" ]; then
	echo2 "$0 must be started by \"root\"."
	exit $err_root
fi

line=`ls insta* | tr " a-z0-9_.\n" [-*]`
echo1 "$line\n* Installation log (`date`):"
echo1 "Kernel: $kern"
echo1 "Card: $prefix$name"
cat release.txt >> $LOGFILE

# STEP 0: Parsing the command line...
#
STEP=0
opt_do_rpm=0		# ALLWAYS unpack rpms
opt_compile=0		# Compile prior to installation
opt_identify=0		# Print info about driver being installed
opt_force_cp=0		# ALLWAYS copy driver file
opt_check_kernel=1	# Check for correct kernel verion
opt_check_pppd=1	# Check for correct pppd version
#
opt_string=""
opt_count=0
opt_ok=0

while [ "$1" != "" ]; do
	opt_ok=1
	case $1 in

	-c)	opt_compile=1
		;;
	-d)	opt_debug=1
		;;
	-f)	opt_force_cp=1
		;;
	-i)	opt_identify=1
		;;
	-m)	opt_do_rpm=1
		;;
	-p)	opt_check_pppd=0
		;;
	*)	opt_ok=0
		echo3 "Unrecognized option: $1"
		;;
	esac
        if [ $opt_ok -eq 1 ]; then
		opt_string="$opt_string $1"
		opt_count=$((opt_count + 1))
	fi
	shift
done
echos "$opt_count accepted options: $opt_string"

if [ $opt_identify -eq 1 ]; then
	echo -e "Installing $module.o (CAPI driver for $cardname)"
fi
						
# STEP 1: What is running?
#
STEP=1

check_mod () {
	# 1st arg: Name of module to check
	# ... arg: Subsequent names to check
	#
	local num=0
	local nok=0
	while [ "$1" != "" ]; do
		num=$((num + 1))
		if lsmod | grep "^$1" > /dev/null 2>&1; then
			echos "Module $1 found."
			nok=$((nok + 1))
		else
			echos "Module $1 not found."
		fi
		shift
	done
	return $((num==nok ? 0 : 1))
}

K=0
if [ $opt_check_kernel -eq 1 ]; then
	if [ "$kern" != "$TGZVER1" ]; then
		if [ "$kern" != "$TGZVER2" ]; then
			echo2 "Invalid kernel version: $kern"
			exit $err_kver
		else
			K=2
		fi
	else
		K=1
	fi
fi
echo1 "Module selector K: $K"

# CHECK = 0	Neither I4L nor CAPI found
#	= 1	I4L found
#	= 2	I4L + CAPI drivers found
#	= 3	I4L + CAPI drivers + Card driver found
#
CHECK1=0
if check_mod isdn; then
	if check_mod slhc; then
		CHECK1=1
		if check_mod hisax; then
			echo2 ""
			echo2 "Your system has been configured to use the Hisax driver."
			echo2 "In order to use the CAPI driver, you should change your"
			echo2 "driver configuration in YaST and start this installation"
			echo2 "again..."
			echo2 ""
			exit $err_hisax
		fi
		if check_mod $module; then
			if check_mod capi capifs capiutil kernelcapi; then
				CHECK1=3
			else
				echo2 ""
				echo2 "How could you load $module.o without CAPI modules?"
				echo2 ""
				exit $err_nocapi
			fi
		else
			if check_mod capi capifs capiutil kernelcapi; then
				CHECK1=2
			fi
		fi
	fi
fi
echos "Result: $CHECK1"

# STEP 2: What has been installed?
#
STEP=2

# CHECK = 0	This archive's driver is not installed
#	= 1	... is installed
#
CHECK2=0
NCCTRL=0
if ls $ICONFIG/cfg-contr[0-9] > /dev/null 2>&1; then
	for cfg in $ICONFIG/cfg-contr[0-9]; do
		drv=""
		while read line; do
			case $line in

			"DRIVER="*)	drv=`echo $line | cut -d= -f2 | tr -d "\""`
					;;
			*)		;;
			esac
		done < $cfg
		if ! test -z "$drv"; then
			echos "Found driver $drv."
			if [ "$drv" == "$module" ]; then
				CHECK2=1
			fi
			case $drv in

			"fc"*|"fxusb")	NCCTRL=$((NCCTRL + 1))
					;;
			*)		;;
			esac
		else
			echos "File $cfg seems to lack an DRIVER entry!"
		fi
	done
fi
echos "Result: $CHECK2 ($NCCTRL known driver(s))"

# STEP 3: What's been copied?
#
STEP=3
C4LRPM=1
if rpm -q capi4linux > /dev/null 2>&1; then
	echos "SuSE RPM 'capi4linux' found."
	C4LRPM=0
fi
if [ $opt_do_rpm -eq 1 ]; then
	echos "Option override: C4LRPM=1 (was $C4LRPM)"
	C4LRPM=1
fi
if ! test -f $MODDIR/$module.o; then
	echos "$MODDIR/$module.o does not exist."
	C4LDRV=1
elif test $module$K.o -nt $MODDIR/$module.o; then
	echos "$MODDIR/$module.o needs to be updated."
	C4LDRV=1
else	
	if [ $opt_force_cp -eq 1 ]; then
		echos "Forced update of $module.o!"
	else
		echos "File $module.o does not need an update."
	fi
	C4LDRV=$opt_force_cp
fi

# STEP 4: What needs to be installed?
#
STEP=4
ready=0
if [ $CHECK2 -eq 0 ]; then
	echos "Controller is not known by YaST."
	case $CHECK1 in

	0)	echo2 ""
		echo2 "Please activate ISDN and configure your controller in YaST!"
		echo2 "Then, restart this installation..."
		echo2 ""
		ready=1
		;;
	1)	echo2 ""
		echo2 "No CAPI controller has been configured. Please use YaST to"
		echo2 "install your controller and restart this installation..."
		echo2 ""
		ready=1
		;;
	2)	echo2 ""
		echo2 "The CAPI kernel modules are available but $cardname"
		echo2 "is not known by YaST. Please configure the controller in YaST"
		echo2 "and restart this installation..."
		if [ $NCCTRL -gt 0 ]; then
			echo2 ""
			echo2 "Note: You can not install more than one CAPI controller."
		fi
		echo2 ""
		ready=1
		;;
	3)	echo2 ""
		echo2 "The driver for $cardname is running, but it seems that it"
		echo2 "was not configured with YaST. (Installation continues...)"
		;;
	*)	echos "Illegal CHECK1 value!"
		exit $err_check1
		;;
	esac
else
	echos "Controller has been configured by YaST."
fi
if [ $ready -eq 1 ]; then
	exit $err_ready
fi

# STEP 5: Copying my stuff...
#
STEP=5
LIBVER=`ls c4l-lib-* | cut -b9-18`
echos "Libver: $LIBVER"
echos "Sysver: obsolete"

if ! initialize; then
	echos "Card initializer failed."
	exit $err_init
fi
if [ $C4LDRV -eq 1 ]; then
	if ! test -d $MODDIR; then
		echo1 "Creating $MODDIR"
		mkdir -p $MODDIR 2>> $LOGFILE
	fi
	if [ $K -gt 0 ]; then
		cp -vf $module$K.o $MODDIR/$module.o >> $LOGFILE
		echo1 "Running depmod -a"
		depmod -a >> $LOGFILE 2>> $LOGFILE
	else
		echo2 "Please copy the driver module manually to $MODDIR..."
	fi
fi
if [ $C4LRPM -eq 1 ]; then
	if rpm -q capi4linux > /dev/null 2>&1; then
		rpm -e --nodeps capi4linux 2>> $LOGFILE
	fi
	rpm -U -vh --nodeps c4l-lib-$LIBVER-1.i386.rpm 2>> $LOGFILE
fi

if [ $BLACKFLAG -ne 0 ]; then
	if ! grep $module $BLACKLIST > /dev/null 2>&1; then
		echo1 "Adding $module to $BLACKLIST"
		{
			echo ""
			echo "# $cardname (loaded by ISDN subsystem)"
			echo $module
		} >> $BLACKLIST
	fi
fi

if ! test -d $DOCDIR/CAPI4Linux/de; then
	echo1 "Creating $DOCDIR/CAPI4Linux/(de/en)"
	mkdir -p $DOCDIR/CAPI4Linux/de
	mkdir -p $DOCDIR/CAPI4Linux/en
fi
cp -vf install_passive-d.html $DOCDIR/CAPI4Linux/de/install_passive.html >> $LOGFILE
cp -vf install_passive-e.html $DOCDIR/CAPI4Linux/en/install_passive.html >> $LOGFILE
for lf in $loadfile1 $loadfile2 x; do
	if [ "$lf" != "x" ]; then
		cp -vf $lf $FWDIR >> $LOGFILE
	fi
done
if ! install_card; then
	echos "Card install failed."
	exit $err_inst
fi

# STEP 6: Check presence of pppd
#
STEP=6
echos "Checking pppd..."
if ! which pppd > /dev/null; then
	echo3 "pppd has not been installed!"
	echo2 "If you want to use pppd to connect to the Internet, please"
	echo2 "install the ppp package!"
fi

pppd --version 2> tmp
cat tmp >> $LOGFILE
if [ $opt_check_pppd -eq 1 ]; then
	pver=`cat tmp | cut -d" " -f3`
	if [ "$pver" != "2.4.1" ]; then
		echo3 "Version 2.4.1 of pppd is required!"
		exit $err_pver
	fi
fi
rm tmp

# STEP 7: Final tasks
#
STEP=7
echo2 ""
echo2 "The driver files have been updated. Reboot or restart the ISDN"
echo2 "package in order to activate the changes..."
if ! finalize; then
	echos "Card finalizer failed."
	exit $err_final
fi

