#!/usr/bin/perl -w

###########################################################################
#
# registermacrofile.pl - Add macrofile 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 macrofile by adding it to the "macrofiles"
#   list in the $GSDLHOME/etc/main.cfg file. It:
#
# - Parses the required argument: macrofile name.
# - Checks if the macrofile specified is already defined in main.cfg.
# - If it isn't, add a new macrofile entry and rewrite main.cfg.
#
###########################################################################


use util;


sub main
{
    # Get the name of the macrofile being registered
    local $macrofilename = shift(@_);

    # Check that the necessary arguments were supplied
    if (!$macrofilename) {
	print STDERR "Usage: registermacrofile.pl <macrofile-name>\n";
	die "Error: Required argument missing.\n";
    }

    # Casefold the macrofile name
    $macrofilename =~ 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 macrofile is already specified in the main.cfg file
    local $lastmacroline;
    local $inmacrofiles = 0;
    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 signal the start of the list of macrofiles?
	if ($line =~ /^macrofiles\s+/i) {
	    $inmacrofiles = 1;
	    $line =~ s/^macrofiles\s+//i;
	}

	if ($inmacrofiles == 1) {
	    # Remove any whitespace before the macros
	    $line =~ s/^\s*//;

	    # If there isn't a trailing '\' on this line, it's the last line
	    if ($line !~ /\\$/) {
		$lastmacroline = $i;
		$inmacrofiles = 0;
	    }
	    # Otherwise remove the '\'
	    else {
		$line =~ s/\\$//;
	    }

	    # Casefold the line
	    $line =~ tr/A-Z/a-z/;
	    local @macrofiles = split(/\s+/, $line);

	    # No need to register a macrofile that is already defined
	    for ($j = 0; $j < scalar(@macrofiles); $j++) {
		if ($macrofiles[$j] eq $macrofilename) {
		    # print "Note: Macrofile '$macrofilename' is already defined.\n";
		    return;
		}
	    }
	}
    }

    # The macrofile has not been defined, so add a new entry to the main.cfg file
    $maincfglines[$lastmacroline] =~ s/(\s*)$//;
    if (length($maincfglines[$lastmacroline]) > 60) {
	$maincfglines[$lastmacroline] .= " \\\n";
	$maincfglines[$lastmacroline] .= "               ";
    }
    $maincfglines[$lastmacroline] .= " $macrofilename\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);
