#!/usr/bin/perl
#
#  ns2latex.pl <netz>
#
#  Bsp: ns2latex.pl 193.141.21
#

$IP = shift;

while (<>) {
    next if (/^$/);
    chop;
    if (/^;;/) {
	($d1, $d2, $nr, $desc) = split(/;/, $_);
	$host[$nr] = $desc;
    }
    next if (/^;/);
    ($nr, $d1, $d2, $host) = split(/\s+/, $_);
    if (($d1 eq "IN") && ($d2 eq "PTR")) {
	chop $host if ( $host =~ /\.$/);
	$host[$nr] = $host;
    }
}

#$dots='\multiput(4,-3)(0,2){6}{\begin{picture}(0,0)\multiput(0,0)(-2,0){45}{\circle*{}}\end{picture}}';

&PrintDocHead();
&PrintSubnet(0,63);
&PrintSubnet(64,127);
&PrintSubnet(128,191);
&PrintSubnet(192,255);
&PrintDocEnd();

exit 0;

# -----------------------------------------------------------------------

sub PrintDocHead {
    print <<'ZZ';
\documentstyle[a4,german,11pt]{article}
    
\begin{document}

\setlength{\parskip}{3ex}
\begin{center}

ZZ
}

# -----------------------------------------------------------------------

sub PrintDocEnd {
    print <<'ZZ';

\end{center}
\end{document}

ZZ
}

# -----------------------------------------------------------------------

sub PrintSubnet {
    local($start, $end) = @_;
    
    $s1=$start;
#    $s2=$start+32;
    $e1=$end-32;
#    $e2=$end;

    print <<"ZZ";

{\\Huge IP-Nummern}

{\\Large Netz: $IP.0}

{\\Large Bereich: $start-$end}

\\today

\\begin{tabular}{||l|r||c||l|r||}
\\hline \\hline
IP-Nummer & Rechnername & \\verb.   . & IP-Nummer & Rechnername\\\\
\\hline \\hline
ZZ
			
    for $h1 (($s1)..($e1)) {
	$h2=$h1+32;

	$host1 = &PrintHost($h1);
	$host2 = &PrintHost($h2);

	print "$IP.$h1 & $host1 && $IP.$h2 & $host2 \\\\";
	if (($h1+1) % 8 == 0) {
	    print "\\hline \\hline\n";
	} else {
	    print "\\cline{1-2} \\cline{4-5}\n";
	}
    }

    print <<'ZZ';
\end{tabular}
\newpage
ZZ

}


sub PrintHost {
    local($nr) = @_;
    
    if ( $host[$nr] eq "NETWORK" ) {
	return "{\\bf $host[$nr]}";
    } elsif ( $host[$nr] eq "BROADCAST" ) {
	return "{\\bf $host[$nr]}";
    } elsif ( $host[$nr] =~ /^\(/) {
	return "{\\it $host[$nr]}";
    } else {
	return $host[$nr];
    }
}


