#! /bin/bash
#
# isdn		Starts isdn. This script starts the isdnlog program
#		and brings up isdn interfaces.
#
#
# chkconfig: - 80 20
# description: Starts and stops ISDN services. ISDN is a networking \
#	interface, common in Europe.

num_devices=0
num_sync=-1
num_raw=-1

# Source function library
. /etc/rc.d/init.d/functions

[ ${NETWORKING} = "no" ] && exit 0
[ -x /usr/sbin/isdnctrl ] || exit 0
[ -x /usr/sbin/isdnlog ] || exit 0

case "$1" in
  start)
	if [ -f /etc/sysconfig/activeisdn ]; then
		source /etc/sysconfig/activeisdn
		ACTIVEPROV=$PROVIDER
		# ART=
	fi
	modprobe hisax
	hisaxctrl HiSax 1 0x3ff
	# isdnlog -vw2 -S -m10231 -F -h5:3 -t1 /dev/isdnctrl 2>/var/log/isdn.log
	echo -n "Starting isdn logging: "
	daemon isdnlog -vw2 -S -m10231 -F -t1 /dev/isdnctrl
	RETVAL=$?
	echo
	devices=

	# Loop for each provider... 
	for prov in /etc/sysconfig/provider/conf-*; do
		source $prov
		# ISDNDEV is either ippp$num_sync or isdn$num_raw
		if [ "x$ISDNDEV" = "xippp" ]; then
			num_sync=$[$num_sync + 1 ]
			ISDNDEVICE=${ISDNDEV}${num_sync}
			counter=${num_sync}
		elif [ "x$ISDNDEV" = "xisdn" ]; then
			num_raw=$[$num_raw + 1 ]
			ISDNDEVICE=${ISDNDEV}${num_raw}
			counter=${num_raw}
		else
			echo "ISDNDEV is not one of ippp or isdn. Aborting execution."
			exit 1
		fi

		num_devices=$[$num_devices + 1 ]
		devices="$devices $ISDNDEVICE"
		isdnctrl addif ${ISDNDEVICE}
		isdnctrl eaz ${ISDNDEVICE} ${MSN}

		for MSN in $IN_MSN; do
		  isdnctrl addphone ${ISDNDEVICE} in $MSN
		done

		for MSN in $OUT_MSN; do
		  isdnctrl addphone ${ISDNDEVICE} out $MSN
		done

		isdnctrl huptimeout ${ISDNDEVICE} ${HUPTIMEOUT}
		isdnctrl chargehup ${ISDNDEVICE} ${CHARGEHUP}
		isdnctrl chargeint ${ISDNDEVICE} ${CHARGEINT}
		isdnctrl cbhup ${ISDNDEVICE} ${CBHUP}
		isdnctrl cbdelay ${ISDNDEVICE} ${CBDELAY}
		isdnctrl dialmax ${ISDNDEVICE} ${DIALMAX}
		isdnctrl ihup ${ISDNDEVICE} ${IHUP}
		isdnctrl callback ${ISDNDEVICE} ${CALLBACK}
		isdnctrl secure ${ISDNDEVICE} ${SECURE}
		isdnctrl l2_prot ${ISDNDEVICE} ${L2_PROT}
		isdnctrl l3_prot ${ISDNDEVICE} ${L3_PROT}
		isdnctrl encap ${ISDNDEVICE} ${ENCAP}
		#isdnctrl status ${ISDNDEVICE} on

		ifconfig ${ISDNDEVICE} ${IP} -broadcast -arp -allmulti
		route add -host ${GATEWAY} dev ${ISDNDEVICE}

		if [ "x$DUAL" = "xyes" -o "x$DUAL" = "xYes" -o "x$DUAL" = "xYES" ]; then
			if [ "x$ISDNDEV" = "xippp" ]; then
				counter=${num_sync}
				num_sync=$[$num_sync + 1 ]
				MASTERDEVICE=${ISDNDEV}${counter}
				SLAVEDEVICE=${ISDNDEV}${num_sync}
				# dualcounter=${num_sync}
			elif [ "x$ISDNDEV" = "xisdn" ]; then
				counter=${num_raw}
				num_raw=$[$num_raw + 1 ]
				MASTERDEVICE=${ISDNDEV}${counter}
				SLAVEDEVICE=${ISDNDEV}${num_raw}
				# dualcounter=${num_raw}
			fi

			isdnctrl addslave ${MASTERDEVICE} ${SLAVEDEVICE}
			isdnctrl eaz ${SLAVEDEVICE} ${DUAL_MSN}

			for MSN in $DUAL_IN_MSN; do
				isdnctrl addphone ${SLAVEDEVICE} in $MSN
			done
			for MSN in $DUAL_OUT_MSN; do
				isdnctrl addphone ${SLAVEDEVICE} out $MSN
			done

			isdnctrl huptimeout ${SLAVEDEVICE} ${DUAL_HUPTIMEOUT}
			isdnctrl chargehup ${SLAVEDEVICE} ${CHARGEHUP}
			isdnctrl chargeint ${SLAVEDEVICE} ${CHARGEINT}
			isdnctrl cbhup ${SLAVEDEVICE} ${CBHUP}
			isdnctrl sdelay ${MASTERDEVICE} ${SDELAY}
			isdnctrl trigger ${MASTERDEVICE} ${TRIGGER}
			isdnctrl dialmax ${SLAVEDEVICE} ${DUAL_DIALMAX}
			isdnctrl ihup ${SLAVEDEVICE} ${IHUP}
			isdnctrl callback ${SLAVEDEVICE} ${CALLBACK}
			isdnctrl cbdelay ${SLAVEDEVICE} ${CBDELAY}
			isdnctrl secure ${SLAVEDEVICE} ${SECURE}
			isdnctrl l2_prot ${SLAVEDEVICE} ${L2_PROT}
			isdnctrl l3_prot ${SLAVEDEVICE} ${L3_PROT}
			isdnctrl encap ${SLAVEDEVICE} ${ENCAP}
			# isdnctrl status ${SLAVEDEVICE} on
		fi
		num_devices=$[$num_devices + 1 ]

		# Default gateway
		if [ "x$ACTIVEPROV" = "x$PROVIDER" -a "x$ART" = "xautomatisch" -a "$ART" = "automatic" ]; then
			isdnctrl dialmode ${ISDNDEVICE} auto
			echo ${ISDNDEVICE} > /var/run/dialondemand
			route add default gw ${GATEWAY} dev ${ISDNDEVICE}
		else
			isdnctrl dialmode ${ISDNDEVICE} auto
			# isdnctrl dialmode ${ISDNDEVICE} manual
		fi

		# SyncPPP-Devices and Ipppd start:
		if [ ${ENCAP} = "syncppp" ]; then
			isdnctrl pppbind ${ISDNDEVICE} ${counter}
			if [ "x$DUAL" = "xyes" ]; then
				isdnctrl pppbind ${SLAVEDEVICE} ${num_sync}
			fi
			# route add -host ${GATEWAY} dev ${ISDNDEVICE}
			OPTIONSFILE="/etc/ppp/ioptions.${PROVIDER}"
			echo -n "Starting ipppd for ${ISDNDEVICE}: "
			daemon ipppd file ${OPTIONSFILE} ${ISDNDEVICE}
			echo
		fi

		if [ "x$DUAL" = "xno" ]; then
			echo "device=${ISDNDEVICE}" > /var/run/isdndevice${PROVIDER}
		elif [ "x$DUAL" = "xyes" ]; then
			echo "device=${MASTERDEVICE} ${SLAVEDEVICE}" > /var/run/isdndevice${PROVIDER}
		fi

		# List all configured SynPPP-Devices:
		if [ "x$ISDNDEV" = "xippp" ]; then
			echo ${PROVIDER}"="${ISDNDEVICE} >> /var/run/isdn.syncppp
		# List all configured RawIP-Devices:
		elif [ "x$ISDNDEV" = "xisdn" ]; then
			echo ${PROVIDER}"="${ISDNDEVICE} >> /var/run/isdn.rawip
		fi
	done

	isdnctrl verbose 4

	# Activate dynamic IP address (0=disable, 1=enable, 2=enable verbose mode)
	if [ -f /proc/sys/net/ipv4/ip_dynaddr ]; then
		echo 1 > /proc/sys/net/ipv4/ip_dynaddr
	fi
	if [ $RETVAL -eq 0 ]; then
	 	touch /var/lock/subsys/isdn
		echo "OFF" > /var/lock/subsys/mail-active
	 fi
         RETVAL=$?
         ;;
  stop)
  	echo -n "Stopping isdn logging: "
	killproc isdnlog
	echo
	echo -n "Stopping ipppd: "
	killproc ipppd
	RETVAL=$?
	echo 
	devices=
	[ -f /var/run/isdn ] && source /var/run/isdn
	for dev in $devices; do
		ifconfig ${dev} down
		isdnctrl delif ${dev}
	done
	modprobe -r hisax
	if [ $RETVAL -eq 0 ] ; then
		rm -f /var/run/dialondemand
		rm -f /var/run/isdndevice*
		rm -f /var/run/isdn
		rm -f /var/run/isdn.rawip
		rm -f /var/run/isdn.syncppp
		rm -f /var/lock/subsys/isdn
		rm -f /var/lock/subsys/mail-active
	fi
	;;
  reload|restart)
	$0 stop
	sleep 3
	$0 start
	RETVAL=$?
        ;;
  *)
        echo "Usage: isdn {start|stop|restart|reload}"
        exit 1
        ;;
esac

exit $RETVAL
