#!/usr/bin/perl -w

###########################################################################
#
# registerencoding.pl - Add "Encoding" 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 encoding by adding an "Encoding" entry to
#   the $GSDLHOME/etc/main.cfg file. It:
#
# - Parses the three required arguments: encoding name, encoding
#     description, and mapfile name.
# - Checks if the encoding specified is already defined in main.cfg.
# - If it isn't, add a new "Encoding" entry and rewrite main.cfg.
#
###########################################################################


use util;


sub main
{
    # Get the short name of the encoding being registered
    local $encodingname = shift(@_);
    # Get the long name of the encoding being registered
    local $encodingdesc = shift(@_);
    # Get the name of the map file for the encoding being registered
    local $mapfilename = shift(@_);

    # Read any extra arguments
    local $option_multibyte_encoding = 0;
    local $optionalarg = shift(@_);
    while ($optionalarg) {
	if ($optionalarg eq "-multibyte") { $option_multibyte_encoding = 1; }
	$optionalarg = shift(@_);
    }

    # Check that the necessary arguments were supplied
    if (!$encodingname || !$encodingdesc || !$mapfilename) {
	print STDERR "Usage: registerencoding.pl <encoding-name> <encoding-desc> <mapfile-name> [-multibyte]\n";
	die "Error: Required argument missing.\n";
    }

    # Casefold the encoding name
    $encodingname =~ 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 encoding is already defined in the main.cfg file
    local $lastencline;
    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 an Encoding entry?
	if ($line =~ /^Encoding\s+/i) {
	    local @encodingparts = split(/\s+/, $line);

	    # Read the encoding name
	    local $encname = "";
	    for ($j = 1; $j < scalar(@encodingparts); $j++) {
		if ($encodingparts[$j] =~ /^shortname=(.*)$/i) {
		    $encname = $1;
		}
	    }

	    # Casefold the encoding name
	    $encname =~ tr/A-Z/a-z/;

	    # No need to register an encoding that is already defined
	    if ($encname eq $encodingname) {
		# print "Note: Encoding with name '$encodingname' is already defined.\n";
		return;
	    }

	    # Remember the last line containing an Encoding entry
	    $lastencline = $i;
	}
    }

    # The encoding has not been defined, so add a new entry to the main.cfg file
    local $encodingline = "Encoding shortname=$encodingname \"longname=$encodingdesc\"" .
	                    " map=$mapfilename";
    if ($option_multibyte_encoding == 1) {
	$encodingline = $encodingline . " multibyte";
    }
    $maincfglines[$lastencline] = $maincfglines[$lastencline] . $encodingline . "\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);
