#!perl -w
# Need to specify the full path of Perl above


use gsdlCGI;
use strict;


my $debugging_enabled = 0;

my $mail_enabled = 0;
my $mail_to_address = "user\@server";  # Set this appropriately
my $mail_from_address = "user\@server";  # Set this appropriately
my $mail_smtp_server = "smtp.server";  # Set this appropriately


sub main
{
    my $gsdl_cgi = new gsdlCGI();

    # Load the Greenstone modules that we need to use
    $gsdl_cgi->setup_gsdl();
    my $gsdlhome = $ENV{'GSDLHOME'};
    $gsdl_cgi->checked_chdir($gsdlhome);
    require "$gsdlhome/perllib/util.pm";  # This is OK on Windows
    require "$gsdlhome/perllib/cpan/Crypt/UnixCrypt.pm";  # This is OK on Windows

    # Encrypt the password
    if (defined $gsdl_cgi->param("pw")) {
	$gsdl_cgi->param('-name' => "pw", '-value' => &Crypt::UnixCrypt::crypt($gsdl_cgi->clean_param("pw"), "Tp"));
    }

    $gsdl_cgi->parse_cgi_args();

    # We don't want the gsdlCGI module to return errors and warnings in XML
    $gsdl_cgi->{'xml'} = 0;

    # Retrieve the (required) command CGI argument
    my $cmd = $gsdl_cgi->clean_param("cmd");
    if (!defined $cmd) {
	$gsdl_cgi->generate_error("No command specified.");
    }
    $gsdl_cgi->delete("cmd");

    # The check-installation command has no arguments
    if ($cmd eq "check-installation") {
	&check_installation($gsdl_cgi);
	return;
    }

    # All other commands require a username, for locking and authentication
    my $username = $gsdl_cgi->clean_param("un");
    if ((!defined $username) || ($username =~ m/^\s*$/)) {
	$gsdl_cgi->generate_error("No username specified.");
    }
    # Remove the un argument (since this can mess up other scripts)
    $gsdl_cgi->delete("un");

    # Get then remove the ts (timestamp) argument (since this can mess up other scripts)
    my $timestamp = $gsdl_cgi->clean_param("ts");
    if ((!defined $timestamp) || ($timestamp =~ m/^\s*$/)) {
	$timestamp = time();  # Fall back to using the Perl time() function to generate a timestamp
    }
    $gsdl_cgi->delete("ts");

    if ($cmd eq "delete-collection") {
	&delete_collection($gsdl_cgi, $username, $timestamp);
    }
    elsif ($cmd eq "download-collection") {
	&download_collection($gsdl_cgi, $username, $timestamp);
    }
    elsif ($cmd eq "download-collection-archives") {
	&download_collection_archives($gsdl_cgi, $username, $timestamp);
    }
    elsif ($cmd eq "download-collection-configurations") {
	&download_collection_configurations($gsdl_cgi, $username, $timestamp);
    }
    elsif ($cmd eq "download-collection-file") {
	&download_collection_file($gsdl_cgi, $username, $timestamp);
    }
    elsif ($cmd eq "delete-collection-file") {
	&delete_collection_file($gsdl_cgi, $username, $timestamp);
    }
    elsif ($cmd eq "get-script-options") {
	&get_script_options($gsdl_cgi, $username, $timestamp);
    }
    elsif ($cmd eq "move-collection-file") {
	&move_collection_file($gsdl_cgi, $username, $timestamp);
    }
    elsif ($cmd eq "new-collection-directory") {
	&new_collection_directory($gsdl_cgi, $username, $timestamp);
    }
    elsif ($cmd eq "run-script") {
	&run_script($gsdl_cgi, $username, $timestamp);
    }
    elsif ($cmd eq "timeout-test") {
	while (1) { }
    }
    elsif ($cmd eq "upload-collection-file") {
	&upload_collection_file($gsdl_cgi, $username, $timestamp);
    }
    else {
	$gsdl_cgi->generate_error("Unrecognised command: '$cmd'");
    }
}


sub authenticate_user
{
    my $gsdl_cgi = shift(@_);
    my $username = shift(@_);
    my $collection = shift(@_);

    # Remove the pw argument (since this can mess up other scripts)
    my $user_password = $gsdl_cgi->clean_param("pw");
    $gsdl_cgi->delete("pw");

    if ((!defined $user_password) || ($user_password =~ m/^\s*$/)) {
	$gsdl_cgi->generate_error("Authentication failed: no password specified.");
    }

    my $gsdlhome = $ENV{'GSDLHOME'};
    my $etc_directory = &util::filename_cat($gsdlhome, "etc");
    my $users_db_file_path = &util::filename_cat($etc_directory, "users.db");

    # Use db2txt instead of GDBM_File to get the user accounts information
    my $users_db_content = "";
    open(USERS_DB, "db2txt \"$users_db_file_path\" |");
    while (<USERS_DB>) {
	$users_db_content .= $_;
    }

    # Get the user account information from the users.db database
    my %users_db_data = ();
    foreach my $users_db_entry (split(/-{70}/, $users_db_content)) {
	if ($users_db_entry =~ /\n?\[(.+)\]\n/) {
	    $users_db_data{$1} = $users_db_entry;
	}
    }

    # Check username
    my $user_data = $users_db_data{$username};
    if (!defined $user_data) {
	$gsdl_cgi->generate_error("Authentication failed: no account for user '$username'.");
    }

    # Check password
    my ($valid_user_password) = ($user_data =~ /\<password\>(.*)/);
    if ($user_password ne $valid_user_password) {
	$gsdl_cgi->generate_error("Authentication failed: incorrect password.");
    }

    # Check group
    my ($user_groups) = ($user_data =~ /\<groups\>(.*)/);
    if ($collection eq "") {
	# If we're not editing a collection then the user doesn't need to be in a particular group
	return $user_groups;  # Authentication successful
    }
    foreach my $user_group (split(/\,/, $user_groups)) {
	# Does this user have access to all collections?
	if ($user_group eq "all-collections-editor") {
	    return $user_groups;  # Authentication successful
	}
	# Does this user have access to personal collections, and is this one?
	if ($user_group eq "personal-collections-editor" && $collection =~ /^$username\-/) {
	    return $user_groups;  # Authentication successful
	}
	# Does this user have access to this collection
	if ($user_group eq "$collection-collection-editor") {
	    return $user_groups;  # Authentication successful
	}
    }

    $gsdl_cgi->generate_error("Authentication failed: user is not in the required group.");
}


sub lock_collection
{
    my $gsdl_cgi = shift(@_);
    my $username = shift(@_);
    my $collection = shift(@_);

    my $steal_lock = $gsdl_cgi->clean_param("steal_lock");
    $gsdl_cgi->delete("steal_lock");

    my $gsdlhome = $ENV{'GSDLHOME'};
    my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
    $gsdl_cgi->checked_chdir($collection_directory);

    # Check if a lock file already exists for this collection
    my $lock_file_name = "gli.lck";
    if (-e $lock_file_name) {
	# A lock file already exists... check if it's ours
	my $lock_file_content = "";
	open(LOCK_FILE, "<$lock_file_name");
	while (<LOCK_FILE>) {
	    $lock_file_content .= $_;
	}
	close(LOCK_FILE);

	# Pick out the owner of the lock file
	$lock_file_content =~ /\<User\>(.*?)\<\/User\>/;
	my $lock_file_owner = $1;

	# The lock file is ours, so there is no problem
	if ($lock_file_owner eq $username) {
	    return;
	}

	# The lock file is not ours, so throw an error unless "steal_lock" is set
	unless (defined $steal_lock) {
	    $gsdl_cgi->generate_error("Collection is locked by: $lock_file_owner");
	}
    }

    my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
    my $current_time = sprintf("%02d/%02d/%d %02d:%02d:%02d", $mday, $mon + 1, $year + 1900, $hour, $min, $sec);

    # Create a lock file for us (in the same format as the GLI) and we're done
    open(LOCK_FILE, ">$lock_file_name");
    print LOCK_FILE "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    print LOCK_FILE "<LockFile>\n";
    print LOCK_FILE "    <User>" . $username . "</User>\n";
    print LOCK_FILE "    <Machine>(Remote)</Machine>\n";
    print LOCK_FILE "    <Date>" . $current_time . "</Date>\n";
    print LOCK_FILE "</LockFile>\n";
    close(LOCK_FILE);
}


# ----------------------------------------------------------------------------------------------------
#   ACTIONS
# ----------------------------------------------------------------------------------------------------


sub check_installation
{
    my ($gsdl_cgi) = @_;

    my $installation_ok = 1;
    my $installation_status = "";

    # Check that Java is installed and accessible
    my $java = $gsdl_cgi->get_java_path();
    my $java_command = "$java -version 2>&1";
    my $java_output = `$java_command`;
    my $java_status = $?;
    if ($java_status < 0) {
	# The Java command failed
	$installation_status = "Java failed -- do you have the Java run-time installed?\n" . $gsdl_cgi->check_java_home() . "\n";
	$installation_ok = 0;
    }
    else {
	$installation_status = "Java found: $java_output";
    }

    # Show the values of some important environment variables
    $installation_status .= "\n";
    $installation_status .= "GSDLHOME: " . $ENV{'GSDLHOME'} . "\n";
    $installation_status .= "GSDLOS: " . $ENV{'GSDLOS'} . "\n";
    $installation_status .= "PATH: " . $ENV{'PATH'} . "\n";

    if ($installation_ok) {
	$gsdl_cgi->generate_ok_message($installation_status . "\nInstallation OK!");
    }
    else {
	$gsdl_cgi->generate_error($installation_status);
    }
}


sub delete_collection
{
    my ($gsdl_cgi, $username, $timestamp) = @_;

    my $collection = $gsdl_cgi->clean_param("c");
    if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
	$gsdl_cgi->generate_error("No collection specified.");
    }

    # Ensure the user is allowed to edit this collection
    &authenticate_user($gsdl_cgi, $username, $collection);

    my $gsdlhome = $ENV{'GSDLHOME'};
    my $collect_directory = &util::filename_cat($gsdlhome, "collect");
    $gsdl_cgi->checked_chdir($collect_directory);

    # Check that the collection exists
    if (!-d $collection) {
	$gsdl_cgi->generate_error("Collection $collection does not exist.");
    }

    # Make sure the collection isn't locked by someone else
    &lock_collection($gsdl_cgi, $username, $collection);

    $gsdl_cgi->checked_chdir($collect_directory);
    $gsdl_cgi->local_rm_r("$collection");

    # Check that the collection was deleted
    if (-e $collection) {
	$gsdl_cgi->generate_error("Could not delete collection $collection.");
    }

    $gsdl_cgi->generate_ok_message("Collection $collection deleted successfully.");
}


sub delete_collection_file
{
    my ($gsdl_cgi, $username, $timestamp) = @_;

    my $collection = $gsdl_cgi->clean_param("c");
    if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
	$gsdl_cgi->generate_error("No collection specified.");
    }
    my $file = $gsdl_cgi->clean_param("file");
    if ((!defined $file) || ($file =~ m/^\s*$/)) {
	$gsdl_cgi->generate_error("No file specified.");
    }
    $file =~ s/\|/&util::get_dirsep()/eg;  # Convert the '|' characters into whatever is right for this OS

    # Make sure we don't try to delete anything outside the collection
    if ($file =~ /\.\./) {
	$gsdl_cgi->generate_error("Illegal file specified.");
    }

    # Ensure the user is allowed to edit this collection
    &authenticate_user($gsdl_cgi, $username, $collection);

    my $gsdlhome = $ENV{'GSDLHOME'};
    my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
    $gsdl_cgi->checked_chdir($collection_directory);

    # Make sure the collection isn't locked by someone else
    &lock_collection($gsdl_cgi, $username, $collection);

    # Check that the collection file exists
    if (!-e $file) {
	$gsdl_cgi->generate_ok_message("Collection file $file does not exist.");
    }
    $gsdl_cgi->local_rm_r("$file");

    # Check that the collection file was deleted
    if (-e $file) {
	$gsdl_cgi->generate_error("Could not delete collection file $file.");
    }

    $gsdl_cgi->generate_ok_message("Collection file $file deleted successfully.");
}


sub download_collection
{
    my ($gsdl_cgi, $username, $timestamp) = @_;

    my $collection = $gsdl_cgi->clean_param("c");
    if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
	$gsdl_cgi->generate_error("No collection specified.");
    }

    # Ensure the user is allowed to edit this collection
    &authenticate_user($gsdl_cgi, $username, $collection);

    my $gsdlhome = $ENV{'GSDLHOME'};
    my $collect_directory = &util::filename_cat($gsdlhome, "collect");
    $gsdl_cgi->checked_chdir($collect_directory);

    # Check that the collection exists
    if (!-d $collection) {
	$gsdl_cgi->generate_error("Collection $collection does not exist.");
    }

    # Make sure the collection isn't locked by someone else
    &lock_collection($gsdl_cgi, $username, $collection);

    # Zip up the collection
    my $java = $gsdl_cgi->get_java_path();
    my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar");
    my $zip_file_path = &util::filename_cat($collect_directory, $collection . "-" . $timestamp . ".zip");
    my $java_args = "\"$zip_file_path\" \"$collect_directory\" \"$collection\"";
    my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipCollectionShell $java_args"; 

    my $java_output = `$java_command`;
    my $java_status = $?;
    if ($java_status > 0) {
	$gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
    }

    # Check that the zip file was created successfully
    if (!-e $zip_file_path || -z $zip_file_path) {
	$gsdl_cgi->generate_error("Collection zip file $zip_file_path could not be created.");
    }

    &put_file($gsdl_cgi, $zip_file_path, "application/zip");
    unlink("$zip_file_path") unless $debugging_enabled;
}


sub download_collection_archives
{
    my ($gsdl_cgi, $username, $timestamp) = @_;

    my $collection = $gsdl_cgi->clean_param("c");
    if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
	$gsdl_cgi->generate_error("No collection specified.");
    }

    # Ensure the user is allowed to edit this collection
    &authenticate_user($gsdl_cgi, $username, $collection);

    my $gsdlhome = $ENV{'GSDLHOME'};
    my $collect_directory = &util::filename_cat($gsdlhome, "collect");
    $gsdl_cgi->checked_chdir($collect_directory);

    # Check that the collection archives exist
    if (!-d &util::filename_cat($collection, "archives")) {
	$gsdl_cgi->generate_error("Collection archives do not exist.");
    }

    # Make sure the collection isn't locked by someone else
    &lock_collection($gsdl_cgi, $username, $collection);

    # Zip up the collection archives
    my $java = $gsdl_cgi->get_java_path();
    my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar");
    my $zip_file_path = &util::filename_cat($collect_directory, $collection . "-archives-" . $timestamp . ".zip");
    my $java_args = "\"$zip_file_path\" \"$collect_directory\" \"$collection\"";
    my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipCollectionArchives $java_args"; 

    my $java_output = `$java_command`;
    my $java_status = $?;
    if ($java_status > 0) {
	$gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
    }

    # Check that the zip file was created successfully
    if (!-e $zip_file_path || -z $zip_file_path) {
	$gsdl_cgi->generate_error("Collection archives zip file $zip_file_path could not be created.");
    }

    &put_file($gsdl_cgi, $zip_file_path, "application/zip");
    unlink("$zip_file_path") unless $debugging_enabled;
}


# Collection locking unnecessary because this action isn't related to a particular collection
sub download_collection_configurations
{
    my ($gsdl_cgi, $username, $timestamp) = @_;

    # Users can be in any group to perform this action
    my $user_groups = &authenticate_user($gsdl_cgi, $username, "");

    my $gsdlhome = $ENV{'GSDLHOME'};
    my $collect_directory = &util::filename_cat($gsdlhome, "collect");
    $gsdl_cgi->checked_chdir($collect_directory);

    # Zip up the collection configurations
    my $java = $gsdl_cgi->get_java_path();
    my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar");
    my $zip_file_path = &util::filename_cat($collect_directory, "collection-configurations-" . $timestamp . ".zip");
    my $java_args = "\"$zip_file_path\" \"$collect_directory\" \"$username\" \"$user_groups\"";
    my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipCollectionConfigurations $java_args"; 

    my $java_output = `$java_command`;
    my $java_status = $?;
    if ($java_status > 0) {
	$gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
    }

    # Check that the zip file was created successfully
    if (!-e $zip_file_path || -z $zip_file_path) {
	$gsdl_cgi->generate_error("Collection configurations zip file $zip_file_path could not be created.");
    }

    &put_file($gsdl_cgi, $zip_file_path, "application/zip");
    unlink("$zip_file_path") unless $debugging_enabled;
}


sub download_collection_file
{
    my ($gsdl_cgi, $username, $timestamp) = @_;

    my $collection = $gsdl_cgi->clean_param("c");
    if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
	$gsdl_cgi->generate_error("No collection specified.");
    }
    my $file = $gsdl_cgi->clean_param("file");
    if ((!defined $file) || ($file =~ m/^\s*$/)) {
	$gsdl_cgi->generate_error("No file specified.");
    }
    $file =~ s/\|/&util::get_dirsep()/eg;  # Convert the '|' characters into whatever is right for this OS

    # Make sure we don't try to download anything outside the collection
    if ($file =~ /\.\./) {
	$gsdl_cgi->generate_error("Illegal file specified.");
    }

    # Ensure the user is allowed to edit this collection
    &authenticate_user($gsdl_cgi, $username, $collection);

    my $gsdlhome = $ENV{'GSDLHOME'};
    my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
    $gsdl_cgi->checked_chdir($collection_directory);

    # Check that the collection file exists
    if (!-e $file) {
	$gsdl_cgi->generate_error("Collection file $file does not exist.");
    }

    # Make sure the collection isn't locked by someone else
    &lock_collection($gsdl_cgi, $username, $collection);

    # Zip up the collection file
    my $java = $gsdl_cgi->get_java_path();
    my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar");
    my $zip_file_path = &util::filename_cat($collection_directory, $collection . "-file-" . $timestamp . ".zip");
    my $java_args = "\"$zip_file_path\" \"$collection_directory\" \"$file\"";
    my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipFiles $java_args"; 

    my $java_output = `$java_command`;
    my $java_status = $?;
    if ($java_status > 0) {
	$gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
    }

    # Check that the zip file was created successfully
    if (!-e $zip_file_path || -z $zip_file_path) {
	$gsdl_cgi->generate_error("Collection archives zip file $zip_file_path could not be created.");
    }

    &put_file($gsdl_cgi, $zip_file_path, "application/zip");
    unlink("$zip_file_path") unless $debugging_enabled;
}


# Collection locking unnecessary because this action isn't related to a particular collection
sub get_script_options
{
    my ($gsdl_cgi, $username, $timestamp) = @_;

    my $script = $gsdl_cgi->clean_param("script");
    if ((!defined $script) || ($script =~ m/^\s*$/)) {
	$gsdl_cgi->generate_error("No script specified.");
    }
    $gsdl_cgi->delete("script");

    # Users can be in any group to perform this action
    &authenticate_user($gsdl_cgi, $username, "");

    my $perl_args = "";
    if ($script eq "classinfo.pl") {
	$perl_args = $gsdl_cgi->clean_param("classifier") || "";
	$gsdl_cgi->delete("classifier");
    }
    if ($script eq "pluginfo.pl") {
	$perl_args = $gsdl_cgi->clean_param("plugin") || "";
	$gsdl_cgi->delete("plugin");
    }

    foreach my $cgi_arg_name ($gsdl_cgi->param) {
	my $cgi_arg_value = $gsdl_cgi->clean_param($cgi_arg_name) || "";
	$cgi_arg_value = $gsdl_cgi->safe_val($cgi_arg_value);
	if ($cgi_arg_value eq "") {
	    $perl_args = "-$cgi_arg_name " . $perl_args;
	}
	else {
	    $perl_args = "-$cgi_arg_name \"$cgi_arg_value\" " . $perl_args;
	}
    }

    my $perl_command = "perl -S $script $perl_args 2>&1";
    my $perl_output = `$perl_command`;
    my $perl_status = $?;
    if ($perl_status > 0) {
	$gsdl_cgi->generate_error("Perl failed: $perl_command\n--\n$perl_output\nExit status: " . ($perl_status / 256));
    }

    print STDOUT "Content-type:text/plain\n\n";
    print STDOUT $perl_output;
}


sub move_collection_file
{
    my ($gsdl_cgi, $username, $timestamp) = @_;

    my $collection = $gsdl_cgi->clean_param("c");
    if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
	$gsdl_cgi->generate_error("No collection specified.");
    }
    my $source_file = $gsdl_cgi->clean_param("source");
    if ((!defined $source_file) || ($source_file =~ m/^\s*$/)) {
	$gsdl_cgi->generate_error("No source file specified.");
    }
    $source_file =~ s/\|/&util::get_dirsep()/eg;  # Convert the '|' characters into whatever is right for this OS
    my $target_file = $gsdl_cgi->clean_param("target");
    if ((!defined $target_file) || ($target_file =~ m/^\s*$/)) {
	$gsdl_cgi->generate_error("No target file specified.");
    }
    $target_file =~ s/\|/&util::get_dirsep()/eg;  # Convert the '|' characters into whatever is right for this OS

    # Make sure we don't try to move anything outside the collection
    if ($source_file =~ /\.\./ || $target_file =~ /\.\./) {
	$gsdl_cgi->generate_error("Illegal file specified.");
    }

    # Ensure the user is allowed to edit this collection
    &authenticate_user($gsdl_cgi, $username, $collection);

    my $gsdlhome = $ENV{'GSDLHOME'};
    my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
    $gsdl_cgi->checked_chdir($collection_directory);

    # Check that the collection source file exists
    if (!-e $source_file) {
	$gsdl_cgi->generate_error("Collection file $source_file does not exist.");
    }

    # Make sure the collection isn't locked by someone else
    &lock_collection($gsdl_cgi, $username, $collection);

    &util::mv($source_file, $target_file);

    # Check that the collection source file was moved
    if (-e $source_file || !-e $target_file) {
	$gsdl_cgi->generate_error("Could not move collection file $source_file to $target_file.");
    }

    $gsdl_cgi->generate_ok_message("Collection file $source_file moved to $target_file successfully.");
}


sub new_collection_directory
{
    my ($gsdl_cgi, $username, $timestamp) = @_;

    my $collection = $gsdl_cgi->clean_param("c");
    if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
	$gsdl_cgi->generate_error("No collection specified.");
    }
    my $directory = $gsdl_cgi->clean_param("directory");
    if ((!defined $directory) || ($directory =~ m/^\s*$/)) {
	$gsdl_cgi->generate_error("No directory specified.");
    }
    $directory =~ s/\|/&util::get_dirsep()/eg;  # Convert the '|' characters into whatever is right for this OS

    # Make sure we don't try to create anything outside the collection
    if ($directory =~ /\.\./) {
	$gsdl_cgi->generate_error("Illegal directory specified.");
    }

    # Ensure the user is allowed to edit this collection
    &authenticate_user($gsdl_cgi, $username, $collection);

    my $gsdlhome = $ENV{'GSDLHOME'};
    my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
    $gsdl_cgi->checked_chdir($collection_directory);

    # Check that the collection directory doesn't already exist
    # ZipTools doesn't zip up empty directories, so this causes an error when downloading a new collection as we explicity
    # try to create the import directory
#    if (-d $directory) {
#	$gsdl_cgi->generate_error("Collection directory $directory already exists.");
#    }

    # Make sure the collection isn't locked by someone else
    &lock_collection($gsdl_cgi, $username, $collection);

    &util::mk_dir($directory);

    # Check that the collection directory was created
    if (!-d $directory) {
	$gsdl_cgi->generate_error("Could not create collection directory $directory.");
    }

    $gsdl_cgi->generate_ok_message("Collection directory $directory created successfully.");
}


sub run_script
{
    my ($gsdl_cgi, $username, $timestamp) = @_;

    my $script = $gsdl_cgi->clean_param("script");
    if ((!defined $script) || ($script =~ m/^\s*$/)) {
	$gsdl_cgi->generate_error("No script specified.");
    }
    $gsdl_cgi->delete("script");
    my $collection = $gsdl_cgi->clean_param("c");
    if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
	$gsdl_cgi->generate_error("No collection specified.");
    }
    $gsdl_cgi->delete("c");

    # Ensure the user is allowed to edit this collection
    &authenticate_user($gsdl_cgi, $username, $collection);

    # Make sure the collection isn't locked by someone else (unless we're running mkcol.pl, of course)
    &lock_collection($gsdl_cgi, $username, $collection) unless ($script eq "mkcol.pl");

    # Last argument is the collection name, except for explode_metadata_database.pl
    my $perl_args = $collection;
    if ($script eq "explode_metadata_database.pl") {
	# Last argument is the file to be exploded
	my $file = $gsdl_cgi->clean_param("file");
	if ((!defined $file) || ($file =~ m/^\s*$/)) {
	    $gsdl_cgi->generate_error("No file specified.");
	}
	$gsdl_cgi->delete("file");
	$perl_args = $file;
    }

    foreach my $cgi_arg_name ($gsdl_cgi->param) {
	my $cgi_arg_value = $gsdl_cgi->safe_val($gsdl_cgi->clean_param($cgi_arg_name));
	if ($cgi_arg_value eq "") {
	    $perl_args = "-$cgi_arg_name " . $perl_args;
	}
	else {
	    $perl_args = "-$cgi_arg_name \"$cgi_arg_value\" " . $perl_args;
	}
    }

    my $perl_command = "perl -S $script $perl_args 2>&1";
    if (!open(PIN, "$perl_command |")) {
	$gsdl_cgi->generate_error("Unable to execute command: $perl_command");
    }

    print STDOUT "Content-type:text/plain\n\n";
    while (defined (my $perl_output_line = <PIN>)) {
	print STDOUT $perl_output_line;
    }
    close(PIN);

    my $perl_status = $?;
    if ($perl_status > 0) {
	$gsdl_cgi->generate_error("Perl failed: $perl_command\n--\nExit status: " . ($perl_status / 256));
    }
    elsif ($mail_enabled) {
	if ($script eq "buildcol.pl") {
	    &send_mail($gsdl_cgi, "Remote Greenstone building event", "Build of collection '$collection' complete.");
	}
    }
}


sub upload_collection_file
{
    my ($gsdl_cgi, $username, $timestamp) = @_;

    my $collection = $gsdl_cgi->clean_param("c");
    if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
	$gsdl_cgi->generate_error("No collection specified.");
    }
    my $file = $gsdl_cgi->clean_param("file");
    if ((!defined $file) || ($file =~ m/^\s*$/)) {
	$gsdl_cgi->generate_error("No file specified.");
    }
    my $directory = $gsdl_cgi->clean_param("directory") || "";
    $directory =~ s/\|/&util::get_dirsep()/eg;  # Convert the '|' characters into whatever is right for this OS
    my $zip = $gsdl_cgi->clean_param("zip");

    # Make sure we don't try to upload anything outside the collection
    if ($file =~ /\.\./) {
	$gsdl_cgi->generate_error("Illegal file specified.");
    }
    if ($directory =~ /\.\./) {
	$gsdl_cgi->generate_error("Illegal directory specified.");
    }

    # Ensure the user is allowed to edit this collection
    &authenticate_user($gsdl_cgi, $username, $collection);

    my $gsdlhome = $ENV{'GSDLHOME'};
    my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
    $gsdl_cgi->checked_chdir($collection_directory);

    # Make sure the collection isn't locked by someone else
    &lock_collection($gsdl_cgi, $username, $collection);

    my $directory_path = &util::filename_cat($collection_directory, $directory);
    if (!-d $directory_path) {
	&util::mk_dir($directory_path);
	if (!-d $directory_path) {
	    $gsdl_cgi->generate_error("Could not create directory $directory_path.");
	}
    }

    my $file_path = &util::filename_cat($directory_path, $file . "-" . $timestamp);
    if (!open(FOUT, ">$file_path")) {
	$gsdl_cgi->generate_error("Unable to write file $file_path");
    }

    # Read the uploaded data and write it out to file
    my $buf;
    my $num_bytes = 0;
    binmode(FOUT);
    while (read(STDIN, $buf, 1024) > 0) {
	print FOUT $buf;
	$num_bytes += length($buf);
    }
    close(FOUT);

    # If we have downloaded a zip file, unzip it
    if (defined $zip) {
	my $java = $gsdl_cgi->get_java_path();
	my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar");
	my $java_args = "\"$file_path\" \"$directory_path\"";
	my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.remote.Unzip $java_args"; 

	my $java_output = `$java_command`;
	my $java_status = $?;

	# Remove the zip file once we have unzipped it, since it is an intermediate file only
	unlink("$file_path");

	if ($java_status > 0) {
	    $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
	}
    }

    $gsdl_cgi->generate_ok_message("Collection file $file uploaded successfully.");
}


sub put_file
{
    my $gsdl_cgi = shift(@_);
    my $file_path = shift(@_);
    my $content_type = shift(@_);

    if (open(PIN, "<$file_path")) {
	print STDOUT "Content-type:$content_type\n\n";

 	my $buf;
 	my $num_bytes = 0;
 	binmode(PIN);
 	while (read(PIN, $buf, 1024) > 0) {
 	    print STDOUT $buf;
 	    $num_bytes += length($buf);
 	}

 	close(PIN);
    }
    else {
	$gsdl_cgi->generate_error("Unable to read file $file_path\n  $!");
    }
}


sub send_mail
{
    my $gsdl_cgi = shift(@_);
    my $mail_subject = shift(@_);
    my $mail_content = shift(@_);

    my $sendmail_command = "perl -S sendmail.pl";
    $sendmail_command .= " -to \"" . $mail_to_address . "\"";
    $sendmail_command .= " -from \"" . $mail_from_address . "\"";
    $sendmail_command .= " -smtp \"" . $mail_smtp_server . "\"";
    $sendmail_command .= " -subject \"" . $mail_subject . "\"";

    if (!open(POUT, "| $sendmail_command")) {
	$gsdl_cgi->generate_error("Unable to execute command: $sendmail_command");
    }
    print POUT $mail_content . "\n";
    close(POUT);
}


&main();
