#!/usr/local/bin/perl -w

#makeinvlink routine

#Creates link from .remoteobject information, level, variable name
#value and level.

#July 16, 1997 rcg
#January 2, 1998 rcg test for existence of html and level, and /jg/serv
#	Don't add if present.

print "Content-type: text/html\n\n<head>\n\n";

$objectroot="/data/globec/objects";

$level=$ARGV[0];
$level =~ s!.*/(\d)$!$1!;

$fieldvalue=$ARGV[0];
$fieldvalue=~ s!.*/(.*)/\d$!$1!;

$fieldname=$ARGV[0];
$fieldname=~ s!/.*/(.*)/$fieldvalue/$level$!$1!;

$object=$ARGV[0];
$object=~ s!^(.*)/$fieldname/$fieldvalue/$level$!$1!;

$objectdir=$object;
$objectdir=~s!^(.*)/.*$!$1!;

$object=~s!^.*$objectdir/(.*)$!$1!;

#Get entry from .remoteobjects file
$filename=$objectroot . $objectdir . "/.remoteobjects";
$lookfor=$object . "=";
if (! -e $filename) {
	print "<br>Cannot find file $filename to resolve link";
}
$entry=`grep $lookfor $filename`;
@location=split /=/, $entry;
chomp @location;
$location=$location[1];
$location=~s/^http://;
#print "\nLocation=$location\n<p>";
if (index ($location, "/jg/serv/") < 0 ) {
	$location=~ s!(^//.*?/)(.*)!$1jg/serv/$2!;
}

#This is what Glenn uses
#$link="Location: http:" . $location . ".html" . $level . "?" .
#	$fieldname . "=" . $fieldvalue;

#Try using meta tag

if (index ($location, "html") < 0 ) {
	$htmlstring =  ".html" . $level;
}
else {
	$htmlstring = "";
}
$link="http:" . $location . $htmlstring . "?" .
	$fieldname . "=" . $fieldvalue;
$output='<meta http-equiv="refresh" content="0;URL=' . $link . '">';
print "$output\n";
print ('</head> <body> <p><br><br><h3>Retrieving the ',$fieldvalue,
	' data ....</h3> <p><br><br>',"\n",
	'If this is not working, please contact the',"\n",
	'<a href="http://globec.whoi.edu/globec-dir/contact_dmo.html">',
	'Data Management Office</a>. </body></html>');
print "\n";





