#! /bin/sh

. /etc/rc.config

CARD=$1
DDI=0
DDIX=0
TDDI1=0
TDDI2=0
TDDI3=0
TDDI4=0
TFILE=dss1
TPROTO=dss1
CARDNO=0
LFILE=
LPROTO=
LOPTIONS=
LIOADDR=
LIRQ=
DN1=DN
SPID1=SPID
DN2=DN2
SPID2=SPID2

DOC=/usr/share/doc
SBIN=/usr/sbin
INIT=/etc/init.d
KERNEL=2.4.10-4GB

# TEST: Kernel version
kern=`uname -r`
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 "Extracted from install_active.txt:" >> install.log
echo -e "-`head -n 11 install_active.de | grep Build`" >> install.log
 
. ./controller

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

# TEST: Card type available & valid?
valid $1
if [ $# -eq 0 -o $? -ne 0 ]; then
	echo -e "\a\nMissing or unknown controller name: $1\n"
	expected
	exit 2
fi
echo "- Card: $1" >> install.log

# TEST: Dialog required?
echo "* Setting installation parameters" >> install.log
if [ $# -gt 1 ]; then
	while [ $2 ]; do 
		case $2 in
		-noddi)	DDI=0
			TDDI1=0
			TDDI2=0
			TDDI3=0
			TDDI4=0
			;;
		-ddi)	DDI=1
			TDDI1=1
			TDDI2=1
			TDDI3=1
			TDDI4=1
			;;
		-ddi1)	TDDI1=1
			;;
		-ddi2)	TDDI2=1
			;;
		-ddi3)	TDDI3=1
			;;
		-ddi4)	TDDI4=1
			;;
		-dss1)	TFILE=dss1
			TPROTO=dss1
			;;
		-ni-1)	TFILE=ni-1
			TPROTO=ni-1
			;;
		-1tr6)	TFILE=1tr6
			TPROTO=1tr6
			;;
		-help)
			echo ""
			options
			exit 1
			;;
		*)	echo -e "\a\nInvalid parameter: $2\n"
			options
			exit 1
			;;
		esac
		shift
	done
else
	which dialog > /dev/null
	if [ $? -ne 0 ]; then
		. ./query
	else
		. ./query-d
	fi
fi

# CHECK: Check card type and set parameters
. ./select

# INIT: Find archives...
LIBVER=`ls c4l-lib-* | cut -b9-18`
DRVVER=`ls c4l-$TYPE-* | cut -b8-17`
echo -e "* rpm versions:\n  lib $LIBVER\n  drv $DRVVER" >> install.log

# TEST: select successful?
if [ $ok -eq 0 ]; then
	exit 2
fi

for NUM in 1 2 3 4; do
	eval "DUMMY=\$TDDI$NUM"
	if [ $DUMMY -eq 1 ]; then
		echo "* DDI set for controller $NUM" >> install.log
		if [ $NUM -le $TDDIN ]; then
			DDIX=1
		else
			echo -e "\aWarning: Options for controller $NUM!"
		fi
	fi
done

# TEST: DDI possible?
if [ $DDIX -eq 1 -a $HAVE_DDI -eq 0 ]; then
	echo "* DDI requested but not available" >> install.log
	echo -e "\a\nPoint-to-point BRI is not supported!\n"
	exit 3
fi

# PATCH: Setting firmware, protocol, etc. 
cline1=`echo "$cline1" | sed -e "s/%P/$LPROTO/"  -e "s/%F/$LFILE/"`
cline1=`echo "$cline1" | sed -e "s/%I/$LIOADDR/" -e "s/%V/$LIRQ/"`
if [ "$noopt" == "" ]; then
	cline1="$cline1    $LOPTIONS"
fi
if [ "$cline2" != "" ]; then
	cline2=`echo "$cline2" | sed -e "s/%P/$LPROTO/"`
	if [ "$noopt" == "" ]; then
		cline2="$cline2    $LOPTIONS"
	fi
fi
echo "Firmware: $LFILE"
echo "Protocol: $LPROTO"
echo "Options:  $LOPTIONS"

# TEST: Does pppd exist?
which pppd > /dev/null
if [ 0 -ne $? ]; then
	echo "pppd has not been installed!"
	exit 5
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: Hiow many cards have been installed?
if ! test -f /proc/capi/controller; then
	ncards=0
	pcmcia=0
else
	ncards=`cat /proc/capi/controller|cut -b23-39|uniq|wc -l|cut -b7`
	pcmcia=`cat /proc/capi/controller|cut -b23-39|uniq|grep pcmcia|wc -l|cut -b7`
fi
echo "* System status:" >> install.log
echo -e "  Installed controllers: $ncards\n  PCMCIA controllers:    $pcmcia" >> install.log

# TEST: How many cards have been registered?
if ! test -f /etc/capi.conf; then
	rcards=0
else
	rcards=`cat /etc/capi.conf|awk '/^[^#]/ { if (($1=="b1pcmcia")||($2!="-")) print $2 }'|wc -l|cut -b7`
fi
echo "  capi.conf controllers: $rcards" >> install.log

if [ $ncards -gt 3 ]; then
	echo -e "\a\nCan not install more than four controllers!\n"
	cat /proc/capi/controller
	echo ""
	exit 6
fi

if [ $ncards -eq 1 ]; then
	echo "One controller found."
else
	echo "$ncards controllers found."
fi
if [ $rcards -eq 1 ]; then
	echo "One controller known by CAPI4Linux."
else
	echo "$rcards controllers known by CAPI4Linux."
fi

if [ $rcards -gt 0 ]; then
	if [ $pcmcia -ne 0 ]; then
		echo -e -n "\a\nThere is/are $pcmcia PCMCIA controller(s) "
		echo -e -n "active.\nIt is necessary to momentarily "
		echo -e -n "deactivate CAPI4Linux. This is\nnot possible "
		echo -e -n "as long as a PCMCIA controller is inserted.\n" 
		echo -e "Please remove it and restart ./install.\n"
		exit 0
	fi
	echo "Deactivating CAPI4Linux..."
	echo "* capiinit stop" >> install.log
	capiinit stop
fi

# TEST: i4l available & active?
if [ "$I4L_START" == "yes" ]; then
        echo "* Installing new 'i4l_hardware'" >> install.log
	if ! test -f $INIT/i4l_hardware.old; then
	        mv -vf $INIT/i4l_hardware $INIT/i4l_hardware.old
	fi
        cp -vf i4l_hardware $INIT
        chmod 744 $INIT/i4l_hardware
        i4l=1
else
        echo "* Installing 'c4l_start'" >> install.log
	if ! test -f $INIT/c4l_start; then
		cp -vf c4l_start $INIT
		chmod 744 $INIT/c4l_start
		ln -s $INIT/c4l_start $INIT/rc2.d/S03c4l
		ln -s $INIT/c4l_start $INIT/rc3.d/S03c4l
		ln -s $INIT/c4l_start $INIT/rc5.d/S03c4l
		ln -s $INIT/c4l_start $INIT/rc2.d/K44c4l
		ln -s $INIT/c4l_start $INIT/rc3.d/K44c4l
		ln -s $INIT/c4l_start $INIT/rc5.d/K44c4l
	fi
	i4l=0
fi

# 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 driver..."
echo "* Running rpm" >> install.log
rpm --upgrade --force c4l-$TYPE-$DRVVER-1.i386.rpm 2>> install.log
echo "Copying CAPI tools..."
rpm --upgrade --force c4l-lib-$LIBVER-1.i386.rpm 2>> install.log
echo "* Copying documentation" >> install.log
cp -vf install_active.de $DOC/CAPI4Linux/de/install_active.txt
cp -vf install_active.en $DOC/CAPI4Linux/en/install_active.txt
echo "* Running depmod:" >> install.log
depmod -q -a 2>> install.log

if [ "$cline1" == "" ]; then
	echo "Oops!"
	exit 7
fi

if ! test -f /etc/capi.conf; then
	echo "# card          file    proto   io      irq     mem     cardnr  options" > /etc/capi.conf
fi
echo -n "# "				>> /etc/capi.conf
echo "$clinec"				>> /etc/capi.conf
if [ $kick -eq 0 ]; then
	echo -n "#"			>> /etc/capi.conf
fi
echo -n "$cline1"			>> /etc/capi.conf
if [ $TDDI1 -eq 1 ]; then
	echo "P2P"			>> /etc/capi.conf
else
	echo ""				>> /etc/capi.conf
fi
NUM=2
if [ "$cline2" != "" ]; then
	while [ $clinen -ne 0 ]; do
		echo -n "$cline2"	>> /etc/capi.conf
		eval "FLAG=\$TDDI$NUM"
		if [ $FLAG -eq 1 ]; then
			echo "P2P"	>> /etc/capi.conf
		else
			echo ""		>> /etc/capi.conf
		fi
		NUM=`expr $NUM + 1`
		clinen=`expr $clinen - 1`
	done
fi
echo "* Contents of /etc/capi.conf:" >> install.log
cat /etc/capi.conf >> install.log

# Call card-specific finalizer
finalize $CARD

# Call archive-specific finalizer...
if test -f scripts/final; then
        scripts/final $prefix$name
fi

# TEST: Are there non-registered cards?
rcards=`expr $rcards + 1`
if [ $ncards -gt $rcards ]; then
	echo "* Request to install remaining controllers" >> install.log
	echo -e -n "\a\nPlease start ./install for the remaining controllers"
	echo -e " as well!\nCAPI4Linux will be started later..."
	exit 0
fi

if [ $kick -eq 1 ]; then
	echo -e "\a\nStarting CAPI4Linux...\n"
	echo "* capiinit start:" >> install.log
	log0=`wc -l < /var/log/messages`
	$SBIN/capiinit start
	log1=`wc -l < /var/log/messages`
	tail -n `expr $log1 - $log0` /var/log/messages >> install.log
			
	echo -e "\nIn case of a symbol error, please reboot!\n"

	echo "* Check for detected controller(s):" >> install.log
	cat /proc/capi/controller | grep detected >> install.log
	if [ $? -eq 0 ]; then
		echo -e "Not all controllers have been correctly initialized."
		echo -e "Also start ./install for these controllers!\n"
	fi
else
	echo -e -n "\n\aRemove the comment character ("#") in front of the "
	echo -e "following line\n\t#$cline1\nThen restart the computer.\n"
fi

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

