#!/usr/bin/perl -w

use gsdlCGI;

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


    my $gsdlhome = $gsdl_cgi->get_config_info("gsdlhome");

    my $col = $gsdl_cgi->clean_param("c");
    if (!defined $col) {
      $gsdl_cgi->generate_error("No collection specified.");
    }

    my $dir = $gsdl_cgi->clean_param("dir");
    $dir = "" if ((!defined $dir) || ($dir eq "."));


    $gsdl_cgi->checked_chdir("$gsdlhome/collect");

    # Change this to Java zip??
    my $zip_cmd = "zip -r $col.zip $col/$dir";
    $gsdl_cgi->unix_cmd($zip_cmd);


    my $pipe_cmd = "cat $col.zip"; # Unix specific

    if (open(PIN,"$pipe_cmd |")) {
 	binmode(PIN);

 	my $buf;
 	my $num_bytes = 0;

	print "Content-type:application/zip\n\n";

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

 	close(PIN);

 	my $status = $?;
    }
    else {
        $gsdl_cgi->generate_error("Unable to pipe input from: $pipe_cmd");
    }
    
    unlink "$col.zip";
}

&main();

