#!/usr/bin/perl -w

###########################################################################
#
# pharos-imageis-add.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;
use File::Basename;

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

    # docid might be undefined, in which case later on is is derived from
    # from col and the file-name tail to srcfilename
    my ($col,$srcfilename,$docid) = @ARGV;
    
    
    if ( ! -f $srcfilename ) {
	print STDERR "Error: failed to find $srcfilename\n";
	exit 1;
    }
    
    if ( $srcfilename !~ m/\.jpe?g$/i ) {
	print STDERR "Error: Unrecognized JPEG filename extension\n";
	exit 1;
    }
    
    if (!defined $docid) {
	my ($fileroot,$dirname_unused,$suffix_unused) 
	    = fileparse($srcfilename, "\\.[^\\.]+\$");
	$docid = $fileroot;
    }
    
    # make sure file extension is lowercase .jpg
    
    my $dstfile = "$docid.jpg";
    
    if ( $col ne "" ) {
	$dstfile="$col:$dstfile";
    }
    
    my $tmp_dir = &util::get_toplevel_tmp_dir();
    $tmp_dir = &util::filename_cat($tmp_dir,"pharos-imageis");
    if (! -d $tmp_dir) {
	&util::mk_all_dir($tmp_dir);
    }
    my $dstfilename=&util::filename_cat($tmp_dir,$dstfile);
    
    &util::cp($srcfilename,$dstfilename);
    
    print "Generating add_file.xml\n";
    
    my $add_template_filename
	= &util::filename_cat($ENV{'PHAROSIS_SRC_HOME'},"resources", "templates","add_file.xml");
    my $add_this_filename = &util::filename_cat($tmp_dir,"add_file.xml");
    
    if (open(FIN,"<$add_template_filename")) {
	
	my $add_this_file_content = "";
	
	my $line;
	while (defined ($line=<FIN>)) {
	    $line =~ s@\*\*imgfile\*\*@$dstfilename@g;
	    
	    $add_this_file_content .= $line;
	}
	
	close(FIN);
	
	if (open(FOUT,">$add_this_filename")) {
	    print FOUT $add_this_file_content;
	    close(FOUT);
	}
	else {
	    print STDERR "Error: failed to write out $add_this_filename\n";
	    print STDERR "$!\n";
	}
    }
    else {
	print STDERR "Error: failed to read $add_template_filename\n";
	print STDERR "$!\n";
    }
    
    
    my $jar_filename = &util::filename_cat($ENV{'PHAROSIS_SRC_HOME'},"pharos-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 \"$add_this_filename\"";


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

main();
