#!/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

[This script can easily be modified so that other xslt processors can
be used; eg java Xalan. I believe xsltproc to be the fastest. If you would
like this script to be extended to use a different xslt processor, please
email the gmod-ontol-sw-devel list]

EOM
;
    exit 1;
}
if (!@ARGV || $ARGV[0] =~ /^\-\-help/) {
    print usage();
    exit 0;
}
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 = ('-');
}

# if GO_ROOT is set then this specifies the location of the xslt dir
#  if it is not set, assume we are using an installed version of go-perl,
#  in which case, the xslts will be located along with the perl modules
my $xslt_file;
my $GO_ROOT = $ENV{GO_ROOT};
if ($GO_ROOT) {
    # env var takes precedence;
    # developers should use this
    $xslt_file = "$GO_ROOT/xml/xsl/$xsl.xsl";
}

# default location is with perl modules
if (!$xslt_file || !-f $xslt_file) {
    # user-mode; xsl will be wherever the GO modules are installed
    require "GO/Parser.pm";
    my $dir = $INC{'GO/Parser.pm'};
    $dir =~ s/Parser\.pm/xsl/;
    $xslt_file = "$dir/$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...]

processes Obo-XML files through an XSLT pipeline

examples:

  go-apply-xslt oboxml_to_owl my.obo-xml
  go-apply-xslt oboxml_filter my.obo-xml --stringparam namespace cell

If your input format is not obo-xml, you can transform it in a pipeline:

  go2obo_xml so.obo | go-apply-xslt text_html -

Which is the same as:

  go2xml -x text_html so.obo

EOM
  ;
}
