#!/usr/bin/perl 

$VERSION = '0.3a';

$PATH_SEP = '/';

$RECURSION_LIMIT = 100;

$opt_outdir = '.';
$opt_export = '';

sub print_usage {
    print <<"EOF";
HTMLINC $VERSION - (more or less) compatible to Phase 5
Copyright (C) 2002 Marc 'BlackJack' Rintsch. All rights reversed.

usage: htmlinc.pl <include_dir> <htmlfile(s)>

Examples:
          1:   htmlinc.pl ~/www/inc/ *.html

          2:   cd ~/www
               htmlinc.pl inc/ `find * | grep "\\\\."html`  (recursive)

Report bugs to <marc\@rintsch.de>.
EOF
    exit;
} # print_usage


sub process_file {

    my ($inputname, $level) = @_;
    my $result = '';
    my ($incfile, $match, $postmatch);
    
    if ($level >= $RECURSION_LIMIT) {
        print "Error! Already $level recursive calls and no end.\n";
        print "Seems to be an endless loop.\n";
        exit -1;
    }
    
    print "  reading $inputname ($level)...\n";
    open IN, "<$inputname" or die "can't open $inputname";
    $_ = join("", <IN>);
    close IN;
    
    $INC_START_PATTERN = "\<!--INC:\"([^<]*)\"--\>";
    
    while (/$INC_START_PATTERN/o) {

        $result .= $` . $&;
        $incfile = $1;
        
        $INC_END_PATTERN = "\<!--\/INC:\"$incfile\"--\>";
        
        if (!/$INC_END_PATTERN/) {
            print "Error! No closing $& found!\n";
            exit -1;
        }

        ($match, $postmatch) = ($&, $');
        
        $result .= process_file("$include_dir/$incfile", $level + 1) . $match;

        $_ = $postmatch;
    }

    return $result . $_;
} # process_file

sub export_includes {

    my ($text, $level) = @_;
    my ($incfile, $match, $postmatch);
    
    $INC_START_PATTERN = "\<!--INC:\"([^<]*)\"--\>";
    
    $_ = $text;
    
    while (/$INC_START_PATTERN/o) {
        
        $incfile = $1;
        $_ = $';
        
        $INC_END_PATTERN = "\<!--\/INC:\"$incfile\"--\>";
        
        if (!/$INC_END_PATTERN/) {
            print "Error! No closing $& found!\n";
            exit -1;
        }
        
        print "  writing $incfile ($level)...\n";
        open OUT, ">$incfile" or die "can't open $incfile";
        print OUT $`;
        close OUT;

        $text = $';
        export_includes($`, $level + 1);
        $_ = $text;
    }
} # export_includes

if ($ARGV[0] eq '-v' or $ARGV[0] eq '--version') {
    print "$VERSION\n";
    exit;
}

# enough parameters?
if (@ARGV < 2) { print_usage(); }

if ($ARGV[0] eq '--export') {
    shift @ARGV;
    $opt_export = '*';
}

$include_dir = shift @ARGV;
print "INCDIR: $include_dir\n";

# process all files from commandline
while ($mainfile = shift @ARGV) {

    print "processing $mainfile ...\n";
    if ($opt_export eq '') {
        $output = process_file("$mainfile", 0);
    
        print "  writing $opt_outdir$PATH_SEP$mainfile ...\n";
        open OUT, ">$opt_outdir$PATH_SEP$mainfile";
        print OUT $output;
        close OUT;
    } else {
        print "  reading $mainfile...\n";
        open IN, "<$mainfile" or die "can't open $mainfile";
        $_ = join("", <IN>);
        close IN;
        export_includes($_, 0);
    }
}
