#!perllocation
#
# Name:         $OPTHOME/optbin/download-1
# Purpose:      part of JGOFS data download package
#		Verify data object name; determine file packaging type.
#
# Modification history:
# Nov 98.clc. under development.
# 981204.clc. add return back here from subsequent page for retry.
# 981018.clc. add build-download-env.plx
# 981230.clc. use full obj name; upgrade how we get object
# 990210.clc. file packaging/compression options (zip, tar.Z tar.gz)
# 990308.clc. offer tar file only (no compression); 
#		more options if no data object specified
# 990312.clc. asst appearance mods; color pallette
# 990317.clc. handle object name the 'JGOFS' way (PATH_INFO) with
#		subsels optionally specified in Q_STRING
# 990319-22.clc. more work on data object and subsels
# 990526.clc. test Help button.
# 990614.clc. remove most DBG lines.
# 990617.clc. minor change to DWNLD lines.  no code changes.
# ###### download version 1.0
# 990621.clc. port to OO server for installation/testing.
# 990715.clc. OPTIONSERVER, direcpage to optsmenu changes.
# 991122.clc. mods for merge with rest of oopt software
# 991124-30.clc. use OBJECT; dobject; $input{'dtempdir'}
# 991201.clc. merge/debug with ooptserver on synthesis.
# 991203.clc. remote object still fails. goto jg/dir if no object
# 991206.clc. use OBJEXT to fix remote object bug
# 991207.clc. minor verbage change. 
# 991209.clc. allow object with subsels to be pkgd into multiple files
# ###### download version 1.1  (991209)
#
# =====================================================================

require "cgi-lib.pl";
#
# setup local operating environment
#
do 'build-opt-env.pl';

MAIN:
{
#
# get object, subselection, etc. from envars set in build-opt-env.pl
#
  $dobject = $ENV{'OBJECT'};		# jgofs object string
  $dobjext = $ENV{'OBJEXT'};		# jgofs object string with extension
  $dispobj = $ENV{'DISPOBJ'};		# jgofs object+(QS) for display
  $subsels = $ENV{'SUBSELS'};		# subselections list to pass along 
  $subsdisp = $ENV{'DISPSS'};		# un-trigram'd subsels list for display
   
  $dtempdir = $ENV{'USETEMPDIR'}; 	# top of temp space tree 
  
  $previous_page = $ENV{'HTTP_REFERER'}; 	# referring URL
#
# color palette
#
  $credhi = "indianred";
  $chdrbar1 = "skyblue";
  $cgobar = "mediumaquamarine";
#
# These variables set by makefile from ENVars at install time 
#
  $jgscdir   = "jgscriptloc";		# where are CGI scripts?
  $webimages = "webimgsdir";   	# where are web images?
  $helpaddr  = "jghelpaddr";		# URL for web help docs
  $optsmenu  = "jgooptserver";		# JGOFS other options menu
  $jgdatadir = "jgdirserver";		# JGOFS data directory server
#
# -------------------------------------------------------------------
#
# Read in all the variables set by the form
#
  &ReadParse(*input);

  #Print the header
  print &PrintHeader;
  print <<ENDOFTEXT;
   <h1>Data download utility - form 1</h1>
   <b>Current object is:  $dispobj</b></br>
   <hr noshade><p>
ENDOFTEXT

  print STDERR "\nData DWNLD: === using script $0 === \n";
  print STDERR "DWNLD: dobject: $dobject \n";
  print STDERR "DWNLD: dobjext: $dobjext \n";
  print STDERR "DWNLD: dispobj: $dispobj \n";
  print STDERR "DWNLD: subsels: $subsels \n";
  print STDERR "DWNLD: subsdisp: $subsdisp \n";
   
  if ($dobject eq "") {
  
    # ... we have a problem; unable to determine object name ...
    print <<ENDOFTEXT;
     <HR NOSHADE WIDTH="100%"><br>
     <B><FONT color=0000ff>You must select a data object</FONT></p>
     Please go <A HREF=\"$previous_page\">back</A>, and try again<p>
     ... or perhaps you would like to go to the 
     <A HREF="$jgdatadir">Data Directory</A> menu </p>
     ... or try using your browser's 'Go' menu to return and 
     relocate the data object.</p></B>
     <HR NOSHADE SIZE=4 WIDTH="100%"><br>
ENDOFTEXT
  
  } else {
#  
# ... we have an object name
#
  $now = `date`;
  print STDERR "Data DWNLD: working on object: $dobject \n";
  print STDERR "Data DWNLD: request started $now \n";
#
# grab just the last part of the data object after the last slash 
#
  $objnameonly=substr($dobject,rindex($dobject,"/")+1);
  print <<ENDOFTEXT;
 <BODY BGCOLOR="#FBFBFF">
 <P>
 <B>The data format that is used by the JGOFS system to display
 data may not suit your needs. Please use this series of forms to 
 select the output format that best suits you.  
 Answer each question about the data you are now viewing.
 </B></CENTER>
 <p>
 
 <form method="post" action="$jgscdir/download-2">
 <input type="hidden" name="dobject" value="$dobject">
 <input type="hidden" name="dobjext" value="$dobjext">
 <input type="hidden" name="dispobj" value="$dispobj">
 <input type="hidden" name="dtempdir" value="$dtempdir">
ENDOFTEXT
# 
# how should we divide/package the data?
#
 if ($subsels ne "") {
#
# User has selected a list of projections, subselections.
#
   print <<ENDOFTEXT;
  <FONT SIZE=+1><B>Subselections: </B></FONT>$subsdisp <BR>

  <input type="hidden" name="subsels" value="$subsels">
  <input type="hidden" name="subsdisp" value="$subsdisp">

  <TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=6>
  <TR>
    <TD COLSPAN=3 BGCOLOR="$chdrbar1">
      <H3>I see you've already done some subselection of this data object.  
      Do you want to use these subselections or the whole data object?</TD>
    <TD BGCOLOR="$chdrbar1">
      <A HREF="$helpaddr/download-help.html"><img alt="[HELP]" 
      align=absmiddle border=0 src="$webimages/helpbutton.gif"></A></TD>
  </TR></H3></TABLE>
  
  <DL><DD>I want to <A HREF="$jgscdir/download-1$dobjext">
   <B>go back</B></B> and get the whole data object</A>.</DD><BR>
   </DL>
   
   <H3>If you want to use the subselections shown above, simply continue 
   with this form.</H3>
  
ENDOFTEXT

  }
#
# User selects package type file size (single/multiple)
# 
 print <<ENDOFTEXT;  
 <TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=6>
  <TR><TD COLSPAN=3 BGCOLOR="$chdrbar1">
    <H3>How would you like your data packaged?</TD>
    <TD BGCOLOR="$chdrbar1">
      <A HREF="$helpaddr/download-help.html#pkgtype"><img alt="[HELP]" 
      align=absmiddle border=0 src="$webimages/helpbutton.gif"></A></TD>
  </TR></H3></TABLE>
 <dl><dd><select name="bulk">
 <option value="moresmall"selected>Multiple smaller files (CTD, hydrographic data, etc.)
 <option value="onebig">One single file (underway, seasoar, continuous data, etc.)
 </select></dd></dl>
 <p>
ENDOFTEXT
#
# User selects compression option
#   
 print <<ENDOFTEXT;   
 <TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=6>
 <TR>
   <TD COLSPAN=3 BGCOLOR="$chdrbar1">
      <H3>Please choose a compression option:</TD>
   <TD BGCOLOR="$chdrbar1">
      <A HREF="$helpaddr/download-help.html#compress"><img alt="[HELP]" 
      align=absmiddle border=0 src="$webimages/helpbutton.gif"></A></TD>
 </TR></H3></TABLE>
 <DL><DD><select name="compress">
 <option value="zip"selected> zip - recommended for PCs and Macs
 <option value="tarZ"> tar.Z - UNIX compressed tar file
 <option value="targz"> tar.gz - GNU gzip compressed tar file
 <option value="tar"> tar - UNIX tar file (no compression)
 </select></H3></DD></DL>
 <p>
 
 <TABLE BORDER=0 WiDTH=100% CELLSPACING=0 CELLPADDING=5>
  <TR><TD BGCOLOR="$cgobar">
   <H3><font color=black><input width="80" type="submit" value="Go"></font>
  </TR></TABLE></H3></form>

ENDOFTEXT
  }	# end if (object)
#
# Close the document cleanly.
#
  print &HtmlBot;
}
