#!/usr/local/bin/perl
#
#  script name:   dir-join
#  used from  :   /data/globec/htmlbin
#  taken from dir to allow user to select an object for join
#   Christine Hammond 
#   September 8, 1997
#  
############ Customized values for this site ###################
$jghome="/data/globec";
$topdir="/globec/gb/";
$refhost="optserv1.whoi.edu:8081";
#
# DMO name, if appropriate
#
$office="US JGOFS";
#
# Location where the image(s) are located, if any
#
$imagedir="http://globec.whoi.edu/images";
#
############ End of Custom values for this site ################
#
$dictionary=".remoteobjects";
$logentry=".log";
$dirspacer = "  ";
$jgroot=$jghome."/objects";
#
#
# 
print "Content-type: text/html\n\n";
#
# get name of this server from env of httpd
#
if ($ENV{'SERVER_NAME'}) {
   $fromhost = $ENV{'SERVER_NAME'};
   if ($ENV{'SERVER_PORT'}) {
      $p=$ENV{'SERVER_PORT'};
      if ($p != 80) {
         $fromhost .= ":".$p;
      }
   }
} else {
   $fromhost = "globec.whoi.edu";
}

if ($fromhost =~ /\/\//) {
  $fromhost=substr($fromhost,2);
}
#
# Locate directory to display
#      NOTE: dir allows for either form: /jg/dir-join?/path/
#                                        /jg/dir-join/path/   <--- newer
#
if ($ENV{'PATH_INFO'}) {
   $arg = $ENV{'PATH_INFO'};   
} elsif ($ENV{'QUERY_STRING'}) {
   $arg = $ENV{'QUERY_STRING'};
} else {
   $arg = $topdir;
}

$currentdir = $arg;

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

print "<title>$currentdir data directory page</title>\n";

chdir("$jgroot$currentdir");
#
# Always check for a file of HTML code to customizes whole system
#
print "<H1> $office Data System</H1>\n";

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 "<strong><em>Data              Investigator         Description and Documentation</em></strong>\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 "<H4>Go to the indicated category of data by clicking on its name</H4>\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) {
      if ($imagedir) {
          printf "     <a href=\"http:/jg/dir-join%s/\">%s</a>    <img \
          src=\"%s/youhere.gif\">\n",$urldir,$diritem,$imagedir;
      } else {
          printf "     <a href=\"http:/jg/dir-join%s/\">%s</a>    <-----\n",
                   $urldir,$diritem;
      }
    } else {
       printf "     <a href=\"http:/jg/dir-join%s/\">%s</a>\n",$urldir,$diritem;
    }
#
  }
}
close(FIND);
print "<\pre>\n";

# -------------------------Subroutines----------------------------
#  readdct - gets a dictionary entry from 'get_entry' routine,
#            prints it, using 'print_entry' routine
#
sub readdct{
   local($whichone) = pop(@_);
   local($ndx) = 0;

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

   while (&get_entry()) {
      &analyze_entry();
      &print_entry();
   }
   close(INFILE);
}

# -------------------------------------------
# get_entry - read the dictionary entry,
#             return false if no more entries
#
sub get_entry{
   local($i);
   local($true) = "1";
   local($false) = "0";

   @dictentry = "";

   if (eof(INFILE)) {
      $i = $false;
   } else {
      foreach $i (1,2,3) {
        $dictentry[$i] = <INFILE>;
        chop($dictentry[$i]);
      }
      $i = $true;
   }
}

# -------------------------------------------------------
# analyze_entry - populate the fields that are displayed, 
#                 based on these rules governing entries:
##############################################################################
#  Rules
#   0. entries consist of 3 lines, as:
#
#      line 1    displayed_object_name=information
#      line 2    - PI_name
#      line 3    - [optional_info] description
#                ^Note: the carot indicates column 1 start
#
#   1. 3rd line contains a brief description of the data, which will
#      become the link to the documentation file.  Optionally, a URL
#      can be included, BEFORE the brief description and separated from it
#      by a space, to indicate a change from the system default documentation
#      scheme (see below).  Syntax for this optional_info is a URL (can omit 
#      hostname, if local host) - only exception applies if documentation file 
#      name is x.info, where x is displayed_object_name:  can end URL in '/'
#      after the path and omit the displayed_object_name.
#           For example, this .remoteobjects entry:
#      phys_params=//another_machine/pacific/cruise77/phys_params
#      - J.Investigator
#      - http://another_machine/jg/info/pacific/cruise77/
#           directs dir to another_machine's /jg/info script which will
#      read a file named 'phys_params.info' in the systems's objects/ tree
#      subdirectory names pacific/cruise77. 
#
#   2. 2nd line contains the Investigator's name responsible for data
#
#   3. 1st line identifies an object name and some information required to 
#      reach it, as:
#
#      displayed_object_name=information_for_hypertext_link
#
#      2 possible forms for syntax of right hand side are
#
#     i.     http:[host]/jg/serv/path/to/object.html0[Xsub-selectionsY]
#    ii.     //host/path/to/objectname[Xsub-selectionsY]
#
#        where [ and ] indicate an optional portion of the syntax,
#          X is a "(", then a comma-separated list of sub-selection parameters 
#          Y is a ")", used to complete the list
##############################################################################
#
sub analyze_entry{
#
#  check that anything is written on 3rd line
#

 $descript = "";
 $addobjtoinfo = "n";
 if (length($dictentry[3]) > 2) {
#
#  when supplied, the optional info-serving information is a complete URL
#
    if ($dictentry[3] =~ /http:\//) {
#
#  a space separates the info serving host and path from the description
#
       $ndx = index($dictentry[3]," ",2);
       $descript = substr($dictentry[3],$ndx+1);

    } else {

       $descript = substr($dictentry[3],2);
    }
 } else {
#
# no information provided on line
#
     $descript = "";
 }
#
# the PI name is on the 2nd line of a dictionary entry
#
 $PI="";
 if (length($dictentry[2]) > 2) {
    $PI=substr($dictentry[2],2);
 } 

#
# 1st line has object=serving-host entry
#

 $URL4obj="";

 $dispobj=substr($dictentry[1],0,index($dictentry[1],"="));
 $rightside=substr($dictentry[1],(index($dictentry[1],"=")+1));

#
# the serving host is provided, strip off anything before it
#
  $ndx = index($rightside,"//");
  if ($ndx != -1) { 
    $rightside = substr($rightside,$ndx+2);
    $haveserver = "y";
  } else {  
    $haveserver = "";
  }

#
# enhancement to catch entries with subselections and htmlify them
#
 $subselections="";
 if ($rightside =~ /\/.+\(.+\)/ ) {
   $ndx = index($rightside,"(");
   $subselections=substr($rightside,$ndx+1,length($rightside)-$ndx-2);
   $rightside=substr($rightside,0,$ndx);
 }

#
# syntax of rightside can include full url: http:[hostname]/jg/serv/test.html0
#
 if ($rightside =~ /jg\/serv/) {
    if ($haveserver) {
      $URL4obj="$rightside";
    } else {
#
# might be form, http:/jg/serv/path/obj
#
      $ndx = index($rightside,"http:");
      if ($ndx != -1) {
         $rightside = substr($rightside,$ndx+5);
         $URL4obj="$fromhost"."$rightside";
      }
    }
#    $datastring="$URL4obj";
#    $URL4obj.="{dir="."$dirstring";
 } else {
#
# it is a pseudo-URL, with no serving process listed - insert /jg/serv
#

    $servhost=substr($rightside,0,index($rightside,"/",2));
    $fullobj=substr($rightside,index($rightside,"/",2));

    if ($servhost =~ /\/\//) {
       $servhost = substr($servhost,2);
    }
    $URL4obj="$servhost"."$fullobj".".html0";
 }

 if ($subselections) {
    $URL4obj.="?"."$subselections";
 }

}

# --------------------------------
# print_entry -  format the output
#
sub print_entry{
#
# print the object name as a hypertext link
#
   $URL4obj =~ s/\/jg\/serv//;
   printf("<a href=\"http://%s/jg/get2andjoin//%s\" TARGET=\"_top\">%-18s</a>",
             $refhost, $URL4obj, $dispobj);
#
# print the PI name
#
   if ($PI) {
      printf("<b>%-20s</b>", $PI);
   } else {
      printf("%-20s", "  ");
   }
#
# finally, print description of the object
#
   print "$descript\n";

}
