#!/usr/local/bin/perl -w

# Copyright 1997 Burton Rosenberg All Rights Reserved

# this perl script merges a data file with a templte
# to form an html file. The
# formatting tags in the template are HTML plus
# substitution patterns, first controlled by a iteration-capable
# <!-- TEMPLATE xxx ... --> template invocation mechanism,
# and second by matching %.. data items in the template and
# the data file.

# Sat May 31 18:14:20 EDT 1997
#   Template invocation has file argument:
#   <!-- FOO file=foo.dat -->

# Thu May 29 10:59:26 EDT 1997
#   Support for named templates:
#      <!-- TEMPLATE FOO  ... 
#      -->
# Tue May 27 14:24:46 EDT 1997
#   End of template must match /^-->/
#   made slightly more general
#   Note: need something other than this! Comments are not nestable in HTML.
# Wed May 14 13:31:25 EDT 1997


unless ( @ARGV==2 ) {
   print "usage: $0 outfile template (see Makefile)" ;
}

($f_html, $f_template ) = @ARGV ;

   if ( -e $f_html ) { unlink $f_html ; }

   open( FT, $f_template ) || die "open: $f_template" ;
   # open( FD, $f_data ) || die "open: $f_data" ;
   open( FH, ">".$f_html ) || die "open: $f_html" ;

   while ( $line = <FT> ) {

      if ( $line =~ /<!-- (\S+) file=(\S+)/ ) {
         $tagname = $1 ;
         $filename = $2 ;
         if ( defined $ft{$tagname} ) {
            # data file
            open( FD, $filename ) || die "open: $filename" ;
            &proc_datafile ($ft{$tagname}) ; 
            close FD ;
            next ;
         }
      }
      
      if ( $line =~ /<!-- DATE -->/ ) {
         $thedate = `date` ;
         chop $thedate ;
         $line =~ s/<!-- DATE -->/$thedate/ ;
         print FH $line ;
      }
      elsif ( $line =~ /<!-- TEMPLATE\s+(\S+)/ ) {
         $template_name = $1 ;
         $ft{$template_name} = &getTemplate ; 
      }
      else { print FH $line ; }
   }

   close FT ;
   close FD ;
   close FH ;

   system ("chmod a+r $f_html") ;
   system ("chmod g+w $f_html") ;

sub getTemplate {
   my $template = "" ;
   while ( $line = <FT> ) {
      if ( $line =~ /^-->/ ) {
         return $template ;
      }
      $template = $template . $line ;
   }
   $template ;
}

sub proc_datafile {
   my $ft = shift ;
   my %d_hash ;
   my @outputlines ;
   my $i ;
   my $item ;

   while ( &get_data(\%d_hash) ) {

     # look for all %.. in $ft and replace with hash data
     @outputlines = split("\n",$ft) ;
     for ( $i=0; $i<@outputlines; $i++ ) {
         if ( $outputlines[$i] =~ /(%..)/ ) {
            $item = $1 ;
            if ( defined  $d_hash{$item} ) {
                $outputlines[$i] =~ s/(%..)/$d_hash{$item}/ ; 
            }
            else {
                $outputlines[$i] = "" ; 
            }
         }
     }

     for ( $i=0; $i<@outputlines; $i++ ) {
        unless ( $outputlines[$i] eq "" ) { 
           print FH $outputlines[$i], "\n" ;
        }
     }
     print FH "\n" ;

     %d_hash = {} ;
   }
}

sub get_data {
  my $d_hash_ref = shift ;
  my $line ;
  my $ok = 0 ;

  while ( $line = <FD> ) {
     $line =~ /^#/ && next ;
     if ( $line =~ /(%..) (.+)/ ) {
        $ok++ ;
        $d_hash_ref->{$1} = $2 ;
     }
     if ( ($line =~ /^\s*$/) && $ok ) {
        last ;
     }
  }

  $ok ;

}


