#! /bin/sh

# Installation of CAPI4Linux USB drivers, v2.2

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

prompt () {
	eval $3=\"$2\"
	echo -n "$1 [$2]: "
	read tmp
	if [ -n "$tmp" ]; then 
		eval $3=\"$tmp\"
	fi
}

ask_bool () {
	default=`eval echo '$'$2`
	if [ ! "$default" ]; then 
		default=n
	fi
	answer=""
	while [ "$answer" != "n" -a "$answer" != "y" ]; do
		prompt "$1" "$default" answer
	done
	eval "$2=$answer"
}

. /etc/rc.config
. /etc/rc.config.d/hotplug.rc.config
. ./install.card

# INIT: fc (FRITZ!Card) or fx (FRITZ!X)
name="${card#x}"
if [ "$name" != "$card" ]; then
	prefix=fx
else
	prefix=fc
fi

# 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$name" >> 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 "f.*us"  >> 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 -ne $? ]; 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 | tee -a install.log
	fi
	cp -vf i4l_hardware /etc/init.d | tee -a install.log
	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 | tee -a install.log
		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: Existiert /usr/share/doc/CAPI4Linux/...?
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 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 $prefix$name.o $LIBMOD/$ISDN | tee -a install.log
cp -vf usb.conf /etc/capi.conf | tee -a install.log
cp -vf install_passive-d.html $DOC/CAPI4Linux/de/install_passive.html | tee -a install.log
cp -vf install_passive-e.html $DOC/CAPI4Linux/en/install_passive.html | tee -a install.log

if [ "$loadfile" != "" ]; then
	# INIT: Copy card-specific hotplug script (fcusb2 only)
	echo "* Copying start script (fcusb2)" >> install.log
	mkdir -p $HOTPLUG/usb
	sed "s!#LOADFILE1#!$loadfile!"  fcusb2.start > tmp
	sed "s!#LOADFILE2#!$loadfile2!" tmp > $HOTPLUG/usb/fcusb2
	chmod 700 $HOTPLUG/usb/fcusb2
	rm tmp
fi

# INIT: Force removal of module on unplug event
ULDVAR=HOTPLUG_MODULES_TO_UNLOAD
HPCONFIG=/etc/rc.config.d/hotplug.rc.config

if ! echo $HOTPLUG_MODULES_TO_UNLOAD | grep "$prefix$name " > /dev/null; then
	echo "* Enabling 'removal on unplug'" >> install.log
	if ! test -f $HPCONFIG.old; then
		echo "Saving $HPCONFIG" >> install.log
		cp $HPCONFIG $HPCONFIG.old | tee -a install.log
	fi
	HMTU="$prefix$name $HOTPLUG_MODULES_TO_UNLOAD"
	echo "Modifying $HPCONFIG for $ULDVAR" >> install.log
	cat $HPCONFIG | sed "s/$ULDVAR.*/$ULDVAR=\"$HMTU\"/" > tmp
	cp -vf tmp $HPCONFIG >> install.log
	echo "* Value of $ULDVAR:" >> install.log
	echo "Current: \"$HOTPLUG_MODULES_TO_UNLOAD\"" >> install.log
	echo "Updated: \"$HMTU\"" >> install.log
	rm tmp
fi 

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)	if [ $kick -eq 1 ]; then
			echo -e "\n\aStarting CAPI4Linux..."
			echo "* Running insmod:" >> install.log
			log0=`wc -l < /var/log/messages`
			insmod capiutil   >> install.log 2>> install.log
			insmod kernelcapi >> install.log 2>> install.log
			insmod capifs     >> install.log 2>> install.log
			insmod capi       >> install.log 2>> install.log
			log1=`wc -l < /var/log/messages`
			echo "* Kernel log:" >> install.log
			tail -n `expr $log1 - $log0` /var/log/messages >> install.log
		fi
		;;
	*)	echo "Oops!"
		;;
esac

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

