#!/usr/local/bin/perl
#
# get2andjoin
#  purpose is to assign the second join object after a user selects
#  it from a 'dir'-like window, and then to issue a form allowing
#  the user to Submit the join for action
#
#
$remote=$ENV{'REMOTE_HOST'};
$pathinfo=$ENV{'PATH_INFO'};
$server=$ENV{'SERVER_NAME'};
$port=$ENV{'SERVER_PORT'};
if (!$port) {
  $port=80;
}
$mainlocation="/jgofsopt/$port/$remote/";

MAIN:
{
  &printtop;

    $object2=substr($pathinfo,0,rindex($pathinfo,"."));
    if ($ENV{'QUERY_STRING'}) {
       $param2=$ENV{'QUERY_STRING'};
       $totalobject="$object2"."("."$param2".")";
    } else {
       $totalobject=$object2;
    }
# $totalobject="//"."$server"."$totalobject";
 $totalobject="$totalobject";

 &getfilevars;
 &print_and_doit;
}
################### printtop
sub printtop{
 print "Content-type: text/html\n\n";
 print "<HTML><HEAD><TITLE>Join 2 Data Objects</TITLE></HEAD>\n";
 print "<BODY BGCOLOR=\"ffffff\">\n";
}
################### getfilevars
sub getfilevars {
$objnamefile="$mainlocation"."join-object-1";

open(OBJ,"$objnamefile") || die "Cannot open object $!<p>\n";
  while (<OBJ>) {
    $object1 = $_;
  }
chomp($object1);
close(OBJ);

$paramfile="$mainlocation"."parameters";

open(IN,"$paramfile") || die "Cannot open parameters $!<p>\n";
   while (<IN>) {
     $param1 = $_;
   }
  chomp($param1);
  close(IN);

  if ($param1 ne "none") {
    $object1.="("."$param1".")";
  }

$from_location="$mainlocation"."from_loc";
open(IN,"$from_location") || die "Cannot open fromlocation $!<p>\n";
  while (<IN>) {
    $refloc = $_;
  }
close(IN);
chomp($refloc);

}

################### print_and_doit
sub print_and_doit{

  print "<H1>Ready to Join 2 Data Objects</H1>\n";

  print "<B>Object 1: $object1<BR>\n";
  print "Object 2:  $totalobject</B><BR>\n";
  print "<HR NOSHADE>\n";
  print "<P>\n";

  print "<FORM METHOD=\"POST\" 
          ACTION=\"http://optserv1.whoi.edu/jg/joinuopt$object1\">\n";
  print "<INPUT TYPE=\"hidden\" 
          NAME=\"secondobj\" VALUE=$totalobject>\n";
  print "Press <FONT SIZE=\"4\" COLOR=\"0000ff\">
               <INPUT ALIGN=left TYPE=\"submit\" NAME=\"choice\"
                VALUE=\"Join\"></FONT> to do it.\n";
  print "</FORM>\n";

  print "<FORM METHOD=\"POST\" ACTION=\"$refloc\">\n";
  print "Or <FONT SIZE=\"4\"  COLOR=\"0000ff\">
            <INPUT TYPE=\"submit\" ALIGN=right
             VALUE=\"Cancel\"></FONT>\n";

  print "</FORM>\n";
  print "</BODY>\n";
  print "</HTML>\n";
 
}
