package g2futil;


BEGIN 
{
    if (!defined $ENV{'FEDORA_HOME'}) {
	print STDERR "Environment variable FEDORA_HOME not set.\n";
	exit 1;
    }

    my $fedora_home = $ENV{'FEDORA_HOME'};
    my $fedora_client_bin = &util::filename_cat($fedora_home,"client","bin");
    $ENV{'PATH'} .= ":$fedora_client_bin";
}

use util;

sub run_cmd_old
{
    my ($cmd,$verbosity,$tolerate_error) = @_;

    if (($verbosity == 0) 
	|| (defined $tolerate_error && ($tolerate_error eq "tolerate_error"))) {
	$cmd .= " > /dev/null"; # Too Unix specific?
    }

    if ($verbosity >= 2) {
	print "Runing command:\n";
	print "$cmd\n";
    }

    my $status = system($cmd);

    if ($verbosity >= 2) {
	print "Exit status = ", $status/256, "\n";
    }

    if ((!defined $tolerate_error) || ($tolerate_error ne "tolerate_error")) {
	if ($status>0) {
	    print STDERR "Error executing:\n$cmd\n";
	    print STDERR "$!\n";
	}
    }

    return $status;
}


sub run_cmd
{
    my ($prog,$arguments,$verbosity,$tolerate_error) = @_;

    my $script_ext = ($ENV{'GSDLOS'} =~ m/^windows/) ? ".bat" : ".sh";

    if ($prog =~ m/^fedora-/) {
	$prog .= $script_ext;
    }

    my $cmd = "$prog $arguments";

###    print "*** cmd = $cmd\n";

    if (open(CMD,"$cmd 2>&1 |"))
    {
	my $result = "";
	my $line;
	while (defined ($line = <CMD>))
	{	
	    $result .= $line;	    

	    if ((!defined $tolerate_error) || ($tolerate_error ne "tolerate_error"))
	    {
		print $line;
	    }


	}
	
	close(CMD);
	
	$cmd_status = $?;

	if ($cmd_status == 0) {
	    # Check for any lines in result begining 'Error:'
	    
	    if ($result =~ m/^Error\s*:/m) {
		# Fedora script generated an error, but did not exit
		# with an error status => artificially raise one

		$cmd_status = -1;
	    }
	}

	if ($cmd_status != 0) {

	    if ((!defined $tolerate_error) || ($tolerate_error ne "tolerate_error"))
	    {
		print STDERR "Error: processing command failed.  Exit status $cmd_status\n";
		
		if ($verbosity >= 2) {
		    print STDERR "  Command was: $cmd\n";
		}
		if ($verbosity >= 3) {
		    print STDERR "result: $result\n";
		}

	    }
	}
    }
    else 
    {
	print STDERR "Error: failed to execute $cmd\n";
    }


    return $cmd_status;
}


sub run_datastore_info
{
    my ($pid,$options) = @_;

    my $verbosity = $options->{'verbosity'};

    my $hostname = $options->{'hostname'};
    my $port     = $options->{'port'};
    my $username = $options->{'username'};
    my $password = $options->{'password'};
    my $protocol = $options->{'protocol'};

    my $prog = "fedora-dsinfo";
    my $arguments = "$hostname $port $username $password $pid $protocol";
    my $status = run_cmd($prog,$arguments,$verbosity,"tolerate_error");

    return $status;
}

sub run_purge
{
    my ($pid,$options) = @_;

    my $verbosity = $options->{'verbosity'};

    my $hostname = $options->{'hostname'};
    my $port     = $options->{'port'};
    my $username = $options->{'username'};
    my $password = $options->{'password'};
    my $protocol = $options->{'protocol'};

    my $server = "$hostname:$port";

    my $prog = "fedora-purge";
    my $arguments = "$server $username $password $pid $protocol";
    $arguments .= " \\\n \"Automated_purge_by_g2f_script\"";

    my $status = run_cmd($prog,$arguments,$verbosity);

    return $status;
}

sub run_ingest
{
    my ($docmets_filename,$options) = @_;

    my $verbosity = $options->{'verbosity'};

    my $hostname = $options->{'hostname'};
    my $port     = $options->{'port'};
    my $username = $options->{'username'};
    my $password = $options->{'password'};
    my $protocol = $options->{'protocol'};

    my $server = "$hostname:$port";

    my $prog = "fedora-ingest";

    my $type = undef;
    
    if ($ENV{'FEDORA2_HOME'}) {
	$type = "metslikefedora1";
    }
    else {
	$type = "info:fedora/fedora-system:METSFedoraExt-1.1";
    }

    my $arguments = "file \"$docmets_filename\" $type $server $username $password $protocol";
    $arguments .= " \\\n \"Automated_purge_by_g2f_script\"";

    my $status = run_cmd($prog,$arguments,$verbosity);

    return $status;
}



sub get_hash_id
{
    my ($hash_dir) = @_;

    my $hash_id = undef;

    my $docmets_filename = &util::filename_cat($hash_dir,"docmets.xml");

    if (open(DIN,"<$docmets_filename"))
    {
	while (defined (my $line = <DIN>))
	{
	    if ($line =~ m/<dc:identifier>(.*?)<\/dc:identifier>/)
	    {
		$hash_id = $1;
		last;
	    }
	}
    
	close(DIN);
    }
    else
    {
	print STDERR "Warning: Unable to open \"$docmets_filename\"\n";
    }

    return $hash_id;

}




1;
