#! /bin/sh
#
# Installation of CAPI4Linux drivers & tools, v20030813
# (FRITZ!Card DSL SL on SuSE 8.2)

# 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
MODDIR=$LIBMOD/misc
PCMCIADIR=$LIBMOD/misc
ICONFIG=/etc/sysconfig/isdn
LOGFILE=install.log
LOADER=load-fcdslsl

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

# STEP 2: What has been copied?
#
STEP=2
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 3: Copying my stuff...
#
STEP=3
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 ! 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 4: Create boot service
#
STEP=4
echos "Installing boot service..."
if ! test -f $INITDIR/$LOADER; then
	cp -vf $LOADER $INITDIR >> $LOGFILE
	insserv -d $LOADER >> $LOGFILE
else
	echos "Boot service exists."
fi

# STEP 5: Check presence of pppd
#
STEP=5
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 6: Final tasks
#
STEP=6
if ! finalize; then
	echos "Card finalizer failed."
	exit $err_final
fi
echo ""
echo2 "The driver files have been copied. Starting the driver..."
echo ""
$INITDIR/$LOADER start
echos "Loader returned $?"

echo""
drdsl
echos "drdsl returned $?"

