#!/usr/bin/perl -w

###########################################################################
#
# picklanguage.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.
#
###########################################################################

# This code generates the front page of the Greenstone Interface Translation Agency
# which asks the user to pick their base language and translating language before
# proceeding into the translation pages

BEGIN {
    die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
    unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
}

use parsargv;
use util;
use Cwd;
use File::Basename;
use GDBM_File;
use CGI;
#use utf8;

sub main
{
    my $query = new CGI;
    my @languages;

    # directory in which to put the generated html pages
    my $dir = "$ENV{'GSDLHOME'}/tmp/lang/package_forms";
    # creates directory if it doesn't already exist
    if (!-e $dir) {
	my $store_umask = umask(0000);
	if (! mkdir($dir, 0777)) {
	    umask($store_umask);
	    die "Couldn't create directory $dir\n";
	}
	umask($store_umask);
    }
 
    open(LANGFILE, "<$dir/languages.log") or die "MURGH\n";

    #reads into the languages array all languages currently in use
    #and their corresponding language codes from the languages.log file
    while (<LANGFILE>) {
	$lang = $_;
	chomp($lang);
	push(@languages,$lang);
	<LANGFILE>;
    }
    
    close LANGFILE;

    #opens a file to write HTML content of the page to
    open(HTMLFILE, ">$dir/picklanguage.lang") or die "MURGH\n";
    
    #writes to file the instructions on how to use the interface (_textinitial)
    #and then displays a table at the end with menu options from which the user
    #selects their base and translation languages, and submits these choices
    #to proceed into the interface translation agency
    print HTMLFILE (" _textinitial_ <br>\n",
		    "<table border=\"2\" cellpadding=\"8\"><tr><td><center>",
		    "_textselectbase_ <p>\n",
		    $query->popup_menu(-name=>"bl",
				       -values=>\@languages,
				       -default=>'english'),
		    "<p></center></td><td><center>\n",
		    "_textselectforeign_ <p>\n",
		    $query->popup_menu(-name=>"tlng",
				       -values=>\@languages),
		    "<p>\n",
		    "_textselectnew_ <p>\n",
		    $query->textfield(-name=>"ownchoice",
				      -default=>"",
				      -size=>50),
		    "<p></center></td></tr></table>\n",
		    $query->submit("submitlanguage","_textenter_"),
		    "</center><p>\n",
		    "<img src=\"_httpimg_/divb.gif\"><p>");
    
    close HTMLFILE;

}

&main();
