#!/bin/bash
#
# Config script to make your own rspfd.conf file automagically
#
#==================================================================
# 0.01  	Initial script
# 0.02		They've moved the config files on me!!
#		Ignore comments
# 0.03		Now it looks like a debian script
#==================================================================

AXPORTS=/etc/ax25/axports
OUTFILE=rspfd.conf

function getparm() {
	ans=$2
	echo -n $1'  ['$2'] ' 
	read TMP
	if [ -z $TMP ]
	then
		TMP=$2
	fi
	ans=$TMP

}

function speedcost() {
	if [ $1 -ge 9600 ] ; then
		METRIC=2
	else if [ $1 -ge 4800 ] ; then
			METRIC=4
		else if [ $1 -ge 1200 ] ; then
				METRIC=8
			else
				METRIC=16
			fi
		fi
	fi
}
#
#
# Start here *********************************************

#if [ $# -gt 0 ] ; then
#	if [ $1 == "--force" ] ; then
#		echo 'forcing!'
#		FORCE=1
#	fi
#fi

echo 'RSPFd configuration program v 0.02'
echo 
echo 'This program automagically creates a config file for the RSPF'
echo 'daemon.  It needs the axports file to find the interfaces.'
echo 'Please send any comments to the author.'
echo

if [ ! -r ${AXPORTS} ]
then
	echo -e 'Cannot find the axports file, I'\''m looking in'
	echo ${AXPORTS}
	exit 1
fi

echo '# Configuration file for '`hostname` > ${OUTFILE}
echo '# automatically created '`date` >> ${OUTFILE}
echo '#' >> ${OUTFILE}

COUNT=0
for CALL in `grep -v '^#' ${AXPORTS} | awk '{print $2}' | xargs`
do
	let COUNT=COUNT+1

	BCALL=`echo ${CALL} | cut -f 1 -d '-' `
	IFACE=`ifconfig | grep ${CALL}'$' | cut -f 1 -d ' '`
	SPEED=`grep ${CALL} ${AXPORTS} | awk '{print $3}'`
	DESC=`grep ${CALL} ${AXPORTS} | awk '{print $6 $7 $8 $9}'`
	
	echo	
	echo '-----------------------------------------------------' 
	echo 'Port:'${COUNT}'   Callsign: '${CALL}'   Speed:'${SPEED}
	echo '         (' ${DESC} ') '
	echo

	getparm 'Configure this port for RSPF ' y

	if [ "$ans" = "n" -o "$ans" = "N" ]
	then
		continue
	fi
	
	if [ -z "${IFACE}" ]
	then
		IFACE=`ifconfig | grep ${BCALL} | cut -f 1 -d ' ' | head -n 1 `
	fi

	getparm 'Interface name ' ${IFACE}
	IFACE=${ans}

# Work out what the default quality is
	speedcost ${SPEED}
	SPEED=${ans}

	getparm 'Metric/Cost for this port' ${METRIC}
	METRIC=${ans}
	
	echo '# ' >> ${OUTFILE}
	echo '# Port:'${COUNT}'  Callsign:'${CALL}'  Speed:'${SPEED} >> ${OUTFILE}
	echo 'rspfiface='${IFACE}'  '${METRIC} >> ${OUTFILE}

done

# Put in the node groups
	echo '#' >> ${OUTFILE}
	echo '# Node groups' >> ${OUTFILE}

	echo
	echo '-----------------------------------------------------------'
	echo 'Node groups'
	echo 'These are groups of IP addresses that this router will look'
	echo 'after.  The main router for the area would have an entry for'
	echo 'the local subnet.  Also if you have an ethernet network, put'
	echo 'it here.  Basically use them as static routes you want to'
	echo 'advertise.'

	while true
	do
		getparm 'Do you want to add a nodegroup? ' n
		if [ "$ans" = "n" -o "$ans" = "N" ]
		then
			break
		fi

		echo
		echo 'Node groups have three of four components:'
		echo '  1) IP address, make all non significant bits 0'
		echo '  2) Significant bits, like NOS (eg 255.0.0.0 is 8, 255.255.255.128 is 129'
		echo '  3) Interface name. (eg sl0, pt0a, pi0b)'
		echo '  $) Metric/Cost, the default is the interface cost.'
		echo
		getparm 'Enter IP address ' ""
		if [ -z "$ans" ]
		then
			continue
		fi

		IPADD=$ans

		getparm 'Enter significant bits' "32"
		if [ -z "$ans" -o  $ans -lt 1  -o  $ans -gt 32  ]
		then
			echo 'Signifcant bits must be between 1 and 32'
			continue
		fi
		SIGBIT=$ans
		
		echo
		getparm 'Enter interface the node group goes on' "sl0"
		if [ -z "$ans" ] 
		then
			continue
		fi
		NGIF=$ans

		echo
		getparm 'Metric/Cost of node group (hit enter for default to port cost' ""
		COST=$ans

		echo
		echo -n 'Node group '${IPADD}'/'${SIGBIT}' is on interface '${NGIF}
		if [ -z "${COST}" ]
		then
			echo ' and has the same cost as the port.'
		else
			echo ' and has a cost of '${COST}'.'
		fi

		getparm 'Add this node group to config file? ' y
		if [ "$ans" = "n" -o "$ans" = "N" ]
		then
			continue
		fi
		if [ -z "${COST}" ]
		then
			echo 'nodegroup='${IPADD} ${SIGBIT} ${NGIF} >> ${OUTFILE}
		else
			echo 'nodegroup='${IPADD} ${SIGBIT} ${NGIF} ${COST}>> ${OUTFILE}
		fi

	done

echo 'Configuration complete.  Remember to move the config file to /etc/ax25.'
