#! /bin/sh
#
# Installation of CAPI4Linux drivers & tools, v20020815A
# (PCI/ISA/PCMCIA, ISDN)

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

DEBUG=0
CMIN=70
LMIN=25
INITDIR=/etc/init.d
LIBMOD=/lib/modules/`uname -r`
DOCDIR=/usr/share/doc
SBINDIR=/usr/sbin
FWDIR=/usr/lib/isdn
EPDIR=/etc/pcmcia
MODDIR=$LIBMOD/misc
PCMCIADIR=$LIBMOD/pcmcia-external
ICONFIG=/etc/sysconfig/isdn
HPCONFIG=/etc/sysconfig/hotplug
PCCONFIG=/etc/sysconfig/pcmcia
ESC=`echo -en "\033"`
RED="${ESC}[1;31m"
NORM=`echo -en "${ESC}[m\017"`
KERNEL=2.4.18-4GB
LOGFILE=install.log

# INIT: Logfile handling
echo1() {
	# Normal log message. Copy to console in debug mode...
	echo -e "$1" >> $LOGFILE
	if [ $DEBUG -eq 1 ]; then
		echo -e "$1"
	fi
}
							
echo2() {
	# Normal message, written to log and console
	echo -n "console: " >> $LOGFILE
	echo -e "$1" | tee -a $LOGFILE
}
										
echo3() {
	# Warning message, written to log and console (in red)
	echo "warning: $1" >> $LOGFILE
	echo -e "\a${RED}$1${NORM}"
}
													
# TEST: Kernel version
kern=`uname -r`
if [ "$kern" != "$KERNEL" ]; then
        echo2 "Invalid kernel version... found $kern, expected: $KERNEL."
        exit 2
fi

line=`ls insta* | tr " a-z_.\n" [-*]`
echo1 "$line\n* Installation log (`date`):"
echo1 "Kernel: $kern"
cat release.txt >> $LOGFILE
 
. ./controller

# TEST: Am I root?
user=`id -u`
if [ "$user" != "0" ]; then
	echo ""
	echo2 "./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 ""
	echo3 "Missing or unknown controller name: $1"
	expected
	exit 2
fi
echo1 "- Card: $1"

# TEST: Dialog required?
echo1 "* Setting installation parameters"
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
			;;
		*)	echo2 "Invalid parameter: $2"
			options
			exit 1
			;;
		esac
		shift
	done
else
	dok=0
	COLUMNS=`tput cols`
	LINES=`tput lines`
	if [ $COLUMNS -ge $CMIN -a $LINES -ge $LMIN ]; then
		if which dialog > /dev/null; then
			dok=1
		fi
	else
		echo1 "* Terminal size ${COLUMNS}x${LINES} < ${CMIN}x${LMIN}!"
	fi
	if [ $dok -eq 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`
echo1 "* rpm versions: lib $LIBVER, drv $DRVVER"

# 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
		echo1 "* DDI set for controller $NUM"
		if [ $NUM -le $TDDIN ]; then
			DDIX=1
		else
			echo3 "Options for controller $NUM!"
		fi
	fi
done

# TEST: DDI possible?
if [ $DDIX -eq 1 -a $HAVE_DDI -eq 0 ]; then
	echo1 "* DDI requested but not available"
	echo2 "Point-to-point BRI is not supported!"
	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
echo2 "Firmware: $LFILE"
echo2 "Protocol: $LPROTO"
echo2 "Options:  $LOPTIONS"

# TEST: Does pppd exist?
which pppd > /dev/null
if [ 0 -ne $? ]; then
	echo2 "pppd has not been installed!"
	exit 5
fi

# TEST: New pppd, check version
pppd --version 2> tmp
cat tmp >> $LOGFILE
pver=`cat tmp | cut -d" " -f3`
if [ "$pver" != "2.4.1" ]; then
        echo2 "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: How 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
echo1 "* System status:"
echo1 "  Installed controllers: $ncards\n  PCMCIA controllers:    $pcmcia"

# 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
echo1 "  capi.conf controllers: $rcards"

if [ $ncards -gt 3 ]; then
	echo3 "Can not install more than four controllers!"
	cat /proc/capi/controller
	echo2 ""
	exit 6
fi

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

# TEST: i4l available & active?
i4l=0
i4lstop=0
if ls $ICONFIG/cfg-contr[0-9] > /dev/null 2>&1; then
	if lsmod | grep "isdn " > /dev/null 2>&1; then
		nctrl=`cat /proc/capi/controller | wc -l`
		if [ $nctrl -ne 0 ]; then
			echo1 "Stopping $nctrl CAPI controllers"
			if ! test -f /etc/capi.conf; then
				echo "" > /etc/capi.conf
				capiinit stop > /dev/null 2>&1
				rm -f /etc/capi.conf 
			else
				capiinit stop > /dev/null 2>&1
			fi
		fi
		echo2 "Stopping I4L..."
		rcisdn stop > /dev/null 2>&1
		i4lstop=1
	fi
	echo1 "* I4L config files available..."
	for cfg in $ICONFIG/cfg-contr[0-9]; do
		echo1 "  Checking $cfg"
		if grep "NAME=\"AVM" $cfg > /dev/null 2>&1; then
			i4l=$((i4l + 1))
			mv -vf $cfg $cfg.old >> $LOGFILE
		fi
	done
fi

if [ $rcards -gt 0 ]; then
	if [ $pcmcia -ne 0 ]; then
		echo2 "There is/are $pcmcia PCMCIA controller(s) active."
		echo2 "It is necessary to momentarily deactivate CAPI4Linux."
		echo2 "This is not possible as long as a PCMCIA controller"
		echo2 "is inserted. Please remove it and restart ./install."
		exit 0
	fi
	echo2 "Deactivating CAPI4Linux..."
	echo1 "* capiinit stop"
	capiinit stop
fi

# INIT: Install a new service in the SuSE boot procedure for runlevels
# 2, 3 and 5.
if ! test -f $INITDIR/capi4linux; then
        echo1 "* Installing 'capi4linux' boot script"
       	cp -vf capi4linux $INITDIR/capi4linux >> $LOGFILE
        insserv -d capi4linux
       	(cd $INITDIR; ls -laR | grep capi4linux) >> $LOGFILE
fi

# TEST: Does /usr/share/doc/CAPI4Linux/ exist...?
if ! test -d $DOCDIR/CAPI4Linux/de; then
        mkdir -p $DOCDIR/CAPI4Linux/de
        mkdir -p $DOCDIR/CAPI4Linux/en
fi
echo1 "* $DOCDIR/CAPI4Linux ok"

echo2 "Copying CAPI driver..."
echo1 "* Running rpm"
rpm --upgrade --force c4l-$TYPE-$DRVVER-1.i386.rpm 2>> $LOGFILE
echo2 "Copying CAPI tools..."
rpm --upgrade --force c4l-lib-$LIBVER-1.i386.rpm 2>> $LOGFILE
echo1 "* Copying documentation"
cp -vf install_active-d.html $DOCDIR/CAPI4Linux/de/install_active.html
cp -vf install_active-e.html $DOCDIR/CAPI4Linux/en/install_active.html
echo1 "* Running depmod:"
depmod -a 2>> $LOGFILE

if [ "$cline1" == "" ]; then
	echo2 "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
echo1 "* Contents of /etc/capi.conf:"
cat /etc/capi.conf >> $LOGFILE

# 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
	echo2 ""
	echo2 "Please start ./install for the remaining controllers as well!"
	echo2 "CAPI4Linux will be started later..."
	exit 0
fi

if [ $i4lstop -ne 0 ]; then
	if ls $ICONFIG/cfg-contr* > /dev/null 2>&1; then
		echo2 "Restarting I4L..."
		rcisdn start 
	fi
fi

if [ $kick -eq 1 ]; then
	echo2 "Starting CAPI4Linux..."
	echo1 "* capiinit start:"
	log0=`wc -l < /var/log/messages`
	$SBINDIR/capiinit start
	log1=`wc -l < /var/log/messages`
	tail -n `expr $log1 - $log0` /var/log/messages >> $LOGFILE
			
	echo2 "In case of a symbol error, please reboot!"

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

echo1 "* Leaving install script"

