#! /bin/sh
#
# Installation of CAPI4Linux drivers & tools, v200301301400
# (PCI/ISA, ISDN)

DEBUG=0
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/pcmcia-external
ICONFIG=/etc/sysconfig/isdn
HPCONFIG=/etc/sysconfig/hotplug
PCCONFIG=/etc/sysconfig/pcmcia
ESC=`echo -en "\033"`
RED="${ESC}[1;31m"
NORM=`echo -en "${ESC}[m\017"`
KERNEL=2.4.19-4GB
LOGFILE=install.log

# INIT: Logfile handling

echo1() {
	# Normal log message. Copy to console in debug mode...
	echo -e "$1" >> $LOGFILE
	if [ $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}"
}
										
# TEST: Tools

firmware_version () {
	# Firmware version
	#
	local tmp
	#
	tmp=`cat $1 | tr -c "[:print:]" "\n" | grep -E "3<?\.[0-9<]*.*[\.\-][0-9<]*" | tr -d -c "0-9.\-\n"`
	#
	if test -z "$tmp"; then
		return 1
	fi
	if ! test -z "$2"; then
		eval "$2=$tmp"
	fi
	return 0
}

# TEST: Kernel version
kern=`uname -r`
if [ "$kern" != "$KERNEL" ]; then
        echo2 "Invalid kernel version... found $kern, expected: $KERNEL."
        exit 2
fi

line=`ls insta* | tr " a-z_.\n" [-*]`
echo1 "$line\n* Installation log (`date`):"
echo1 "Kernel: $kern"
cat release.txt >> $LOGFILE
 
# TEST: Am I root?
user=`id -u`
if [ "$user" != "0" ]; then
	echo ""
	echo2 "./install has to be started by \"root\"."
	exit 4
fi

# INIT: Get controller data & check installation
. ./ctrldata
cf=0
if ls $ICONFIG/cfg-contr* > /dev/null 2>&1; then
	for cfg in $ICONFIG/cfg-contr*; do
		cn=`cat $cfg | grep NAME= | cut -d= -f2 | tr -d "\""`
		cb=${cn#$CTRL}
		if [ "$cn" != "$cb" ]; then
			echo1 "Found $cn in $cfg"
			cf=$((cf + 1))
		fi
	done
fi
if [ $cf -eq 0 ]; then
	echo ""
	echo "No AVM ISDN controller $UTYPE found..."
	echo2 "You must first install your controller with YaST!"
	exit 5
fi

# INIT: Find & check firmware files...
FWCHK=0
FWUPD=0
ix=1
while [ $ix -le $FWMAX ]; do
	fwn=$FWDIR/${FWFILE[ix]}
	if test -f $fwn; then
		if ! firmware_version $fwn fwv; then
			echo1 "Version of $fwn unknown"
			fwv=0
		fi
		fwv1=`echo $fwv | tr -d ".\-"`
		fwv2=`echo ${FWVERS[ix]} | tr -d ".\-"`
		if [ $fwv1 -eq $fwv2 ]; then
			echo1 "$fwn: ok"
			FWCHK=$((FWCHK + 1))
		else
			echo1 "$fwn: have $fwv, want ${FWVERS[ix]}"
			cp -vf $fwn $fwn-$fwv >> $LOGFILE
		fi
	fi
	ix=$((ix + 1))
done
if [ $FWCHK -lt $FWMAX ]; then
	echo2 "At least one firmware file needs to be updated."
	FWUPD=1
fi

# INIT: Find archives...
LIBVER=`ls c4l-lib-* | cut -b9-18`
DRVVER=`ls c4l-$TYPE-* | cut -b8-17`
echo1 "* rpm versions: lib $LIBVER, drv $DRVVER"

# TEST: Does pppd exist?
which pppd > /dev/null
if [ 0 -ne $? ]; then
	echo ""
	echo2 "pppd has not been installed!"
	exit 5
fi

# TEST: New pppd, check version
pppd --version 2> tmp
cat tmp >> $LOGFILE
pver=`cat tmp | cut -d" " -f3`
if [ "$pver" != "2.4.1" ]; then
	echo ""
        echo2 "This installer has only been tested for pppd 2.4.1!"
        exit 3
fi

# TEST: Does /dev/capi exist?
if ! test -d /dev/capi; then
        mkdir /dev/capi
fi

# TEST: Does /usr/share/doc/CAPI4Linux/ exist...?
if ! test -d $DOCDIR/CAPI4Linux/de; then
        mkdir -p $DOCDIR/CAPI4Linux/de
        mkdir -p $DOCDIR/CAPI4Linux/en
fi
echo1 "* $DOCDIR/CAPI4Linux ok"

echo2 "Copying CAPI driver..."
echo1 "* Running rpm"
rpm --upgrade --force c4l-$TYPE-$DRVVER-1.i386.rpm 2>> $LOGFILE
echo2 "Copying CAPI tools..."
rpm --upgrade --force c4l-lib-$LIBVER-1.i386.rpm 2>> $LOGFILE
echo1 "* Copying documentation"
cp -vf install_active-d.html $DOCDIR/CAPI4Linux/de/install_active.html
cp -vf install_active-e.html $DOCDIR/CAPI4Linux/en/install_active.html
echo1 "* Running depmod:"
depmod -a 2>> $LOGFILE

# TASK: Call card-specific finalizer
finalize

# TASK: Call archive-specific finalizer...
if test -f scripts/final; then
        scripts/final
fi

echo2 ""
echo2 "New driver files have been installed, please reboot or restart"
echo2 "the ISDN package in order to activate the changes."

echo1 "* Leaving install script"

