#!/usr/bin/perl

while ($_ = $ARGV[0]) {
  $infile = shift(@ARGV);
  $infile =~ /\.l(\w+)$/ && ($outfile = $` . ".$1.tex");
  &lit2pgm($infile,$outfile);
}


sub lit2pgm {
  local($infile,$outfile) = @_;

  open(PGM,$infile)        || die "litTopgm: Unable to open $infile";
  open(OUT,"> " . $outfile)|| die "litTopgm: Unable to open $outfile";
  while (<PGM>) {
    print OUT $_;
    if (/^\s*\\begin\{code\}\s*$/) {
       while (<PGM>) {
         if (/^\s*\\end{code}\s*$/) {
            print OUT $_;
            last;
         } else {
            s/\\/\\\\/g;
            s/\{/\\{/g;
            s/\}/\\}/g;
            s/^\\{/ \\{/g;
            s/^\\}/ \\}/g;
            print OUT $_;
         }
       }
    }
  }
  close(PGM);
  close(OUT);

}
