#! /bin/sh

# Installation of CAPI4Linux PCMCIA 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=kernel/drivers/isdn/fritz
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 3
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 USB installation?
uninstall() {
	if test -f $2/$1.o; then
		rm -v $2/$1.o >> install.log
	fi
}
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

# INIT: Copying files... FINALLY! ;-)
echo "* Running rpm" >> install.log
echo Copying CAPI drivers...
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 modules 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 fcpcmcia.o         $LIBMOD/$ISDN
if ! test -d $LIBMOD/$PCMCIA; then
        echo "* Creating directory $LIBMOD/$PCMCIA" >> install.log
        mkdir -p $LIBMOD/$PCMCIA
fi
cp -vf fcpcmcia_cs.o      $LIBMOD/$PCMCIA
cp -vf pcmcia.conf        /etc/capi.conf
cp -vf install_passive-d.html $DOC/CAPI4Linux/de/install_passive.html
cp -vf install_passive-e.html $DOC/CAPI4Linux/en/install_passive.html
echo "* Copying PCMCIA config files" >> install.log
cp -vf avm                /etc/pcmcia
cp -vf avm.opts           /etc/pcmcia
if ! test -f /etc/pcmcia/avm.conf.old; then
	mv -vf /etc/pcmcia/avm.conf /etc/pcmcia/avm.conf.old
fi
cp -vf avm.conf           /etc/pcmcia

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

# INIT: Call card-specific finalizer...
finalize

# 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)	
		echo -e "\n\aPlease reboot (required by the PCMCIA CardServices)\n"
		;;
	*)	
		echo "Oops!"
		;;
esac

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

