#!/usr/bin/perl 

# 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
# 0.04		Uses perl
#==================================================================
$AXPORTS="/etc/ax25/axports";
$OUTFILE="rspfd.conf";
$hostname=`hostname`;
$date=`date`;

sub getparm(@) {
	my $tmp;
	my ($prompt, $default) = @_;

	print "$prompt [$default] ";
	$tmp = <STDIN>;
	chomp($tmp);
	if (! $tmp =~ /[A-Za-z0-9]/)
	{
	  $tmp=$default;
	}
	return $tmp;
}

sub speedcost
{
	my ($speed) = @_;

	return 2 if ($speed > 9600);
	return 4 if ($speed > 4800);
	return 8 if ($speed > 1200);
	return 16;

}

sub get_nodegroups
{
  print <<EOF;
 --------------------------------------------------------------------------
 Node Groups
 These are groups of IP addresses that this router will look
 after.  The main router for the area would have an entry for the local
 subnet. Also if you have an ethernet network and what it advertised, put
 it here. Basically use them as static router you want to advertise.
.
EOF

LOOP:  while (1)
  {
    $yorn = getparm('Do you want to add a nodegroup', 'n');
    if ($yorn =~ /^[Yy]/)
    {
      print <<EOF;
 Node groups have three or four components:
   1) IP network address
   2) Significant bits (eg 255.0.0.0 is 8, 255.255.255.128 is 129)
   3) Interface name
   4) Metric/Cost, the default is the interface cost

EOF
      $ipa = getparm('Enter IP network address', '192.168.1.0');
      $sigbits = 0;
      while($sigbits < 1 || $sigbits > 32)
      {
        $sigbits = getparm('Enter significant bits (must be between 1 and 32, inclusive)','32');
      }
      $ngif = getparm('Enter interface the node group goes on','ax0');
      $ngcost = getparm('Metric/Cost of the node group (hit enter to default to port cost)', '');
      print "\nNode group $ipa / $sigbits is on interface $ngif";
      if (-z $ngcost) {
        print " and has the same cost as the interface.\n";
      } else {
        print " and has a cost of $ngcost.\n";
      }
      $yorn = getparm('Add this node group to config file','y');
      if ($yorn =~ /^[Yy]/)
      {
        if (-z $ngcost) {
          print FP "nodegroup=$ipa $sigbits $ngif\n";
        } else {
          print FP "nodegroup=$ipa $sigbits $ngif $ngcost\n";
        }
      }
    } else {
      last LOOP;
    }
  }
}
########################################################
#
# Main routine
#
#print <<EOF
# RSPFd configuration programs v 0.02
# .
# This program automagically creates a config file for the RSPF daemon.
# It needs the /etc/ax25/axports file to find the interfaces.
#EOF

print "fff";
open (FP, ">$OUTFILE") or die "Unable to open output file $OUTFILE : $! \n";

print FP "# Configuration file for $hostname\n";
print FP "# Automatically created by rspfd-configure on $date\n";
print FP "#\n";

open (IFP, "<$AXPORTS") or die "Unable to open file $AXPORTS for reading: $!\n";
$portcount=1;
while (defined($line = <IFP>))
{
	if ($line =~ /^([^d]\S*)\s+(\S+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(.*)$/)
	{
		$call = $2;
		$speed = $3;
		$desc = $6;
		$iface = `/sbin/ifconfig | grep $call | cut -f 1 -d ' '`;
		print "---------------------------------------------------------------------------\n";
		print "Port: $portcount\tCallsign: $call\tSpeed: $speed\n($desc)\n\n";
		$yorn = getparm('Configure this port for RSPF', 'y');
		if ($yorn =~ /^[Yy]/)
		{
			if (! $iface =~ /[A-Za-z]/)
			{
				$iface = getparm('Interface name for this port', 'ax0');
			}
			$speedcost = speedcost($speed);
			$speedcost = getparm('Metric/Cost for this port',$speedcost);
			print FP "#\n";
			print FP "#Port:$portcount Callsign:$call Speed:$speed\n";
			print FP "rspfiface=$iface $speedcost\n";
			$portcount++;
		}
	}
}
get_nodegroups;
close IFP;
close FP;
