#!/usr/local/bin/perl -w
#
#  converted from dir (/bin/csh script) originally written by Glenn Flierl
#   Christine Hammond 
#   April 12, 1996
#  
#  added documentation for buttons
#   CLH
#   May 14,1996
#
# Note:  this script uses images/youhere.gif,
#                         images/helpb.gif,
#                         images/homeb.gif
#           which can be downloaded from anonymous ftp
#           on dataone.whoi.edu (128.128.80.28) in pub/images
#
# NOTE:  Please change the values for the entries below 
#        to those that will work on your machine
#        Additionally, links to buttons at the top of the directory display 
#        can easily be added.  See note "To add buttons" below.
#
############ Customized values for this site ###################
$jgroot="jgofsdir";
$topdir="objectdir/";
$infoserver="informationserver";
$homeurl="myaddr";
#
# in DocumentRoot (srm.conf) name of file with help info
#
$helpfile="dir-help.html";
#
# DMO name
#
$office="US JGOFS";
#
############ End of Custom values for this site ################
#
$dictionary=".remoteobjects";
$logentry=".log";
$dirspacer = "  ";
$jgroot=$jgroot."/objects";

if ($ENV{'QUERY_STRING'}) { 
  $currentdir = $ENV{'QUERY_STRING'};   
} else {
  $currentdir = $topdir;
}

$curdir = $currentdir;
chop($curdir);

print "Content-type: text/html\n\n";
print "<title>$currentdir data directory page</title>\n";

chdir("$jgroot$currentdir");
#
# At the toplevel directory, check for files describing 
#  the system (dirhead) and explaining the organization (direxplain)
#
if ($currentdir eq $topdir) {
  if (-r "dirhead") {
    open(INFILE,"dirhead") || die "Can't open input file: $!\n";
    while (<INFILE>) {
     print;
    }
    close(INFILE);
  }
  if (-r "direxplain") {
   open(INFILE,"direxplain") || die "Can't open input file: $!\n";
    while (<INFILE>) {
     print;
    }
    close(INFILE);
  }
}

#
# Display the Links at top of Directory page
#   To add buttons:  create a button GIF file by creating a simple html doc with
#                    the commands <form action=get><font size="5" color=0000ff>
#                           <input type="submit" value=" xxxx "></font></form>
#                    grab the image that is displayed (use snapshot (on a Sun), 
#                    or xwd) then, xv does a nice job with cropping and saving 
#                    as a GIF.   Refer to the 2nd print statement below, and   
#		     duplicate the 3rd and 4th lines of the continued print
#                    (starts with <a href=\).   Edit the second set of these
#                    lines to:
#                    change the URL to point to the button action target, 
#                    change the button name (*.gif file) to the one you have
#                    created.  Don't forget to copy the .gif to your 
#                    DOCUMENT_ROOT/images file.
#
print "<H1> $office Data System</H1>\n";
#
# Following is the print statement that prints the buttons
#
print "<H2><a href=\"http:$homeurl/$helpfile\">\
           <img border=0 src=\"/images/helpb.gif\"></a> \
           <a href=\"http:$homeurl/\"> \
           <img border=0 src=\"/images/homeb.gif\"></a> \
       </H2>";

print "<pre>\n";

#
# Check that there is some data to display (.remoteobjects or .log)
#

if (-r $dictionary || -r $logentry) {

   print "\n<H2>Directory of Data in $currentdir</H2>\n";
   if (-r $logentry) {
      &readdct($logentry);
       print "\n";
   }
 
   if (-r $dictionary) {
     print "<em>Data           Investigator            Documentation</em>\n";
     print "<hr>\n";
     &readdct($dictionary);
   }
}

print "<hr>\n";
#
# Always print the directory categories of data in the jgroot 
#
print "<H2>$office Data Categories</H2>";
print "Go to the indicated category of data by clicking on its name\n";
print "<p>\n";
#
# First, find all subdirectories in the given tree
#
open(FIND,"find $jgroot$topdir -type d -print| sort |");

$|=1;
while ($dir = <FIND>) {
  chop $dir;
#
# find all subdirectories containing 'objects' and the top of the tree
#
  if ($dir =~ /\/objects\// && $dir =~ /$topdir/) {
#
# the url pathitem for the link starts at $topdir
#
    $urldir = substr($dir,(rindex($dir,$topdir)));
#
# strip final '/' from $topdir (only one that has it), printf will insert it.
#
    if ($urldir eq $topdir) {
       $urldir = substr($topdir,0,(rindex($topdir,"/")));
    }
    @subdirs = split(/\//,$dir);
    foreach $diritem (@subdirs) {
     print $dirspacer;
    }
    @revlist = reverse(@subdirs);
#
# get tail of directory list - last item in array of directories
#
    $diritem = shift(@revlist);
#
# mark the current directory being displayed by an arrow
#

    if ($curdir eq $urldir) {
       printf "     <a href=\"http:/jg/dir?%s/\">%s</a>    <img \
          src=\"/images/youhere.gif\">\n",$urldir,$diritem;
    } else {
       printf "     <a href=\"http:/jg/dir?%s/\">%s</a>\n",$urldir,$diritem;
    }
#
  }
}
close(FIND);
print "<\pre>\n";

# -------------------------Subroutines----------------------------
sub readdct{
   local($whichone) = pop(@_);
   local($dictentry);
   local($i) = 0;
   local($ndx) = 0;

   open(INFILE,"$whichone") || die "Can't open input file: $!\n";

   while ($dictentry = <INFILE>) {
     $i++;
     chop($dictentry);
#
# 1st line has object=serving-host entry
#
     if ($i == 1) {
         $dispobj=substr($dictentry,0,index($dictentry,"="));
         $rightside=substr($dictentry,(index($dictentry,"=")+1));
#
# syntax of serving host entry can include full url: http:/jg/serv/test.html0
#
         if ((index($rightside,":/")) > 1) {
            printf("<a href=\"%s\">%-15s</a>", $rightside, $dispobj);
#
# find string /jg/serv and move to string just beyond it
#
            $ndx = index($rightside,"/jg/serv/",0)+8;
#
# now have to remove the .html0 extension from objectname
#
            $fullobj=substr($rightside,$ndx,(rindex($rightside,".htm")-$ndx));

         } else {
            $servhost=substr($rightside,0,index($rightside,"/",2));
            $fullobj=substr($rightside,index($rightside,"/",2));
            printf("<a href=\"%s/jg/serv%s.html0\">%-15s</a>",
               $servhost, $fullobj, $dispobj);
         }
     } elsif ($i == 3) {
#
# 3rd line, infoserving-host and the description of the data 
#
       $servhost=$infoserver;
       if ($dictentry =~ /\/jg\/info/) {
          $servhost = substr($dictentry,2,index($dictentry," ",3));
          $descript = substr($dictentry,index($dictentry," ",3));
       } else {
          $descript = substr($dictentry,2);
       }
       printf(" <a href=\"http:%s/jg/info?%s\">%s</a>\n",
                $servhost, $fullobj, $descript );

       $i = 0;
     } else {
#
# the PI name is on the 2nd line of a dictionary entry
#
       if (length($dictentry) < 3) {
           printf("%-20s", "  ");
       } else {
           printf("<b>%-20s</b>", substr($dictentry,2));
       }
     }
   }
   close(INFILE);
}
