#! /bin/sh

# Skript zur Installation von CAPI4Linux/USB (binr), v2.0
# (2.4.4-4GB)

# INIT: Allgemeine Definitionen
DOC=/usr/share/doc
OLDBIN=/sbin
BIN=/usr/sbin
USBMGR=/etc/usbmgr
KERNEL=2.4.4-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
. ./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: 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 -ne $? ]; then
	echo "pppd has not been installed!"
	exit 2
fi

# TEST: Existiert usbmgr?
which usbmgr > /dev/null
if [ 0 -ne $? ]; then
	echo "usbmgr has not been installed!"
	exit 3
fi
echo "* `usbmgr -V`" >> install.log

# TEST: Host controller checks
keep=0
changed=0
if [ "$1" != "" ]; then
	if [ "$1" == "-k" ]; then
		echo "Option -k accepted." >> install.log
		keep=1
	else
		echo "Option $1 irgnored." >> install.log
		echo "Command line option ignored."
	fi
fi
hc=`lsmod | grep "^[usb-]*[uo]hci" | cut -d" " -f1`
hcused=0
if [ "$hc" == "" ]; then
	echo "Could not find a host controller driver!"
	exit 4
else
	hcused=`lsmod | awk '/^(usb-)?[uo]hci/ { print $3 }'` 
fi

echo "* Host controller driver: $hc" >> install.log
echo "* Host controller use count: $hcused" >> install.log

if [ $keep -eq 0 ]; then
	case "$hc" in
	"usb-ohci"|"uhci")
		;;
	"usb-uhci")
		hcyn=y
		echo -e "\n\aThe driver for $cardname seems to work better with"
		echo -e "host controller driver \"uhci\". Currently, the other driver"
		echo -e "(\"usb-uhci\") is loaded at boot time... The installer can change" 
		echo -e "this setting in order to load \"uhci\" instead.\n"
		ask_bool "Do you want to change the host controller driver? (y/n) " hcyn
		echo -n "* usb-uhci --> uhci: " >> install.log
		if [ "$hcyn" == "y" ]; then
			echo "uhci" > /etc/usbmgr/host
			changed=1
			if [ $hcused -eq 0 ]; then
				rmmod usb-uhci
				insmod uhci
				changed=0
				hc=uhci
				echo -e "\nThe host controller driver has been changed.\n"
				echo -e -n "done\nlsmod: " >> install.log
				lsmod | grep "uhci" >> install.log
			else
				echo -e "\nThe host controller driver is in use! In order to activate\n"
				echo -e "host controller driver \"uhci\", a reboot is required...\n"
				echo "installed" >> install.log
			fi
		else
			echo -e "\nThe host controller driver has not been changed.\n"
			echo "rejected" >> install.log
		fi
		;;
	*)
		echo "Could not find a host controller driver!"
		exit 5
		;;
	esac
else
	echo "Keeping host controller driver \"$hc\"."
fi

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

# TEST: i4l vorhanden & aktiv?
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: 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
echo "* $DOC/CAPI4Linux ok" >> install.log

# 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
echo "* CAPI tools ok" >> install.log

# INIT: USB-Device-Datenbank
udb=$USBMGR/usbmgr.conf
line="vendor 0x57c product 0x$cardid"
if [ "$loadfile" != "" ]; then
	line="$line script c4l_usbstart module $prefix$name"
	line="$line\nvendor 0x57c product 0x$cardid2 script c4l_usbstart"
	sed "s!#LOADFILE1#!$loadfile!" c4l_usbstart > tmp
	sed "s!#LOADFILE2#!$loadfile2!" tmp > $USBMGR/c4l_usbstart
	chmod 744 $USBMGR/c4l_usbstart
	rm tmp
fi
line="$line module $prefix$name"
if test ! -f $USBMGR/usbmgr.conf.old; then
	cp -vf $udb $USBMGR/usbmgr.conf.old
fi

echo "* Starting USB device filesystem update..." >> install.log
echo "  (Output of ls -laR /etc/usbmgr/vendor/057c)" >> install.log
echo "* Before update:" >> install.log
ls -laR /etc/usbmgr/vendor/057c >> install.log 2>> install.log

echo "Updating USB device database..."
grep "CAPI4Linux patch" $udb > /dev/null
if [ 0 -eq $? ]; then
	if test -d $USBMGR/vendor/057c; then
		(cd $USBMGR/vendor; rm -r 057c)
	fi
	cat $udb | sed -f ucpatch.sed - > $udb.tmp
	rm $udb
	mv $udb.tmp $udb
	update_usbdb -f $udb
	echo "* After removing old entries:" >> install.log
	ls -laR /etc/usbmgr/vendor/057c >> install.log 2>> install.log
fi
echo -e "\n# CAPI4Linux patch for $cardname" >> $udb 
echo -e "$line\n# End of CAPI4Linux patch"   >> $udb
update_usbdb -f $udb
echo "* After adding entries for $prefix$name:" >> install.log
ls -laR /etc/usbmgr/vendor/057c >> install.log 2>> install.log

echo "* Content of installed file(s):" >> install.log
for file in /etc/usbmgr/vendor/057c/$cardid/*; do
	echo "File $file:" >> install.log
	cat $file >> install.log
done
if [ "$cardid2" != "" ]; then
	for file in /etc/usbmgr/vendor/057c/$cardid2/*; do
		echo "File $file:" >> install.log
		cat $file >> install.log
	done
fi

# INIT: Kopieren! Endlich... ;-)
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
cp -vf $prefix$name.o  /lib/modules/$KERNEL/misc
cp -vf usb.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 "* Running depmod:" >> install.log
depmod -q -a 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..."
			echo "* Running insmod (for CAPI modules):" >> 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 (for CAPI modules):" >> install.log
			tail -n `expr $log1 - $log0` /var/log/messages >> install.log
		fi
		;;
	*)	echo "Oops!"
		;;
esac

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

