#!/usr/bin/perl -w

###########################################################################
#
# inittranslator.pl
#
# 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 initialises the Greenstone Translator Interface. It:
#
# - Creates a directory for the translator data files, if one doesn't
#     already exist.
# - Reads the languages specified in the etc/main.cfg file.
# - Parses the translator configuration file, reading the files to
#     translate.
# - Writes an HTML subfile for the Translator Interface front page, asking
#     the user to choose their source language, target language, and what
#     to translate.
#
###########################################################################


use translator;
use util;
use CGI;


my $gsdldir = "$ENV{'GSDLHOME'}";


sub main
{
    # Everything needs to be world-writeable
    local $currentmask = umask;
    umask(0000);

    # Create the translator directory, if necessary
    local $translatordir = &translator::get_translator_dir();
    if (!-e $translatordir) {
	if (!mkdir($translatordir, 0777)) {
	    die "Error: Couldn't create directory $translatordir.\n";
	}
    }

    # Log any configuration errors to the general translator log
    local $translatorlogfile = &util::filename_cat($translatordir, "translator.log");
    if (!open(LOG, ">>$translatorlogfile")) {
	die "Error: Could not append to log file $translatorlogfile.\n";
    }

    # Log the command that launched this script
    print LOG "\nCommand: $0 @ARGV\n";

    # Read the language names from the etc/main.cfg file
    local %langhash = &translator::read_languages_from_main_cfg_file();
    # local @languages = sort(keys(%langhash));

    # Read the files to translate from the translator.cfg file
    local %files = &translator::read_translation_files_from_config_file();

    # Write the HTML content of the page to picklanguage.trans (in translator directory)
    local $picklangfile = &util::filename_cat($translatordir, "picklanguage.trans");
    if (!open(PICK_LANG_OUT, ">$picklangfile")) {
	print LOG "Error: Could not write $picklangfile.\n";
	die "\n";
    }

    # Writes to file instructions on how to use the interface (_textinitial_), and
    #   a table with menus from which the user selects their source and target
    #   languages. Submitting these choices proceeds into the Translator Interface.
    local $CGIobj = new CGI;
    print PICK_LANG_OUT (" _textinitial_ <br>\n",
			 "<table border=\"0\" cellpadding=\"8\">\n",

#  			 # "Please select the source language" popup menu
#  			 "<tr>\n",
#  			 "<td>_textselectbase_</td>\n",
#  			 "<td>",
#  			 $CGIobj->popup_menu(-name => "slng",
#  					     -values => \@languages,
#  					     -default => "english"),
#  			 "</td></tr>\n",
			 $CGIobj->hidden(-name => "slng",
					 -default => "en") . "\n",

			 # "Please select the target language" popup menu
			 "<tr>\n",
			 "<td>_textselectforeign_</td>\n",
			 "<td>",
			 $CGIobj->popup_menu(-name => "tlng",
					     -values => \%langhash),
					     # -values => \@languages),
			 "</td></tr>\n",

			 # "Please select what to translate" popup menu
			 "<tr>\n",
			 "<td>_textselectfiletotranslate_</td>\n",
			 "<td>",
			 $CGIobj->popup_menu(-name => "file",
					     -values => \%files),
			 "</td></tr>\n",

			 "</table>\n",

			 # "Enter the Translator Interface" button
			 "<center>\n",
			 $CGIobj->submit("submitlanguage","_textenter_"),
			 "</center><p>\n",
			 "<center><img src=\"_httpimg_/divb.gif\"></center><p>");

    # Clean up
    close(PICK_LANG_OUT);
    umask($currentmask);
}


&main(@ARGV);
