#!/usr/bin/perl -w

###########################################################################
#
# pharos-imageis-delete.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.
#
###########################################################################

BEGIN {
    die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
    die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
    die "PHAROSIS_SRC_HOME not set\n" unless defined $ENV{'PHAROSIS_SRC_HOME'};
    unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
}


use strict;
no strict 'refs'; # allow filehandles to be variables and vice versa
no strict 'subs'; # allow barewords (eg STDERR) as function arguments

use util;


sub main
{
    
    my $ARGC = scalar(@ARGV);
    
    
    if ($ARGC != 2) {
	my ($progname) = ($0 =~ m/^.*[\/|\\](.*?)$/);
	print STDERR "Usage: $progname collection doc-id\n";
	exit 1;
    }

    my ($col,$id) = @ARGV;
            
    my $docid = $id;
    if ( $col ne "" ) {
	$docid="$col:$docid";
    }
    
    my $tmp_dir = &util::get_toplevel_tmp_dir();
    $tmp_dir = &util::filename_cat($tmp_dir,"pharos-imageis");

    print "Generating del_id.xml\n";
    
    my $del_template_filename
	= &util::filename_cat($ENV{'PHAROSIS_SRC_HOME'},"resources", "templates","del_id.xml");
    my $del_this_filename = &util::filename_cat($tmp_dir,"del_id.xml");
    
    if (open(FIN,"<$del_template_filename")) {
	
	my $del_this_file_content = "";
	
	my $line;
	while (defined ($line=<FIN>)) {
	    $line =~ s@\*\*docid\*\*@$docid@g;
	    
	    $del_this_file_content .= $line;
	}
	
	close(FIN);
	
	if (open(FOUT,">$del_this_filename")) {
	    print FOUT $del_this_file_content;
	    close(FOUT);
	}
	else {
	    print STDERR "Error: failed to write out $del_this_filename\n";
	    print STDERR "$!\n";
	}
    }
    else {
	print STDERR "Error: failed to read $del_template_filename\n";
	print STDERR "$!\n";
    }
    
    
    my $jar_filename = &util::filename_cat($ENV{'PHAROSIS_SRC_HOME'},"ImageIS.jar");
    my $prop_dir = &util::filename_cat($ENV{'PHAROSIS_SRC_HOME'},"classes");

    &util::envvar_append("CLASSPATH",$prop_dir);
    &util::envvar_append("CLASSPATH",$jar_filename);

    my $java_cmd = "java imageis.ImageIS \"$del_this_filename\"";

    print "Deleting $docid\n";
    
    my $status = system($java_cmd);
    
    if ($status == 0 ) {
	&util::rm($del_this_filename);
    }
    else {
	print STDERR "Error: Failed to run:\n";
	print STDERR "$!\n";
	exit 1;
    }
}
 

main();

