#!/usr/bin/perl -w

###########################################################################
#
# registerlanguage.pl - Add "Language" entry to etc/main.cfg file
#
# 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'};
    unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
}


###########################################################################
#
# CODE SYNOPSIS
#
# This script registers a new interface language by adding a "Language"
#   entry to the $GSDLHOME/etc/main.cfg file. It:
#
# - Parses the three required arguments: language name, ISO 639 language
#     code, and encoding name.
# - Checks if the language specified is already defined in main.cfg.
# - If it isn't, add a new "Language" entry and rewrite main.cfg.
#
###########################################################################


use util;


sub main
{
    # Get the code of the language being registered
    local $languagecode = shift(@_);
    # Get the name of the language being registered
    local $languagename = shift(@_);
    # Get the name of the encoding for the language being registered
    local $encodingname = shift(@_);

    # Check that the necessary arguments were supplied
    if (!$languagecode || !$languagename || !$encodingname) {
	print STDERR "Usage: registerlanguage.pl <language-code> <language-name> <encoding-name>\n";
	die "Error: Required argument missing.\n";
    }

    # Casefold the language code and name
    $languagecode =~ tr/A-Z/a-z/;
    $languagename =~ tr/A-Z/a-z/;

    # Open and read the etc/main.cfg file
    local $gsdldir = "$ENV{'GSDLHOME'}";
    local $maincfgfile = &util::filename_cat($gsdldir, "etc", "main.cfg");
    open(MAIN_CFG_FILE, "<$maincfgfile") or die "Error: Could not open $maincfgfile.\n";
    local @maincfglines = <MAIN_CFG_FILE>;
    close(MAIN_CFG_FILE);

    # Check if the language is already defined in the main.cfg file
    local $lastlangline;
    for ($i = 0; $i < scalar(@maincfglines); $i++) {
	local $line = $maincfglines[$i];

	# Remove any nasty whitespace at the end of the line
	$line =~ s/(\s*)$//;

	# Does this line contain a Language entry?
	if ($line =~ /^Language\s+/i) {
	    local @languageparts = split(/\s+/, $line);

	    # Read the language code and name
	    local $langcode = "";
	    local $langname = "";
	    for ($j = 1; $j < scalar(@languageparts); $j++) {
		if ($languageparts[$j] =~ /^shortname=(.*)$/i) {
		    $langcode = $1;
		}
		if ($languageparts[$j] =~ /^longname=(.*)$/i) {
		    $langname = $1;
		}
	    }

	    # Casefold the language code and name
	    $langcode =~ tr/A-Z/a-z/;
	    $langname =~ tr/A-Z/a-z/;

	    # No need to register a language that is already defined
	    if ($langcode eq $languagecode) {
		# print "Note: Language with code '$languagecode'";
		# if ($langname eq $languagename) {
		#     print " and name '$languagename'";
		# }
		# print " is already defined.\n";
		return;
	    }

	    # Remember the last line containing a Language entry
	    $lastlangline = $i;
	}
    }

    # The language has not been defined, so add a new entry to the main.cfg file
    local $languagenametc = $languagename;
    substr($languagenametc, 0, 1) =~ tr/a-z/A-Z/;  # Change language name to title case
    local $languageline = "Language shortname=$languagecode longname=$languagenametc" .
	                   " default_encoding=$encodingname";
    $maincfglines[$lastlangline] = $maincfglines[$lastlangline] . $languageline . "\n";

    # Rewrite the (modified) main.cfg file
    open(MAIN_CFG_FILE, ">$maincfgfile") or die "Error: Could not write $maincfgfile.\n";
    foreach $line (@maincfglines) {
	print MAIN_CFG_FILE "$line";
    }
    close(MAIN_CFG_FILE);
}


&main(@ARGV);
