#!/usr/bin/perl -w

use CGI;
use usabcgi;

sub init_cgi
{
    my $line = <STDIN>;
    my $cgi;

    if ((defined $line) && ($line ne "")) {
	$cgi = new CGI($line);
    }
    else {
	$cgi = new CGI();
    }

    return $cgi;
}

sub clean_param
{
    my ($cgi,$param) = @_;

    my $val = $cgi->param($param);
    $val =~ s/[\r\n]+$//;

    return $val;
}


sub main
{
    my $cgi = init_cgi();

    my ($gsdlhome) = usabcgi::get_config_info("gsdlhome");

    my $col = clean_param($cgi,"c");
    my $dir = clean_param($cgi,"dir");
    $dir = "" if ($dir eq ".");

    chdir "$gsdlhome/collect";

    my $cmd = "zip -r $col.zip $col/$dir";


    #print "Content-type:text/plain\n\n";
    #print "$cmd";

    my $output = `$cmd`;
    
    ### print STDERR "**** output = $output\n";

    print "Content-type:application/zip\n\n";
##    my $output2 = `cat $col.zip`;

##    print $output2;

     if (open(PIN,"cat $col.zip |")) {
 	binmode(PIN);

 	my $buf;
 	my $num_bytes = 0;

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

 	close(PIN);

 	my $status = $?;
    }



    unlink "$col.zip";
}

&main();

