#!/usr/bin/perl -w

use gsdlCGI;

sub load_gsdl_utils
{
    my ($gsdlhome) = @_;

    require "$gsdlhome/perllib/util.pm";
}

sub main
{
    my $gsdl_cgi = new gsdlCGI();
    $gsdl_cgi->parse_cgi_args();
    $gsdl_cgi->setup_gsdl();
    my $gsdlhome = $ENV{'GSDLHOME'};

    load_gsdl_utils($gsdlhome);

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

    my $full_col = &util::filename_cat($gsdlhome,"collect",$col);

    #If the collection dir on the server somehow got erased, create it.
    if(!-e $full_col) {
	&util::mk_dir($full_col);
    }

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

    my $delete = $gsdl_cgi->clean_param("del");
    $delete = "all" if((!defined $delete) || ($delete eq ""));

    $dir = "" if (($dir eq ".") || ($dir =~ m/^\.\./));

    if ($col =~ m/^\//) {
	# leading / at start denotes special "cols" such as "/tmp"
	$gsdl_cgi->checked_chdir("$gsdlhome");
	$col =~ s/^\///;
    }
    else {
	$gsdl_cgi->checked_chdir("$gsdlhome/collect");
    }

    my $col_dir = &util::filename_cat($col,$dir);
    my $full_col_dir = &util::filename_cat($full_col,$dir);

    if($delete eq "all") {
	$gsdl_cgi->local_rm_r($col_dir);
    }
    elsif($delete eq "files") {
	my $full_col_dir = &util::filename_cat($gsdlhome,"collect",$col_dir);
	# delete everything except metadata.xml files
	&util::filtered_rm_r($full_col_dir,undef,"metadata.xml\$");
    }
    elsif($delete eq "metadata") {
	my $full_col_dir = &util::filename_cat($gsdlhome,"collect",$col_dir);
	# delete only metadata xml files
	&util::filtered_rm_r($full_col_dir,"metadata.xml\$",undef);
    }

    if(!-e $full_col_dir) {
	&util::mk_dir($full_col_dir);
    }

    my $zip_file_name = $col . ".zip";
    my $zip_file_path = &util::filename_cat($full_col_dir, $zip_file_name);
    if (open(ZOUT, ">$zip_file_path")) {
	binmode(ZOUT);

	my $ZIN = $gsdl_cgi->upload('zip');
	binmode($ZIN);

	my $buf;
	my $num_bytes = 0;

	while (read($ZIN,$buf,1024)>0) {
	    print ZOUT $buf;
	    $num_bytes += length($buf);
	}

	close($ZIN);
	close(ZOUT);

	my $java = $gsdl_cgi->get_java_path();

	my $classpath = &util::filename_cat($gsdlhome,"bin","java",
					    "SignedGatherer.jar");

	my $java_cmd = "$java -classpath \"$classpath\" org.greenstone.gatherer.util.Unzip \"$zip_file_path\" \"$full_col_dir\"";
	$gsdl_cgi->generate_ok_message("Command: $java_cmd\n");

	my $java_output = `$java_cmd`;
	
	my $status = $?;
	unlink "$zip_file_path";

	if ($status > 0) {
	    $status = $status/256;
	    my $exit_status = "Exit status: $status\n";
	    my $mess = "Java failed: $java_cmd\n--\n$java_output\n$exit_status\n" . $gsdl_cgi->check_java_home();
	    $gsdl_cgi->generate_error($mess);
	}
	
	$gsdl_cgi->generate_ok_message("$dir or $col uploaded successfully.");
    }
    else {
	$gsdl_cgi->generate_error("unable to open $zip_file_path");
    }
}

&main();
