#!/usr/local/bin/perl
#
# prdbnl.pl  prdb netlist 
#

require "getopts.pl";
require "qradbserver.pl";

&Getopts('H:P:p:hd');

$host = 'toad.merit.edu';
$port = '43';
$sources = 'PRDB';
local(@Routes) = ();
local($pref) = 1;
local(@NSFComms) = ('COMM_NSFNET', 'COMM_ANS', 'COMM_ANS_CORE', 'US_FED' );

if ( $opt_h ) {
    print <<"EOF";

Usage: $0 [-h] [-H<host>] [-P<port>] [-d] [<asnum-list>]

<host>        host to query        (default $host)
<port>        port to use          (default $port)
-d            debug msgs

If there is no asnum-list, then retrieve all routes
with COMM_NSFNET in the PRDB (prdb.db).
If there is a list, return only those in prdb.db with
the specified AS's specified as primary (eg 1:70).

Print out in gated format at the specified preference, like:

   35 masklen 8    preference 102

Example:  $0 -Htoad.merit.edu -P42 70 71 > prdbnl.out

Note: Each . it writes to STDERR means 500 routes written.

EOF
    exit;
}

$port = $opt_P	  if ( $opt_P );
$host = $opt_H    if ( $opt_H );
$pref = $opt_p    if ( $opt_p );

&OpenConnection( $host, $port );

$cmd = "s$sources";
print STDERR "Set sources cmd: '$cmd'...\n" if $opt_d;
if ( ($r = &AskServer( $cmd, *Routes )) == -1 ) {
	print STDERR "\n\n==> ERROR from server on set sources cmd ($cmd): $Routes[0]\n";
	exit -1;
}
@Routes = ();

if ( $#ARGV < 0 ) {    # just print them all from prdb: they are in 4 communities
    local($c);

    foreach $c ( @NSFComms ) {
        local($cnt) = 0;
        $cmd = "h$c";
        print STDERR "\nradbserver cmd '$cmd'...";
	    if ( ($r = &AskServer( $cmd, *Routes )) == $qradbserverErrorReturn ) {
		    print STDERR "\n\n ERROR from server on Routes-of-Comm query ($cmd): $Routes[0]\n";
		    exit $qradbserverErrorReturn;
	    }
	    elsif ( $r == $qradbserverNoKeyReturn ) {
		    print STDERR "\n Nothing found on Routes-of-Comm query ($cmd): $Routes[0]\n";
	    }
	    else {
		    foreach (@Routes) {
                local(@rts) = split;
                print STDERR "$#rts routes to write:";
                foreach $r (@rts) {
			        $r =~ /^([0-9.]*)\/([0-9]*)/; 
			   	    printf STDOUT "\t%16.16s masklen %2.2s preference $pref;\n", $1, $2;
                    ++$cnt;
                    print STDERR "." if ( $cnt % 500 == 499 );
                }
			}
            print STDERR "done.\n";
	    	print STDOUT "\n";		# end of one comm
		}
    }
}
else {
    foreach $as (@ARGV) {
        $cmd = "j1:$as";
        print STDERR "\nradbserver cmd '$cmd'...";
	    if ( ($r = &AskServer( $cmd, *Routes )) == $qradbserverErrorReturn ) {
		    print STDERR "\n\n ERROR from server on Routes-of-Comm query ($cmd): $Routes[0]\n";
		    exit $qradbserverErrorReturn;
	    }
	    elsif ( $r == $qradbserverNoKeyReturn ) {
		    print STDERR "\n Nothing found on Routes-of-Comm query ($cmd): $Routes[0]\n";
	    }
	    else {
		    foreach (@Routes) {
                local(@rts) = split;
                print STDERR "$#rts routes to write:";
                foreach $r (@rts) {
			        $r =~ /^([0-9.]*)\/([0-9]*)/; 
			   	    printf STDOUT "\t%16.16s masklen %2.2s preference $pref;\n", $1, $2;
                    ++$cnt;
                    print STDERR "." if ( $cnt % 500 == 499 );
                }
			}
            print STDERR "done.\n";
	    	print STDOUT "\n";	
		}
    }
}

&CloseConnection( $host, $port );
