#!/usr/local/bin/perl5 -w

###########################################################################
#
# webpage_delcol.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 program is a webpage wrapper that deletes the named collection

use CGI;
use GSDLHOME;
use Fcntl ':flock';

require util;
require webpageutil;

sub parse_cgiargs
{
    # get arguments
    my $cgi = new CGI;
    my %args = ();
    
    foreach $p ($cgi->param()) 
    { 
	$args{$p} = $cgi->param($p);
    }

    return \%args;
}



sub main
{
    # get arguments
    my $args = parse_cgiargs();

    my $dirname = $args->{'bc1dirname'};
    if (!defined($dirname))
    {
	# directory name derived from the collection name missing
	my $mess = "Directory name derived from the collection name missing.";
	&webpageutil::error_location($args,$mess);
	return;
    }

    my $delete_area = $args->{'bc1deletearea'};

    if ($delete_area eq "all")
    {
	# delete all files
	my $col_dir = &util::filename_cat($ENV{'GSDLHOME'},"collect",$dirname);
	&util::rm_r($col_dir);

	# delete from collections.txt
	my $collist_filename 
	    = &util::filename_cat($ENV{'GSDLHOME'},"etc","collections.txt");
	if (open(CLIN,"<$collist_filename"))
	{
	    if (flock(CLIN,LOCK_EX))
	    {
		my @keep_dirnames = ();
		while (defined($line=<CLIN>))
		{
		    chop $line;
		    push(@keep_dirnames,$line) if ($line ne $dirname);
		}
		close(CLIN);

		if (open(CLIN,">$collist_filename"))
		{
		    print CLIN join("\n",@keep_dirnames), "\n";
		}

		flock(CLIN,LOCK_UN);
		close(CLIN);
	    }
	    else
	    {
		# problem locking file
		my $mess = "Unable to lock collection list configuration";
		$mess .= " file: $collist_filename";
		&webpageutil::error_location($args,$mess);
		return;
	    }
	}
	else
	{
	    # problem opening file for reading in

	    my $mess = "Unable to open for input the collection list";
	    $mess .= " configuration file: $collist_filename";
	    &webpageutil::error_location($args,$mess);
	    return;
	}
    }
    elsif ($delete_area eq "import")
    {

	my $collectdir =  &util::filename_cat($ENV{'GSDLHOME'},"collect",$dirname);
	my $import_dir = &util::filename_cat($collectdir,"import");
	my $archives_dir = &util::filename_cat($collectdir,"archives");
	&util::rm_r($import_dir,$archives_dir);
	&util::mk_dir($import_dir);
    }
    elsif ($delete_area eq "archives")
    {
	my $collectdir =  &util::filename_cat($ENV{'GSDLHOME'},"collect",$dirname);
	my $archives_dir = &util::filename_cat($collectdir,"archives");
	&util::rm_r($archives_dir);
    }
    else
    {
	# unrecoginised area to delete
	my $mess = "Unrecognised area to delete: $delete_area";
	&webpageutil::error_location($args,$mess);
	return;
    }

    my $mess_url = "$args->{'httpbuild'}&bca=mess";
    $mess_url .= "&bc1dirname=$dirname" if ($delete_area ne "all");
    print "Content: text/html\n\n $mess_url&head=_headdone_&mess=_messdonedelcol_\n\n";
    return;

}

&main();


