#! /bin/sh

# Installation of CAPI4Linux drivers, v2.1

. /etc/rc.config
. ./install.card
prefix=fc

# INIT: Common definitions
DOC=/usr/share/doc
BIN=/usr/sbin
LIBMOD=/lib/modules/`uname -r`
ISDN=misc
PCMCIA=pcmcia-external
KERNEL=2.4.10-4GB

# TEST: Kernel version
kern=`uname -r`
cardname=`head -n 1 install.card | cut -b 3-`
if [ "$kern" != "$KERNEL" ]; then
	echo "Invalid kernel version... found $kern, expected: $KERNEL."
	exit 2
fi

echo -e "\n\n* Installation log (`date`):" >> install.log
echo "Kernel: $kern" >> install.log
echo "Card: $prefix$card" >> install.log
cat release.txt >> install.log

# INIT: Find archives...
LIBVER=`ls c4l-lib-* | cut -b9-18`
SYSVER=`ls c4l-sys-* | cut -b9-18`

echo "Libver: $LIBVER" >> install.log
echo "Sysver: $SYSVER" >> install.log
echo "Files:" >> install.log
ls -laR | grep -E -e "\..*:" -e "c4l-.*" -e "instal.*" -e "fc.*"  >> install.log 2>> install.log

# TEST: Am I root?
user=`id -u`
if [ "$user" != "0" ]; then
	echo -e "./install must be started by \"root\"."
	exit 1
fi

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

# TEST: New pppd, check version
pppd --version 2> tmp
cat tmp >> install.log
pver=`cat tmp | cut -d" " -f3`
if [ "$pver" != "2.4.1" ]; then
	echo "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: i4l available & active?
if [ "$I4L_START" == "yes" ]; then
	echo "* Installing new 'i4l_hardware'" >> install.log
        if ! test -f /etc/init.d/i4l_hardware.old; then
		mv -vf /etc/init.d/i4l_hardware /etc/init.d/i4l_hardware.old
	fi
	cp -vf i4l_hardware /etc/init.d
	chmod 744 /etc/init.d/i4l_hardware
	i4l=1
else
	echo "* Installing 'c4l_start'" >> install.log
	if ! test -f /etc/init.d/c4l_start; then
		cp -vf c4l_start /etc/init.d
		chmod 744 /etc/init.d/c4l_start
		ln -s /etc/init.d/c4l_start /etc/init.d/rc2.d/S03c4l
		ln -s /etc/init.d/c4l_start /etc/init.d/rc3.d/S03c4l
		ln -s /etc/init.d/c4l_start /etc/init.d/rc5.d/S03c4l
		ln -s /etc/init.d/c4l_start /etc/init.d/rc2.d/K44c4l
		ln -s /etc/init.d/c4l_start /etc/init.d/rc3.d/K44c4l
		ln -s /etc/init.d/c4l_start /etc/init.d/rc5.d/K44c4l
	fi
	i4l=0
fi

# TEST: Competing PCMCIA/USB installation?
uninstall() {
	if test -f $2/$1.o; then
		rm -v $2/$1.o >> install.log
	fi
}
uninstall fcpcmcia_cs $LIBMOD/$PCMCIA
uninstall fcusb  $LIBMOD/$ISDN
uninstall fcusb2 $LIBMOD/$ISDN
uninstall fxusb	 $LIBMOD/$ISDN			

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

echo Copying CAPI drivers...
echo "* Running rpm" >> install.log
rpm --upgrade --force c4l-sys-$SYSVER-1.i386.rpm 2>> install.log
echo Copying CAPI tools...
rpm --upgrade --force c4l-lib-$LIBVER-1.i386.rpm 2>> install.log
echo Copying driver module for $cardname...
echo "* Copying driver, conf file and documentation" >> install.log
if ! test -d $LIBMOD/$ISDN; then
	echo "* Creating directory $LIBMOD/$ISDN" >> install.log
	mkdir -p $LIBMOD/$ISDN
fi
cp -vf fc$card.o $LIBMOD/$ISDN

if test -f /etc/capi.conf; then
	if ! cat /etc/capi.conf | grep ^fcdsl > /dev/null 2>&1; then
		cat $card.conf >> /etc/capi.conf
	fi
else
	cp -vf $card.conf /etc/capi.conf
fi
cp -vf install_passive-d.html $DOC/CAPI4Linux/de/install_passive.html
cp -vf install_passive-e.html $DOC/CAPI4Linux/en/install_passive.html

# Call card-specific finalizer...
finalize

# INIT: Running depmod -a:
echo "* Running depmod:" >> install.log
depmod -a 2>> install.log

# Call archive-specific finalizer...
if test -f scripts/final; then
	scripts/final $prefix$name
fi
 
case $i4l in
	1)	
		echo -e "\a\nCheck whether ISDN4Linux is set to AVM B1;"
		echo -e "if not, do so now and reboot the system...\n"
		;;
	0)	if [ "$kick" == "1" ]; then
			echo -e "\n\aStarting CAPI4Linux..."
			echo "* capiinit start:" >> install.log
			log0=`wc -l < /var/log/messages`
			$BIN/capiinit start
			log1=`wc -l < /var/log/messages`
			tail -n `expr $log1 - $log0` /var/log/messages >> install.log
		fi
		echo ""
		;;
	*)	echo "Oops!"
		;;
esac

echo -e "* Leaving install script\n" >> install.log

