#!perl -w

###########################################################################
#
# webpage_buildstatus.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 checks on the progress of 
#  import.pl and buildcol.pl processes

use CGI;
use GSDLHOME;

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 $bc1finished = 0;

    my $tmpname = $args->{'bc1tmpname'};
    if (!defined($tmpname))
    {
	my $mess = "Temporary filename for building communication not defined.";
	$bc1finished = 1;
	&webpageutil::status_location($args,$mess,$tmpname,$bc1finished);
	return;
    }

    my $full_tmpname = &util::filename_cat($ENV{'GSDLHOME'},"tmp");

    if (!-d $full_tmpname) {
	mkdir ($full_tmpname, 0777);
    }

    $full_tmpname = &util::filename_cat($full_tmpname, $tmpname);

    if (open(TMPIN,"<$full_tmpname"))
    {
	my @tmp_text = ();
	my $line;
	while (defined($line=<TMPIN>))
	{
	    chomp $line;
	    push(@tmp_text,$line);
	}
	my $mess = join(" ",@tmp_text);
	$bc1finished = 1 if ($mess eq "Done");
	$bc1finished = -1 if ($mess =~ m/^Error:/);
	# escape backslashes so they don't vanish from printed filenames
	$mess =~ s/\\/\\\\/g;
	&webpageutil::status_location($args,$mess,$tmpname,$bc1finished);

	close(TMPIN);
    }
    else
    {
	my $mess = "Unable to open for reading temporary file: $full_tmpname.";
	$bc1finished = 1;
	&util::rm($full_tmpname);
	&webpageutil::status_location($args,$mess,$tmpname,$bc1finished);
	return;
    }
}

&main();
