#! /bin/sh

# Vorlufiges Skript zur Installation von CAPI4Linux (binr)
# (2.2.18 / 2.4.0-4GB)

. /etc/rc.config
. ./install.card

# INIT: Allgemeine Definitionen
DOC=/usr/share/doc
OLDBIN=/sbin
BIN=/usr/sbin

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

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

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

# TEST: i4l vorhanden & aktiv?
if [ "$I4L_START" == "yes" ]; then
        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
	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: Existiert /usr/share/doc/CAPI4Linux/...?
if ! test -d $DOC; then
	mkdir $DOC
fi
if ! test -d $DOC/CAPI4Linux; then
	mkdir $DOC/CAPI4Linux
	mkdir $DOC/CAPI4Linux/de
	mkdir $DOC/CAPI4Linux/en
fi

# TEST: Kopien der CAPI-Tools in /sbin?
if test -f $OLDBIN/capiinit; then
	echo -e "\a\nPrograms 'capiinit' and 'avmcapictrl' have been moved!"
	echo -e "Please enter 'rehash' (or 'hash -r', respectively) after"
	echo -e "the installation.\n"
	rm -f $OLDBIN/capiinit
	rm -f $OLDBIN/avmcapictrl
fi

# INIT: Dual-Kernel-Dilemma (1)...
if test ! -f /etc/modules.conf.old; then
	cp -vf /etc/modules.conf /etc/modules.conf.old
fi
cat /etc/modules.conf | grep "CAPI4Linux patch" > /dev/null
if [ 0 -ne $? ]; then
	sed -f mcpatch.sed /etc/modules.conf.old | cat - mcpatch > /etc/modules.conf
	echo "CAPI4Linux patch applied to /etc/modules.conf!"
else
	echo "CAPI4Linux patch for /etc/modules.conf is already installed!"
fi

# INIT: Dual-Kernel-Dilemma (2)...
# Z wie in zwo-Zwo
# V wie in zwo-Vier
Z=2.2.18
V=2.4.0-4GB

echo Kopiere CAPI-Treiber...
rpm --upgrade --force c4l-sys-2001.03.08-1.i386.rpm 2>  install.log
echo Kopiere CAPI-Tools...
rpm --upgrade --force c4l-lib-2001.01.15-1.i386.rpm 2>> install.log
cp -vf $Z/fc$card.o  /lib/modules/$Z/misc
cp -vf $V/fc$card.o  /lib/modules/$V/misc
cp -vf $card.conf /etc/capi.conf
cp -vf install_passive.de $DOC/CAPI4Linux/de/install_passive.txt
cp -vf install_passive.en $DOC/CAPI4Linux/en/install_passive.txt
depmod -q -a $Z 2>> install.log 
depmod -q -a $V 2>> install.log

# Aufruf der karten-spezifischen finalize-Methode...
finalize
 
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..."
			$BIN/capiinit start
		fi
		echo ""
		;;
	*)	echo "Oops!"
		;;
esac

