#!/usr/bin/perl -w

###########################################################################
#
# classinfo.pl -- provide information about classifiers
#
# A component of the Greenstone digital library software
# from the New Zealand Digital Library Project at the 
# University of Waikato, New Zealand.
#
# Copyright (C) 1999 New Zealand Digital Library Project
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
###########################################################################


BEGIN {
    die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
    die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
    unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
    unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan");
    unshift (@INC, "$ENV{'GSDLHOME'}/perllib/classify");
}

use classify;
use util;
use parsargv;
use gsprintf;
use printusage;


my $arguments =
    [ { 'name' => "collect",
	'desc' => "{classinfo.collect}",
	'type' => "string",
	'reqd' => "no" },
      { 'name' => "xml",
	'desc' => "{scripts.xml}",
	'type' => "flag",
	'reqd' => "no" },
      { 'name' => "language",
	'desc' => "{scripts.language}",
	'type' => "string",
	'reqd' => "no" } ];

my $options = { 'name' => "classinfo.pl",
		'desc' => "{classinfo.desc}",
		'args' => $arguments };

sub gsprintf
{
    return &gsprintf::gsprintf(@_);
}


#  sub print_usage {
#      print STDERR "\n";
#      print STDERR "classinfo.pl: Prints information about a classifier.\n\n";
#      print STDERR " usage: $0 [options] classifier\n\n";
#      print STDERR " options:\n";
#      print STDERR "  -collect collection-name  Giving a collection name will make classinfo.pl\n";
#      print STDERR "                            look in collect/collection-name/perllib/classify\n";
#      print STDERR "                            first. If the classifier is not found there it will\n";
#      print STDERR "                            look in the general perllib/classify directory.\n\n";
#      print STDERR "  -xml                      Produces the information in an xml form, without\n";
#      print STDERR "                            'pretty' comments but with much more detail.\n\n";
#      print STDERR "  -language language-code   Language to display classifer options in (eg. 'en_US'\n";
#      print STDERR "                            specifies American English). Requires translations\n";
#      print STDERR "                            of the option descriptions to exist in the\n";
#      print STDERR "                            perllib/strings_language-code.rb file.\n";
#  }


sub main {
    my $collect = "";
    my $xml = 0;

    # Parse command line
    if (!parsargv::parse(\@ARGV,
			 q^collect/.*/^, \$collect,
			 q^xml^, \$xml,
			 q^language/.*/^, \$language))
    {
	&PrintUsage::print_txt_usage($options, "{classinfo.params}", 1);
	die "\n";
    }

    # If $language has been specified, load the appropriate resource bundle
    # (Otherwise, the default resource bundle will be loaded automatically)
    if ($language) {
	&gsprintf::load_language_specific_resource_bundle($language);
    }

    # Get classifier
    my $classifier = shift (@ARGV);
    if (!defined $classifier || $classifier eq "") {
	&gsprintf(STDERR, "{classinfo.no_classifier_name}\n\n");
	&PrintUsage::print_txt_usage($options, "{classinfo.params}", 1);
	die "\n";
    }

    # make sure the classifier is loaded from the correct location - a hack.
    if ($collect ne "") {
	$ENV{'GSDLCOLLECTDIR'} = &util::filename_cat ($ENV{'GSDLHOME'}, "collect", $collect);
    } else {
	$ENV{'GSDLCOLLECTDIR'} = $ENV{'GSDLHOME'};
    }
 
    #my $classinfo = &classify::load_classifiers ([[$classifier]]);
    #my $classobj = shift @$classinfo;
    my $classobj = &classify::load_classifier_for_info ($classifier);
    if ($xml) {
        $classobj->print_xml_usage();
    }
    else {
	&gsprintf(STDERR, "\n{classinfo.passing_options}\n\n");
	&gsprintf(STDERR, "{classinfo.option_types}:\n\n");
	&gsprintf(STDERR, "{classinfo.specific_options}\n\n");
	&gsprintf(STDERR, "{classinfo.general_options}\n\n");
	&gsprintf(STDERR, "$classifier {classinfo.info}:\n\n");

	$classobj->print_txt_usage();
    }
}


&main ();
