#!/usr/bin/perl

use strict;
use warnings;
use diagnostics;

my @felder;
my $eintrag;
my %rules;
my $rule_name;
my $rule_fwrule;
my $fwrules;
my $entry;
my $zeile;
my $prot;
my @prot_array;
my $port;

%rules = ();

while($zeile = <STDIN>) {
  if ($zeile ne "\n")
  {
    $zeile =~ s/[\s\n\a]+$//;

    ($rule_name, $rule_fwrule) = split(/[[:space:]]+/, $zeile, 2);

      push( @{$rules{$rule_name}}, $rule_fwrule );
  }
}

foreach $rule_name (sort keys %rules) {

  @felder = @{$rules{$rule_name}};

  foreach $entry (@felder) {
    
    @prot_array = ();
    ($prot, $port) = split(/[[:space:]]+/, $entry, 2);
    if($prot =~ m/prot:(.*)/) {
      $prot_array[0] = $1;
    } else {
      $port = $prot;
      $prot_array[0] = "tcp/udp";
#      $prot_array[1] = "udp";
    }

    foreach $entry (@prot_array) {
      print "$rule_name & $entry & $port \\\\\n";
      $rule_name = "";
    }
  }
  print "\\hline\n";
}
