#! /bin/bash

# c't/ODS-Kommunikationsserver V3.1+    
# (c) Klaus Fller & Redaktion c't 1996
# Anpassung an Version 3.0 von R.Klaproth
# header geaendert, efl, 23aug01
# Nutzung gem der GNU Public License  
# $Header: ether V 3.1+ , 04.07.99

PATH=/usr/lib/ods-server/bin:$PATH

if [ -z "$1" ]; then
	exit 1
fi

if [ x$1 = x-p ]; then
	# if [ -x /lib/modules/`uname ...`/net/3c509.o ]; then
	# uswusw.  Ich wei nicht, wo ich die erluternden Strings
	# herbekommen soll.  Am Besten aus den Quellen.  Aber da
	# stehen sie nicht vernnftig drin.
	# Aufgabe: Gegeben ist ein Treibername (`e3509')
	# Gesucht ist ein erklrender String (`3-com 509-Adapter')

	echo "3c509:3-COM 509-Adapter:irq"
	echo "3c515:3-COM 515-Adapter: "
	echo "3c59x:3COM 900/905-Familie: "
	echo "3c90x:3COM 905b/905c-Karte: "
	echo "ewrk3:DEC Etherworks DE203/204/205:io irq"
	echo "eexpress:Intel Ether Express:io irq"
	echo "eepro:Intel EtherExpress Pro/10:io irq mem"
	echo "eepro100:Intel EtherExpress Pro 100B: "
	echo "ne:NE1000-, NE2000-Adapter:io irq"
	echo "ne2k-pci:PCI-NE2000-Adapter: "
	echo "pcnet32:PCnet-PCI-Karten (AMD-Chips): "
	echo "rtl8139:Realtec 8139-PCI 100MBit-Karten: "
	echo "smc-ultra:SMC Ultra-, EtherEZ-Adapter:io irq"
	echo "epic100:SMC Ether Power II-100: "
	echo "tulip:PCI-Karte mit DEC-Chip (auch 100 MBit/s):mode"
	echo "wd:Western-Digital(SMC) 8003-, 8013-Adapter:io irq mem"
	exit 0
fi

scriptname=`basename $0`
cd /etc/Systemverwaltung

if [ ! -f $scriptname ]; then
	co $scriptname >/dev/null 2>&1
fi
if [ ! -f $scriptname ]; then
	touch $scriptname
	chmod 600 $scriptname
	ci -u $scriptname < /dev/null >/dev/null 2>&1
fi
chmod 600 $scriptname

if [ "$1" = "-d" ]; then
	if [ $# != 2 ]; then
		echo $0 -d ip-nummer >&2
		exit 1
	fi
	egrep -v ^$2: $scriptname > /tmp/$scriptname.$$
	cat /tmp/$scriptname.$$ > $scriptname
	rm /tmp/$scriptname.$$
	exit 0
fi

if [ "$1" = "-dd" ]; then
	rm -f $scriptname
	exit 0
fi

if [ "$1" = "-r" ]; then
	co -p $scriptname 2>/dev/null	# Das geht hier, da keine
					# weitere Aufbereitung ntig.
	exit 0
fi

if [ "$1" = "-rr" ]; then
	if [ -f $scriptname ]; then
		sort $scriptname
	fi
	exit 0
fi

if [ "$1" != "-install" ]; then
	# Karte mit eventuell gleicher Nummer entfernen
	ip=`echo $* | cut -d: -f1`
	egrep -v ^$ip: $scriptname > /tmp/$scriptname.$$
	cat /tmp/$scriptname.$$ > $scriptname
	rm /tmp/$scriptname.$$
	echo $* >> $scriptname
	exit 0
fi

if [ ! -f $scriptname ]; then
	# Es wurde nichts vorgemerkt.  Also brauchen wir
	# nichts zu installieren.
	exit 0
fi

# BITTE, BITTE!!  Jetzt kein Absturz!

co -l -p $scriptname >/dev/null 2>&1
cp $scriptname /tmp/scriptname.$$
sort /tmp/scriptname.$$ > $scriptname
rm -f /tmp/scriptname.$$

# Wir bearbeiten die /etc/conf.modules
cp -f /etc/conf.modules /tmp/conf.modules.$$
fgrep -v '# Diese Zeile kommt von ether' \
	/tmp/conf.modules.$$ > /etc/conf.modules
rm -f /tmp/conf.modules.$$

# Neu eintragen. Muster:
# alias eth0 ne
# options ne io=0x300 irq=10

awk -F: '
	BEGIN {i=0};
	{
	print "alias\teth" i " " $2 " # Diese Zeile kommt von ether"
	if ( $3 != "" )
		print "options " $2 "  " $3 " # Diese Zeile kommt von ether" ;

	++i
	}
	' $scriptname >> /etc/conf.modules

# Was tun, wenn zwei gleiche Karten da sind? Also gehen wir die
# Sache noch mal durch:

awk -F: '{ print $2 ": " $3 }' $scriptname > /tmp/modules

module="3c509 ewrk3 eexpress eepro ne wd smc-ultra"
for typ in $module ; do
	# Nur diese Karten brauchen Sonderbehandlung
	grep "$typ:" /tmp/modules > /tmp/modules.$typ
	wc -l /tmp/modules.$typ > /tmp/lines.$typ
	read lines onfile < /tmp/lines.$typ
	if [ $lines -gt 1 ] ; then
		# An die Arbeit, wir haben doppelte Karten
		# In /tmp/modules.$typ steht, was wir brauchen
		echo -n "io=" > /tmp/io.$typ
		echo -n "irq=" > /tmp/irq.$typ
		echo -n "mem=" > /tmp/mem.$typ
		nums=`nl -s: -w1 /tmp/modules.$typ | cut -d: -f1`
		for line in $nums ; do
		   read ntyp io irq mem
		   if [ $line -gt 1 ] ; then
			echo -n "," >> /tmp/io.$typ
			echo -n "," >> /tmp/irq.$typ
			echo -n "," >> /tmp/mem.$typ
		   fi
		   iox=`echo $io | cut -d= -f2`
		   irqx=`echo $irq | cut -d= -f2`
		   memx=`echo $mem | cut -d= -f2`
		   echo -n $iox >> /tmp/io.$typ
		   echo -n $irqx >> /tmp/irq.$typ
		   echo -n $memx >> /tmp/mem.$typ
		done < /tmp/modules.$typ
		io=`cat /tmp/io.$typ`
		irq=`cat /tmp/irq.$typ`
		mem=`cat /tmp/mem.$typ`
		grep -v "options $typ" /etc/conf.modules > /tmp/conf.modules
		if [ $typ = "3c509" ] ; then
			# Problem: Diese Karten brauchen nur irq=a,b,c..; der
			# steht jetzt aber in $io. Also umbauen!
			irq="irq="`echo $io | cut -d= -f2` 
			echo "options $typ $irq  # Diese Zeile kommt von ether" >> /tmp/conf.modules
		elif [ $typ = "wd" -o $typ = "eepro" ] ; then
			echo "options $typ $io $irq $mem  # Diese Zeile kommt von ether" >> /tmp/conf.modules
		else
			echo "options $typ $io $irq  # Diese Zeile kommt von ether" >> /tmp/conf.modules
		fi
	rm -f /etc/conf.modules
	mv /tmp/conf.modules /etc/conf.modules
	fi
	rm -f /tmp/*.$typ 
done
rm -f /tmp/modules

# Letzte Massnahme: Die /etc/samba/smb.conf anpassen
if [ -f /etc/samba/smb.conf ] ; then
	grep -n "interfaces" /etc/samba/smb.conf > /tmp/smb.tmp
	LINE=`tail -1 /tmp/smb.tmp | cut -d: -f1`
	rm -f /tmp/smb.tmp
	# So, nun haben wir die Zeile gefunden, die zu ndern ist.
	echo "$LINE c" >/tmp/smb.ed
	# Nun schauen wir die Netzkarten durch
	echo -e -n "\tinterfaces\t= " >> /tmp/smb.ed
	awk -F: '{ if ( $1 != "ADSL")
		    printf( " %s/%s", $1, $4 ); }' $scriptname >> /tmp/smb.ed  
	echo " " >>/tmp/smb.ed
	echo "." >>/tmp/smb.ed
	echo "w" >>/tmp/smb.ed
	echo "q" >>/tmp/smb.ed

	chmod 644 /etc/samba/smb.conf
	ed -s /etc/samba/smb.conf < /tmp/smb.ed

	rm -f /tmp/smb.ed
fi

# 1       2      3        4       5       6         7
# ip-addr:modul:param...:netmask:network:broadcast:route ...

ci $scriptname </dev/null >/dev/null 2>&1
exit 0

