#! /bin/sh

# FRITZ!Card DSL SL USB, 20031212

ESC=`echo -en "\033"`
RED="${ESC}[1;31m"
NORM=`echo -en "${ESC}[m\017"`
TGZVER1=2.4.20-4GB
TGZVER2=2.4.20-64GB-SMP
BLACKLIST=/etc/hotplug/blacklist
BLACKFLAG=0
LIBMOD=/lib/modules/`uname -r`
MODDIR=$LIBMOD/misc
DOCDIR=/usr/share/doc
SBINDIR=/usr/sbin
FWDIR=/usr/lib/isdn
HPUSBDIR=/etc/hotplug/usb
LOGFILE=install.log

# Installation options

opt_debug=0

# Helpers...

echo1() {
	# Normal log message. Copy to console in debug mode...
	echo -e "$1" >> $LOGFILE
	if [ $opt_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}"
}

# Installation starts HERE!

# install.card provides:
# $card
# $prefix
# $module
# $loadfile?	
#
. ./install.card
cardname=`head -n 1 install.card | cut -b 3-`
name="${card#x}"
kern=`uname -r`
user=`id -u`
if [ "$user" != "0" ]; then
	echo2 "$0 must be started by \"root\"."
	exit $err_root
fi

line=`ls insta* | tr " a-z0-9_.\n" [-*]`
echo1 "$line\n* Installation log (`date`):"
echo1 "Kernel: $kern"
echo1 "Card: $prefix$name"
cat release.txt >> $LOGFILE

K=0
if [ "$kern" != "$TGZVER1" ]; then
	if [ "$kern" != "$TGZVER2" ]; then
		echo2 "Invalid kernel version: $kern"
		exit $err_kver
	else
		K=2
	fi
else
	K=1
fi
echo1 "Module selector K: $K"

if ! initialize; then
	echos "Card initializer failed."
	exit $err_init
fi

if ! test -d $MODDIR; then
	echo1 "Creating $MODDIR"
	mkdir -p $MODDIR 2>> $LOGFILE
fi
if [ $K -gt 0 ]; then
	cp -vf $module$K.o $MODDIR/$module.o >> $LOGFILE
	echo1 "Running depmod -a"
	depmod -a >> $LOGFILE 2>> $LOGFILE
else
	echo2 "Please copy the driver module manually to $MODDIR..."
fi

if [ $BLACKFLAG -ne 0 ]; then
	if ! grep $module $BLACKLIST > /dev/null 2>&1; then
		echo1 "Adding $module to $BLACKLIST"
		{
			echo ""
			echo "# $cardname (loaded by the AVMDSL script)"
			echo $module
		} >> $BLACKLIST
	fi
fi

if ! test -d $DOCDIR/CAPI4Linux/de; then
	echo1 "Creating $DOCDIR/CAPI4Linux/(de/en)"
	mkdir -p $DOCDIR/CAPI4Linux/de
	mkdir -p $DOCDIR/CAPI4Linux/en
fi
cp -vf install_passive-d.html $DOCDIR/CAPI4Linux/de/install_passive.html >> $LOGFILE
cp -vf install_passive-e.html $DOCDIR/CAPI4Linux/en/install_passive.html >> $LOGFILE
for lf in $loadfile1 $loadfile2 x; do
	if [ "$lf" != "x" ]; then
		cp -vf $lf $FWDIR >> $LOGFILE
	fi
done
if ! install_card; then
	echos "Card install failed."
	exit $err_inst
fi

# STEP 6: Check presence of pppd
#
echo1 "Checking pppd..."
if ! which pppd >> $LOGFILE; then
	echo3 "pppd has not been installed!"
else
	pppd --version 2>> $LOGFILE > /dev/null
fi

echo2 ""
echo2 "The driver files have been copied."
if ! finalize; then
	echos "Card finalizer failed."
	exit $err_final
fi

