#!/usr/local/bin/perl -w
use XML::XSLT;

## global vars ##
my $noweb = "";
my $project = "";
my $xslfile = "";
my $xmlfile = "";

&process_arguments (@ARGV);
print $/ if $XSLT::debug;
$XSLT::Parser->openproject ($xmlfile, $xslfile);
$XSLT::Parser->process_project;
print "Content-type: text/html$/$/" unless $noweb;
$XSLT::Parser->print_result;

exit;


sub process_arguments {
  my @argv = @_;
  if (@argv) {
    while ((@argv > 1) && ($argv[0] =~ /^\-/)) {
      my $param = shift @argv;

      $XSLT::debug = "active" if ($param =~ /^\-d/i);
      $noweb = "active" if ($param =~ /^\-n/i);
      $XSLT::warnings = "active" if ($param =~ /^\-w/i);
      if (($param =~ /^\-s/i) && ((@argv > 1) && ($argv[0] !~ /^\-/))) {
        $param = shift (@argv);
        $xslfile = $param;
      }

      $noweb = "" if $XSLT::debug;
      $XSLT::warnings = "" if $XSLT::debug;

    }

    print "Content-type: text/html$/$/" if $XSLT::debug;
    print "Debug   : \"",$XSLT::debug,"\"$/NoWeb   : \"$noweb\"$/Warnings: \"",$XSLT::warnings,"\"$/" if $XSLT::debug;

    if (@argv && ($argv[0] !~ /^\-/)) {
      $project = shift (@argv);
      $xmlfile = "$project.xml";
      $xslfile = "$project.xsl" unless $xslfile;
      die "Error: could not open $xmlfile, make sure it exists.$/" if (! (-f $xmlfile));
      die "Error: could not open $xslfile, make sure it exists.$/" if (! (-f $xslfile));
    } else {
      die "Error: no project specified, give a name of a project.$/";
    }
    print "Project : \"",$project,"\"$/  XML-file: \"$xmlfile\"$/  XSL-file: \"$xslfile\"$/" if $XSLT::debug;
  } else {
    die "Usage: $0 [options] <project>$/$/  -d[ebug]\t\tturn debugging mode on$/  -n[oweb]\t\tdon't print content-type$/  -s[tylesheet] <file>\tuse <file> instead of <project>.xsl as template$/  -w[arnings]\t\tturns warnings on$/";
  }
}
