#!/usr/local/bin/perl
# -----------------------------------------------------------------
# bdmanualb
# 
# Author: Mic Bowman
#
# Perform a manual update of the server description file.
# -----------------------------------------------------------------
$ENV{'HARVEST_HOME'} = "/usr/local/harvest"
	if (!defined($ENV{'HARVEST_HOME'}));
$ENV{"HSR_HOME"} = "$ENV{'HARVEST_HOME'}/gatherers/HSR";
push(@INC,$ENV{"HSR_HOME"} . "/lib");
require 'bdlibrary.pl';
require 'cgi.pl';
require 'hsrlibrary.pl';

# -----------------------------------------------------------------
# BD_Update
# -----------------------------------------------------------------
sub BD_Update {
    local(*obj,*tags) = @_;
    local($mandatory,$manual,$auto,$desc);
    local($man,$rec,$opt);
    local($val,$box,$line);

    $man = $rec = $opt = "";

    # Create mandatory and recommended fields
    foreach $key (sort keys %tags) {
	($btype,$mandatory,$manual,$auto,$desc) = split('&',$tags{$key});
	next if ($manual ne "Y"); # Skip automatic tags
	$box = &BD_InputBox($btype,$key,$obj{$key});
	$line = "<H4>$key</H4>\n<P>$desc<BR>$box</P>\n";
	
	$man = $man . $line if ($mandatory eq "Y");
	$rec = $rec . $line if ($mandatory eq "N");
    }

    foreach $key (sort keys %obj) {
	next if (defined $tags{$key});
	$box = &BD_InputBox("S",$key,$obj{$key});
	$line = "<H4>$key</H4>\n$box\n<P>\n";
	$opt = $opt . $line;
    }

    return($man,$rec,$opt);
}

# -----------------------------------------------------------------
# -----------------------------------------------------------------
sub BD_Create {
    local(*tags) = @_;
    local($mandatory,$manual,$auto,$desc);
    local($man,$rec,$opt);
    local($val,$box,$line);
    
    $man = $rec = $opt = "";

    foreach $key (sort keys %tags) {
	($btype,$mandatory,$manual,$auto,$desc) = split('&',$tags{$key});
	next if ($manual ne "Y"); # Skip automatic tags
	$box = &BD_InputBox($btype,$key,"");
	$line = "<H4>$key</H4>\n<P>$desc<BR>$box</P>\n";
	
	$man = $man . $line if ($mandatory eq "Y");
	$rec = $rec . $line if ($mandatory eq "N");
    }

    # Add the description file
    ($btype,$mandatory,$manual,$auto,$desc) = 
	split('&',$tags{"Description-File"});
    $box = &BD_InputBox($btype,"Description-File","");
    $line = "<H4>Description-File</H4>\n<P>$desc<BR>$box</P>\n";
    $man = $line . $man;

    return ($man,$rec,$opt);
}

# -----------------------------------------------------------------
# Main
# -----------------------------------------------------------------
sub Main
{
    local($stype,$sfile,$shome) = @ARGV;
    local($type,$url,%obj,%tags);
    local($updateurl, $addopturl);
    local($filename);

    &HSR_Initialize;
    %tags = &BD_ReadTags($stype);

    # Create the form entries
    if (-r $sfile) {
	($type,$url,%obj) = &HSR_ParseDescription($sfile);
	&CGIError("Requested type is different than stored type: $type")
	    if ($type ne $stype);
	($man,$rec,$opt) = &BD_Update(*obj,*tags);
    } else {
	($man,$rec,$opt) = &BD_Create(*tags);
    }

    # Process the skeleton
    $updateurl = &HSR_FileName("CGI","BDUpdate");
    $addopturl = &HSR_FileName("CGI","BDAddOpt");
    $filename = &HSR_FileName("Library","BDManual.Skel");

    open(SFILE,"<$filename") ||
	&CGIError("Unable to find skeleton file, check configuration");

    while (<SFILE>) {
	$_ =~ s/@FILE@/$sfile/g;
	$_ =~ s/@TYPE@/$stype/g;
	$_ =~ s/@HOME@/$shome/g;
	$_ =~ s/@UPDATEURL@/$updateurl/g;
	$_ =~ s/@ADDOPTURL@/$addopturl/g;
	$_ =~ s/@MANDATORY@/$man/g;
	$_ =~ s/@RECOMMENDED@/$rec/g;
	$_ =~ s/@OPTIONAL@/$opt/g;
	print $_;
    }

    close(SFILE);
}

# -----------------------------------------------------------------
&Main;
exit(0);
