#!/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 lookup_string
{
    return &gsprintf::lookup_string($language, shift(@_));
}


sub print_txt_usage
{
    local $language = shift(@_);

    local $programname = $options->{'name'};
    local $programargs = $options->{'args'};

    # Find the length of the longest option string
    local $descoffset = 0;
    if (defined($programargs)) {
	$descoffset = &PrintUsage::find_longest_option_string($programargs);
    }

    # Produce the usage information using the data structure above
    print STDERR $programname . ": " . &lookup_string($options->{'desc'}) . "\n\n";
    print STDERR " " . &lookup_string("{common.usage}") . ": $programname";
    print STDERR " " . &lookup_string("{classinfo.params}") . "\n\n";

    # Display the program options, if there are some
    if (defined($programargs)) {
	# Calculate the column offset of the option descriptions
	local $optiondescoffset = $descoffset + 2;  # 2 spaces between options & descriptions

	print STDERR " " . &lookup_string("{common.options}") . ":\n";

	# Display the program options
	&PrintUsage::print_options_txt($language, $programargs, $optiondescoffset);
    }
}


#  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;
    my $language = "";  # Will display in the default language if not set

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

    # Get classifier
    my $classifier = shift (@ARGV);
    if (!defined $classifier || $classifier eq "") {
	print STDERR &lookup_string("{classinfo.no_classifier_name}") . "\n\n";
	&print_txt_usage($language);
	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;

    if ($xml) {
        $classobj->print_xml_usage($language);
    }
    else {
	print STDERR "\n$classifier " . &lookup_string("{classinfo.info}") . ":\n\n";
	print STDERR &lookup_string("{classinfo.passing_options}") . "\n\n";
	print STDERR &lookup_string("{classinfo.option_types}") . ":\n\n";
	print STDERR &lookup_string("{classinfo.specific_options}") . "\n\n";
	print STDERR &lookup_string("{classinfo.general_options}") . "\n\n";

	$classobj->print_txt_usage($language);
    }
}


&main ();
