#!/usr/bin/perl

use Config;

my $outscript = 'wrapper.pl';
my($flg,$ubf) = (0,0);

for(my $i=0;$i<@ARGV;$i++){
  if($ARGV[$i] =~ /^[\-\/]([cexu]+)$/){
    my $opt = $1;
    $flg |= 4 if $opt =~ /c/;
    $flg |= 2 if $opt =~ /x/;
    $flg |= 1 if $opt =~ /e/;
    $ubf |= 1 if $opt =~ /u/;
  } elsif($ARGV[$i] =~ /^\-o$/ && $i < @ARGV){
    $outscript = $ARGV[++$i];
  }
}

open WRAPPER,">$outscript";
eval{flock(WRAPPER,2);};

print WRAPPER <<_EOW_;
#!$Config{perlpath}

use HTML::EmbeddedPerl;

sub abort{
  my \$msg = shift;
  print STDOUT "Content-Type: text/plain\\r\\n\\r\\nERROR: \$msg\\n";
  exit;
}

my \$htm;

abort 'cannot open file.' unless open HTM, "<\$ENV{PATH_TRANSLATED}";
sysread HTM,\$htm,(-s HTM);
close HTM;

abort 'empty.' unless \$htm;

ep(\\\$htm,$flg,$ubf);
_EOW_

close WRAPPER;

chmod(0755,$outscript);
