#!/usr/bin/perl

# TODO: allow other xslt processors; eg xalan
if (system("which xsltproc > /dev/null")) {
    print <<EOM
You need xsltproc (part of libxslt) for this.

See http://www.godatabase.org/dev/xml/xsl

EOM
;
    exit 1;
}
my $xsl = shift @ARGV;
my @files = ();
while ($ARGV[0] && $ARGV[0] !~ /^\-(.+)/) {
    push(@files, shift @ARGV);
}

if (!$xsl) {
    print "You must specify an XSLT logical name!\n";
    exit 1;
}
#if (!@files) {
#    print "You must specify an xml file to process!\n";
#    exit 1;
#}
if (!@files) {
  @files = ('-');
}
my $GO_ROOT = $ENV{GO_ROOT};
if (!$GO_ROOT) {
    $GO_ROOT = `dirname $0`;
    chomp $GO_ROOT;
    my @parts = split('/',$GO_ROOT);
    pop @parts;
    pop @parts;
    $GO_ROOT = join('/',@parts);
}
my $xslt_file = "$GO_ROOT/xml/xsl/$xsl.xsl";
if (!-f $xslt_file) {
    print <<EOM
I expected to find a file: "$xslt_file"

You may need to download the XSLT files from the go-dev distribution;
see

  http://www.godatabase.org/dev/xml/xsl

Set the env var GO_ROOT to point to the directory containing the "xml"
rdirectory

EOM
;
    exit 1;
}
system("xsltproc @ARGV $xslt_file @files");
exit 0;

sub usage {
    print <<EOM
go-apply-xslt XSLT-NAME [XML FILE...] [XSLTPROC-OPTIONS...]

examples

  go-apply-xslt oboxml_to_owl my.obo-xml

  go-apply-xslt oboxml_filter my.obo-xml --stringparam namespace cell
EOM
  ;
}
