#! /bin/sh
#
# Preparatory steps for a YaST based installation for
# FRITZ!Card DSL v2.0 and SuSE Linux 8.2
# v 20030814

# INIT: Common definitions
#
ESC=`echo -en "\033"`
RED="${ESC}[1;31m"
NORM=`echo -en "${ESC}[m\017"`
NOT[0]=" "
NOT[1]="not "
TGZVER1=2.4.20-4GB
TGZVER2=2.4.20-64GB-SMP
INITDIR=/etc/init.d
LIBMOD=/lib/modules/`uname -r`
DOCDIR=/usr/share/doc
SBINDIR=/usr/sbin
CONFDIR=/sbin/conf.d
FWDIR=/usr/lib/isdn
MODDIR=$LIBMOD/misc
PCMCIADIR=$LIBMOD/misc
ICONFIG=/etc/sysconfig/isdn
LOGFILE=prepare.log
BLACKFLAG=0

# INIT: Define some return codes for this script
#
no_error=0		# Documentation follows... ;-)
err_hisax=1		# Running on hisax
err_check=1		# Invalid CHECK1 value
err_ready=2		# Exit for YaST preparation
err_pver=3		# pppd version != 2.4.1
err_init=4		# card initializer failed
err_inst=5		# card install failed
err_final=6		# card finalizer failed
err_kver=7		# invalid kernel version
err_root=8		# Invalid user id (!= root)
		
# INIT: Include card specific stuff
#
. ./install.card
kern=`uname -r`
cardname=`head -n 1 install.card | cut -b 3-`
name="${card#x}"
if [ "$name" != "$card" ]; then
	prefix=fx
else
	prefix=fc
fi
module=$prefix$name

# INIT: Setting up log file (see: variable LOGFILE)
#
opt_debug=0		# Run script in debug mode

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}"
}

echos() {
	# Normal log messages, step info.
	echo -e "STEP $STEP: $1" >> $LOGFILE
	if [ $opt_debug -eq 1 ]; then
		echo -e "STEP $STEP: $1"
	fi
}	

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

# STEP 0: Parsing the command line...
#
STEP=0
opt_compile=0		# Compile prior to installation
opt_identify=0		# Print info about driver being installed
opt_force_cp=0		# ALLWAYS copy driver file
opt_check_kernel=1	# Check for correct kernel verion
opt_check_pppd=1	# Check for correct pppd version
#
opt_string=""
opt_count=0
opt_ok=0

while [ "$1" != "" ]; do
	opt_ok=1
	case $1 in

	-c)	opt_compile=1
		;;
	-d)	opt_debug=1
		;;
	-f)	opt_force_cp=1
		;;
	-i)	opt_identify=1
		;;
	-p)	opt_check_pppd=0
		;;
	*)	opt_ok=0
		echo3 "Unrecognized option: $1"
		;;
	esac
        if [ $opt_ok -eq 1 ]; then
		opt_string="$opt_string $1"
		opt_count=$((opt_count + 1))
	fi
	shift
done
echos "$opt_count accepted options: $opt_string"

if [ $opt_identify -eq 1 ]; then
	echo -e "Installing $module.o (CAPI driver for $cardname)"
fi
						
# STEP 1: Checking kernel, pppd, etc....
#
STEP=1
K=0
if [ $opt_check_kernel -eq 1 ]; then
	if [ "$kern" != "$TGZVER1" ]; then
		if [ "$kern" != "$TGZVER2" ]; then
			echo2 "Invalid kernel version: $kern"
			exit $err_kver
		else
			K=2
		fi
	else
		K=1
	fi
fi
echo1 "Module selector K: $K"

echos "Checking pppd..."
if ! which pppd > /dev/null; then
	echo3 "pppd has not been installed!"
	echo2 "If you want to use pppd to connect to the Internet, please"
	echo2 "install the ppp package!"
fi

pppd --version 2> tmp
cat tmp >> $LOGFILE
if [ $opt_check_pppd -eq 1 ]; then
	pver=`cat tmp | cut -d" " -f3`
	if [ "$pver" != "2.4.1" ]; then
		echo3 "Version 2.4.1 of pppd is required!"
		exit $err_pver
	fi
fi
rm tmp

# STEP 2: What needs to be copied?
#
STEP=2
if ! test -f $MODDIR/$module.o; then
	echos "$MODDIR/$module.o does not exist."
	C4LDRV=1
elif test $module$K.o -nt $MODDIR/$module.o; then
	echos "$MODDIR/$module.o needs to be updated."
	C4LDRV=1
else	
	if [ $opt_force_cp -eq 1 ]; then
		echos "Forced update of $module.o!"
	else
		echos "File $module.o does not need an update."
	fi
	C4LDRV=$opt_force_cp
fi

# STEP 3: Copying my stuff...
#
STEP=3
if ! initialize; then
	echos "Card initializer failed."
	exit $err_init
fi
if [ $C4LDRV -eq 1 ]; then
	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
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 4: Updating system
#
echos "Prepare YaST installation:"
if ! grep "FRITZ!Card DSL v2.0" $CONFDIR/SuSEconfig.isdn > /dev/null 2>&1; then
	patch -b $CONFDIR/SuSEconfig.isdn SuSEconfig.diff >> $LOGFILE
else
	echos "$CONFDIR/SuSEconfig.isdn seems to be patched!"
fi
echos "Installing new libhd..."
if rpm -q hwinfo-devel-6.19 > /dev/null 2>&1; then
	echo2 "Please update package hwinfo-devel-6.19 to version 6.22!"
fi
rpm -Uv --nodeps hwinfo-6.22-0.i586.rpm >> $LOGFILE 2>&1 
if ! test -f $ICONFIG/scripts/load-fcdsl2; then
	cp -vf *-fcdsl2 $ICONFIG/scripts >> $LOGFILE
fi

# STEP 5: Final tasks
#
STEP=5
echo ""
echo2 "The driver files have been copied."
if ! finalize; then
	echos "Card finalizer failed."
	exit $err_final
fi

