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

CARD=$1
DDI=0
DDIX=0
TDDI1=0
TDDI2=0
TDDI3=0
TDDI4=0
TFILE=dss1
TPROTO=dss1
IPROTO=euro
CARDNO=0
LFILE=
LPROTO=
LOPTIONS=
LIOADDR=
LIRQ=

DEBUG=0
CMIN=70
LMIN=25
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.19-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?
rcards=0
ccmax=-1
ccidx=0
ccvar=0
if ls $ICONFIG/cfg-contr* > /dev/null 2>&1; then
	for cfg in $ICONFIG/cfg-contr*; do
		ccnum=`echo ${cfg##$ICONFIG/cfg-contr}`
		if [ $ccnum -gt $ccmax ]; then
			ccmax=$ccnum
		fi
		if [ $ccvar -ne $ccnum -a $ccidx -eq 0 ]; then
			ccidx=$ccnum
		fi
		if grep AVM $cfg > /dev/null; then
			echo1 "  $cfg"
			rcards=$((rcards + 1))
		fi
		ccvar=$((ccvar + 1))
	done
fi
if [ $ccidx -eq 0 ]; then
	ccidx=$((ccmax + 1))
fi
echo1 "  Found $rcards in $ICONFIG"
echo1 "  Free index: $ccidx"

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 AVM controller installed."
else
	echo2 "$rcards AVM controllers installed."
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

echo1 "* File $ICONFIG/cfg-contr$ccidx:"
{
	drv=`echo $cline1 | cut -d" " -f1`
	echo    "STARTMODE=onboot"
	echo    "DRIVER=$drv"
	echo    "PARA_TYPE=8005"
	echo    "PARA_SUBTYPE=2"
	echo -e "PROTOCOL=\"$IPROTO\""
	if [ "$drv" == "t1isa" ]; then
		echo -e "NAME=\"AVM T1 ISA\""
		echo	"PARA_IO=$LIOADDR"
		echo	"PARA_IRQ=$LIRQ"
	else
		echo -e "NAME=\"AVM T1 PCI\""
		echo -e	"PARA_IO=\"\""
		echo -e	"PARA_IRQ=\"\""
	fi
} | tee $ICONFIG/cfg-contr$ccidx >> $LOGFILE
 
if [ "$cline1" == "" ]; then
	echo2 "Oops!"
	exit 7
fi

echo1 "* File $ICONFIG/capi.conf.contr$ccidx:"
{
	echo "# card          file    proto   io      irq     mem     cardnr  options"
	echo -n "# "
	echo "$clinec"
	echo -n "$cline1"
	if [ $TDDI1 -eq 1 ]; then
		echo "P2P"
	else
		echo ""	
	fi
	NUM=2
	if [ "$cline2" != "" ]; then
		while [ $clinen -ne 0 ]; do
			echo -n "$cline2"
			eval "FLAG=\$TDDI$NUM"
			if [ $FLAG -eq 1 ]; then
				echo "P2P"
			else
				echo ""
			fi
			NUM=`expr $NUM + 1`
			clinen=`expr $clinen - 1`
		done
	fi
} | tee $ICONFIG/capi.conf.contr$ccidx >> $LOGFILE

# Some installations may not have these files...
if ! test -f $ICONFIG/scripts/load-t1isa; then
	cp -vf load-t1* stop-t1* $ICONFIG/scripts >> $LOGFILE
fi

# Call card-specific finalizer
finalize $CARD

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

echo1 "* Running SuSEconfig --module isdn"
SuSEconfig --module isdn
echo1 "* Running rcisdn start"
rcisdn start

echo1 "* Leaving install script"

