#!/usr/bin/perl

require "getopts.pl";

$USAGE = "route_stob.pl -i ascii_input_file -o ascii_output_file";

& Getopts("i:f:o:v");
if(defined $opt_i) {
    $INPUT = $opt_i;
}
if(defined $opt_f) {
    $INPUT = $opt_f;
}
if(defined $opt_o) {
    $OUTPUT = $opt_o;
}
if(defined $opt_v) {
    $DEBUG = 1;
}

if (($INPUT eq "") || ($OUTPUT eq "")) {
    print "Usage: $USAGE\n";
    exit;
}

# handle special stdin and stdout cases
if ($INPUT eq "stdin") {$INPUT = "-";}
if ($OUTPUT eq "stdout") {
    open (OUT, ">-");
}
else {
    open (OUT, "> $OUTPUT") || die "Could not open $OUTPUT : $! \n";
}

&read_input ($INPUT);

sub read_input {
    local ($inputfile) = @_;
    my $month, $mday, $year, $hour, $min, $sec, $src, $dst;
    my $valid, $network, $nexthop, $Metric, $LocPrf, $Weight, $Path, $origin, nexthopAS;
    my $prevNetwork, $time;
    my $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst;

    $time = time;

    if (open (INPUT, $inputfile) < 1) {
	print "Could not open $inputfile\n";
	return;
    }

    printf ("TIME: 02/28/01 10:38:07\nTYPE: TABLE_DUMP/INET\nVIEW: 0\nSEQUENCE: 0\n");
    $line = 0;
    while (<INPUT>) {
	$line++;
	$offset = 0;

	$valid = substr ($_, 0, 3);
	$offset  += 3;

	# Take first 15 chars (xxx.xxx.xxx.xxx), to see whether the
	# network field exist or not.

	$network = substr ($_, $offset, 17);

	# Are there characters other than white spaces?
	if ($network =~ /\S/) {

	  # looking for the location of the last space (white space
	  # which is followed by digit) of the $network. If it
	  # exceedes the boundaries, all other fields are shifted
	  $pos = substr ($_, 3);
	  $pos =~ /\s(\d)/g;
	  $network = substr ($_, $offset, pos($pos)-1);

	  # discard trailing whitespaces from $network
	  $network =~ s/\s+$//;

	  # Check whether it is required to add mask to the network
	  # (if not exist. I think mrt format require mask)
	  unless ($network =~ /\//) {
	    $network =~ s/(.)$/\1\/24/;
	  }
	  $prevNetwork = $network;

	  $offset += (pos($pos)-1);
	} else {
	  $offset += 17;
	  $network = $prevNetwork;
	}
	
	$pos = substr ($_, $offset);
	$pos =~ /\s/g;
	$nexthop = substr ($_, $offset, pos($pos)-1);
	$offset += 15;
	# no nexthop - no valid entry
	exit unless $nexthop =~ /\d/;

	$Metric = substr ($_, $offset, 11);
	$offset += 11;
	#print $Metric;

	$LocPrf = substr ($_, $offset, 7);
	$offset += 7;

	$Weight = substr ($_, $offset, 7);
	$offset += 7;

	chop ($pos);
	$pos = substr ($_, $offset);
	# isolate the origin from the $Path
	if ($pos =~ /[aie\?]/g) {
	  $origin = substr ($_, $offset+(pos($pos)-1));
	  $Path = substr ($_, $offset, pos($pos)-1);
	} else {
	  # no origin attributes. Check whether trailing spaces
	  # exist, and cut them
	  $Path = substr ($_, $offset);
	  chop ($Path);
	}
	# Discard trailing whitespaces
	$Path =~  s/\s+$//;
	# Discard leading whitespaces
	$Path =~ s/^\s+//;
	$nexthopAS = $Path;
	# Isolate the first AS in the list (this is the AS of the peer
	# who sent the update)
	# Discard leading whitespaces
	$nexthopAS =~ s/^\s+//;
	$nexthopAS =~ s/\s.*//;

	#printf ("%s%s%s%s%s%s\n", $network, $nexthop, $Metric, $LocPrf, $Weight, $Path);
	printf ("PREFIX: %s\n", $network);
	printf ("FROM: %s AS%s\n", $nexthop, $nexthopAS);
	$time++;
	($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($time);
	printf ("ORIGINATED: %s/%s/01 %s:%s:%s\n",$mon, $mday, $hour, $min, $sec); 
	#printf ("ORIGINATED: 02/28/01 09:14:19\n");
      SWITCH: {
	  if ($origin =~ /i/) {
	    printf ("ORIGIN: IGP\n");
	    last SWITCH;
	  }
	  if ($origin =~ /e/) {
	    printf ("ORIGIN: EGP\n");
	    last SWITCH;
	  }
	  if ($origin =~ /\?/) {
	    printf ("ORIGIN: INCOMPLETE\n");
	    last SWITCH;
	  }
	}
	printf ("ASPATH: %s\n", $Path);
	printf ("NEXT_HOP: %s\n", $nexthop);
	#printf ("MULTI_EXIT_DISC: 46\n");
	#printf ("STATUS: 0x1\n");
	if ($line eq 100) {
	  $line=0;
	  # Must put here empty line & new view. Otherwise the route_atob crashes
	  print "\n";
	  printf ("TIME: 02/28/01 10:38:07\nTYPE: TABLE_DUMP/INET\nVIEW: 0\nSEQUENCE: 0\n");
	}
      }
  }
