#!/usr/bin/perl
#
#  script name:  info
#  used from  :  /data1/nec/htmlbin
#  converted from info (/bin/csh script) originally written by Glenn Flierl
#  modified to read from .info files by Christine Hammond
#
# NOTE: /images/dir.gif and /images/datadisp.gif are used by this script 
#  if you do not have these buttons, you can get them on anonymous ftp at
#  dataone.whoi.edu (128.128.80.28) in /pub/images
#
# Modified September 10, 1999, clh:  take the object information
#   only from the path_info, in query_string are any current subselections
#   Historically we were checking both for the objectname.
#
# Modified 14 February 2000, clh:  determine the $ddir (path from 
#   objectroot to location of .info file) as "start of path_info up to 
#   the character where we found the objectname starting". logic had
#   been ".. up to the objectname", which did not allow for a sub-dir
#   named same as objectname (in this case, $ddir was wrong, .info not found)
#
# Modified 11 December 2008, xye: Checking the database, if object is in the 
# database, the info is came from database. Otherwise, it is from info file.
# CMarZ $ddir just like /CMarZ. It just has one directory. and also 
# CMarZ program acronym name eq CoML.
################################################################
use LWP::Simple qw(!head);
$dirlink="";
$datalink="";
$imagedir="http://cmarz.whoi.edu/images";
#
# tell browser to display as html
#
   print "Content-type: text/html\n\n";
#
# get the arg, use tail only - should be objectname[+params]
#
# NOTE: info allows for either form: /jg/info?/path/obj
#                                    /jg/info/path/obj   <--- newer
# September 10, 1999: clh remove 'old' QS form of object 
#
 if ($ENV{'PATH_INFO'}) {
    $arg = $ENV{'PATH_INFO'};
 }
 if ($ENV{'QUERY_STRING'}) {
    $param = $ENV{'QUERY_STRING'};
 } 
#print STDERR $arg,"\n";
#print STDERR $param,"\n";
#
# Check for an extended URL 
#
 $ndx = index($arg,'{');
 if ($ndx >= $[) {
   $pathobj=substr($arg,0,$ndx);
   $extent=substr($arg,$ndx);
 } else {
   $pathobj=substr($arg,0);
   $extent="";
 }
#
# Check the object's type - htmlx, info, brevx - remove type
#
 $ndx = rindex($pathobj,'/');
 $obj = substr($pathobj,$ndx+1);
 if ($obj =~ /\.[hibj]+/) {
   $obj = substr($obj,0,(index($obj,".")));
 }
#print STDERR "object is $obj\n";
#
# arg's head is the path from top of object tree
#
 $ddir = substr($pathobj,0,$ndx);
my $project=substr($ddir,rindex($ddir,"/")+1);
#my $project="NEC";
#my $program=substr($ddir,1,rindex($ddir,"/")-1);
my $program="CoML";
#print STDERR "path to obj is $ddir\n";
#
# parse the extension to the URL, get Data URL if present
#
if ($extent) {
  if ($datapiece=index($extent,"data=")) {
    $datalink=substr($extent,$datapiece+5);
# 
# remove the final curly brackets from datalink 
#
    chop($datalink);
  }
#
# and get the Directory URL if present
#
  if (($ndx=index($extent,"dir=")) >= $[) {
    $dirlink=substr($extent,$ndx+4,$datapiece-6);
  }
}

#
# for returning to data object display, need new extension, to dir and info
#
$newextent="{dir="."$dirlink".",info=";
$newextent.="$ENV{'SERVER_NAME'}".":"."$ENV{'SERVER_PORT'}";
$newextent.="/jg/info"."$pathobj"."}";
#
# go to top of objects tree, plus current dir
# NOTE:  this assumes a standard jgofs structure with directories
#         htmlbin/ and objects/ at the first level subdirs of /data1/nec
#
   chdir("../objects$ddir");
#
# show the buttons for directory, data at top of screen
#
  if ($dirlink) {
     print "<a href=\"http://$dirlink\"><img alt=\"[Directory]\" \
        border=0 src=\"$imagedir/dir.gif\"></a> \n";
  }
  if ($datalink) {
     print " <a href=\"http://$datalink$newextent?$param\">
             <img alt=\"[Data...]\" \
        border=0 src=\"$imagedir/datadisp.gif\"></a>\n";
  }
#
# show the objectname for which we have a .info 
#
   print "<h1> $obj </h1>\n";
   print "<pre>\n";
#
# display contents of objectname.info file in current dir
#
my $flag=0;
my $content='';
#my $usedatabase=$ENV{'USEDATABASE'};
#and $usedatabase eq 'YES'
 if(defined $ddir and defined $obj ){

   $content=get("http://access1.bco-dmo.org/bco-test/getinfor.active?ddir=".$ddir."&obj=".$obj);
   
}
   

if (length($content)==0) 
{ $flag=0;}
else
{$flag=1;}

my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
my $date=sprintf "%4d-%02d-%02d&nbsp;&nbsp;%02d:%02d:%02d",$year+1900,$mon+1,$mday,$hour,$min,$sec;

if($flag==1)
{print "$content\n";
print "<p><font size=\"-2\">This document is created from the content of the BCO-DMO metadata database.&nbsp;&nbsp;&nbsp;&nbsp;$date</font></p>\n";
}
elsif (-e "$obj.info") {
	 if (open(INFILE, "$obj.info")) 
     {
   while (<INFILE>) {
     print $_;
       }
   print "</pre>\n";
   print "<hr>\n";
   print "<p><font size=\"-2\">This document is created from the content of the info file.</font></p>\n";
    }
   else
   {
		print STDOUT ("Could not open $obj.info file, error=$!\n");
	}
 
 }

else {
	print STDOUT ("Could not find either database or text based metadata file\n ",
		"for object $obj\n");
 }
 
#print "<p>$ddir---$program---$project---$obj</p>";
 close(INFILE);
#
