#!/usr/bin/perl
##################################################
# pnd2pod
##################################################

my %PND_COMMANDS = map { $_ => 1 } 
                   qw( figure include );

my $IMAGE_COUNTER   = 1;
my $LISTING_COUNTER = 1;

my $current_file = $ARGV;
my $data         = "";

while(<>) {

    die "Not a pnd file: $ARGV" if 
        $ARGV !~ /\.pnd$/ and $ARGV ne "-";

    if($ARGV ne "-" and $current_file ne $ARGV) {

        # File changed, flush old file
        flush_file($current_file, $data);

        $current_file = $ARGV;
        $data         = "";
    }

    # Check if current line is a pnd line
    if(/^=/) {
        my @fields = split;
        (my $name = shift @fields) =~ s/^=//;

        if($PND_COMMANDS{$name}) {
            # Call pnd handler function
            $data .= $name->(@fields);
        } else {
            $data .= $_;
        }
    } else {
        $data .= $_;
    }
}

flush_file($current_file, $data);

##################################################
sub flush_file {
##################################################
    my ($cur_file, $data) = @_;

    if($cur_file ne "" and 
       $cur_file ne "-") {
        (my $pod_file = $cur_file) =~ s/pnd$/pod/;
        open  OUT, ">$pod_file" or 
            die "Cannot open $pod_file";
        print OUT $data;
        close OUT;
    }

    print $data if $cur_file eq "-";
}

##################################################
sub include {
##################################################
    my ($file, $options) = @_;

    my $ln    = 1;
    my $text  = "";
    (my $name = $file) =~ s#.*/##;

    open INFILE, "$file" or 
        die "Cannot open file <$file>";
    my @infile = <INFILE>;
    close(INFILE);
    
    # No options -- just print it
    unless($options) {
        return join '', @infile;
    }

    my $len    = @infile;
    my $format = "%0" . length($len);

    foreach my $line (@infile) {
        $text .= sprintf "${format}d ", 
                   $ln++ if $options eq "listing";
        $text .= $line;
    }

    chop $text;
    (my $indent_text = $text) =~ s/^/    /mg;
    (my $html_text = $indent_text) =~ s/&/&amp;/g;
    $html_text =~ s/</&lt;/g;
    $html_text =~ s/>/&gt;/g;

    my $ret = <<EOT;

=for html
<H2>Listing $LISTING_COUNTER: $name</H2>
<p>
<PRE>
$html_text
</PRE>
<P>

=for text
Listing $LISTING_COUNTER: $name
$indent_text

=for quark
\@KT: Listing $LISTING_COUNTER: $name
\@LI: [Hier bitte Listing $file.txt einfügen]

EOT

    $LISTING_COUNTER++;
    return $ret;
}

##################################################
sub figure {
##################################################
    my ($file, @caption) = @_;
    my $caption = join ' ', @caption;
    my $ret = <<EOT;
=for html
<p>
<TABLE><TR><TD> <IMG SRC="$file"> <TR><TD>
<I>Abb.$IMAGE_COUNTER: $caption</I>
</TABLE>
<p>

=for text
Abbildung $IMAGE_COUNTER: $caption ("$file")

=for quark
\@Bi:$file
\@B:Abb. $IMAGE_COUNTER: $caption

EOT

    $IMAGE_COUNTER++;
    return $ret;
}
