#!/usr/bin/perl -w
#
# Name:         $OPTHOME/optbin/download-1
# Purpose:      part of JGOFS data download package
#		Verify data object name; determine file packaging type.
#				  C Chandler Dec 98
#
$version = "download-1  version 3.3a  7 Jul 2016";
#  7 Jul 2016 WJS
# Comment regarding "the" 4GB limit on zip files
#	  [begin 3.3a]
#  2 Dec 12	WJS
#	Make available features like compression depend on build-env env
#    vars.  download-1 will handle compression options.  Also, the 
#    code to check "everything" that needs checking
#    among the build-env env vars is now here instead of in download-4
#	  [begin 3.3]

# 18 Apr 10	WJS
#	Avoid "only 1 use diagnostic" for var apparently no longer used...
#	Add exit
#	  [begin 3.2a]
#  7 Feb 10	WJS
#	Put URLOBJX and extended URL values (if any) into form for download-2
#    While at it, check input from build-opt-env.  This leads to removal
#    of graceful treatment of a "missing object" problem ... graceful
#    but not particularly correct, since it sent the user to the OOserver's
#    dir, not the dir for the data object (cf reason for v 3.2 mod!)
#	  [begin 3.2]
# 28 Nov 08	WJS
#	1 Nov fooling didn't take into account the various "retreats"
#    programmed into download.  For now, punt w/an error message.
#  1 Nov 08	WJS
#	Try to move non-sel/proj qs args "into" obj spec on hypothesis
#    that these args are "part" of object.  Motivation: url of
#    ...join...?obj1,obj2,sel/proj should be considered
#    join(obj1,obj2) + sel/proj rather than join + obj1,obj2,sel/proj
#	Parametrize tracefile but leave it as STDERR for now.
#	  [begin 3.1]
#  9 May 08	WJS
#	Log with $version rather than script name
#	See download-3 regarding why this is version 3.0
# 30 Aug 07	WJS
#	Remove some code duplicated in utilities.pl
# 29 Jun 05	WJS
#	Add netcdf option
# 17 Dec 04	WJS
#	Assume all download programs execute build-opt-env.pl
#    This allows removal of template & reduction in # hidden vars
#	Hardcode perl location (part of template removal) & add -w
#	Move old comments to download-X_tmp.comments
#	Remove ReadParse call - this program doesn't get any input
#    (at least, if it ever did, said input wasn't processed!)
#	Remove a couple of "only used once" variables
#	Always send a couple of variables to download-2 so they will
#    be defined there.  Previously they were send only if non-empty
#
# =====================================================================

require "cgi-lib.pl";
require "wjs_web_perl_utilities.pl";
#
# setup local operating environment
#
  $build_opt_env = "./build-opt-env.pl";
  &check_r_access($build_opt_env);
  require "$build_opt_env";

#   Do some checking on result of setup.  Check everything even though
#   this particular pass through the program might work w/o one or the 
#   other of these... 
  foreach('JGOFSDIR','JGSCRIPTDIR','BUTTONIMAGESDIR',
						'INFOSERVER','USETEMPDIR') { 
    $ENV{$_} || 
      &quit ("Internal problem.  ", 
	"Did not get defined, non-empty env var $_ from $build_opt_env");
  }
  $jgtopdir = &abs_filespec($ENV{'JGOFSDIR'},"Env var JGOFSDIR");
  $jgbindir = "$jgtopdir/bin";

  $any_compression = 0;
  foreach('TARPROG','UNIXCOMPRESS','GNUCOMPRESS','ZIPPROG') {
    $ENV{$_} && ($any_compression = 1) && &check_x_access($ENV{$_});
  }
  $ENV{'MAIL_SENDER'} && &check_x_access($ENV{'MAIL_SENDER'});

MAIN:
{
#
# get object, subselection, etc. from env vars set in build-opt-env.pl
#
#   jgofs object string
  $dobject = &check_build_opt_env_var('OBJECT',$build_opt_env);  
#   jgofs object string with extension
  $dobjext = &check_build_opt_env_var('OBJEXT',$build_opt_env);  
#   jgofs object+(QS) for display
  $dispobj = &check_build_opt_env_var('DISPOBJ',$build_opt_env); 
#   http link to data object (approx - see build-opt-env.pl)
  $urlobjx = &check_build_opt_env_var('URLOBJX',$build_opt_env); 
#   subselections list to pass along. Might be empty, so don't &check_build... 
  $subsels = $ENV{'SUBSELS'};
#   un-trigram'd subsels list for display
  $subsdisp = $ENV{'DISPSS'};

#   OOserver does NOT get a data= ext URL field, since it already knows
#   the object spec
  $extURL_env_var_prefix = 
		&check_build_opt_env_var('PATH_INFO_PREFIX',$build_opt_env);  
  $dobj_dirserver_env_var = $extURL_env_var_prefix . "dir";
  $dobj_infoserver_env_var = $extURL_env_var_prefix . "info";
  ($dobj_dirserver = $ENV{$dobj_dirserver_env_var}) || ($dobj_dirserver = ""); 
  ($dobj_infoserver = $ENV{$dobj_infoserver_env_var}) || 
						($dobj_infoserver = ""); 

  $previous_page = $ENV{'HTTP_REFERER'}; 	# referring URL
#
# color palette
#
  $credhi = "indianred";
  $chdrbar1 = "skyblue";
  $cgobar = "mediumaquamarine";
#
# These variables set by opt-build-env.pl via build-opt-env.pl
#
#   CGI script locations
  $jgscdir   = &check_build_opt_env_var("JGSCRIPTDIR",$build_opt_env);
#   web images
  $webimages = &check_build_opt_env_var("BUTTONIMAGESDIR",$build_opt_env);
#   URL for web help docs
  $helpaddr  = &check_build_opt_env_var("HELPADDR",$build_opt_env);
#   top of JGOFS tree
  $jgtopdir  = &check_build_opt_env_var("JGOFSDIR",$build_opt_env);
#
  $jgbindir  = "$jgtopdir/bin";				# JGOFS binary execs
  $pqs = "$jgbindir/parse_query_string";

  $no_goback_url = "http://" . 
	&check_build_opt_env_var("MYADDR",$build_opt_env) . 
						    "/no_download_goback.html";

  $tracefile = "STDERR";
#
# -------------------------------------------------------------------
#

  #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 $tracefile "\nData DWNLD: === using script $version === \n";
  print $tracefile "DWNLD: dobject: $dobject \n";
  print $tracefile "DWNLD: dobjext: $dobjext \n";
  print $tracefile "DWNLD: dispobj: $dispobj \n";
  print $tracefile "DWNLD: subsels: $subsels \n";
  print $tracefile "DWNLD: subsdisp: $subsdisp \n";
  print $tracefile "DWNLD: dobj_dirserver: $dobj_dirserver \n";
  print $tracefile "DWNLD: dobj_infoserver: $dobj_infoserver \n";
     
#   "Adjust" object spec and query string (see comments up top and/or
#   comments in wjs_web_perl_utilities.pl)
  ($status,$substatus,$new_dobject,$new_subsels,$n_moved) = 
				&adjust_for_reqd_args($pqs,$dobject,$subsels);
  ($status eq "OK") || 
	&quit ("Problem adjusting object and subsels $dobject and $subsels",
		"$substatus: $new_dobject");
  ($substatus eq "NOARGKEY") || 
    ($substatus eq "ARGSINSPEC") ||
    ($substatus eq "MOVEDNEEDEDARGS") || 
	(print $tracefile "... problematic adjust_for_reqd_args substatus " .
	       "$substatus\n");
  if ($n_moved > 0) { 
    ($status,$substatus,$errmsg,$subsdisp) = 
			&adjust_for_reqd_args($pqs,$dobject,$subsdisp);
    ($status eq "OK") || 
	&quit ("Problem adjusting object and subsels $dobject and $subsels",
		"$substatus: $errmsg");
    $dobjext = "NO_VALID_DOBJEXT";
  }
  $dobject = $new_dobject;
  $subsels = $new_subsels;

  $now = scalar localtime;
  $adjusted = ($n_moved > 0) ? "adjusted" : "";
  print $tracefile "Data DWNLD: working on $adjusted object: $dobject \n";
  print $tracefile "Data DWNLD: request started $now \n";

  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="urlobjx" value="$urlobjx">
 <input type="hidden" name="subsels" value="$subsels">
 <input type="hidden" name="subsdisp" value="$subsdisp">
 <input type="hidden" name="dobj_dirserver" value="$dobj_dirserver">
 <input type="hidden" name="dobj_infoserver" value="$dobj_infoserver">
ENDOFTEXT
# 
# how should we divide/package the data?
#
 if ($subsels ne "") {
#
# User has selected a list of projections, subselections.
#
   $go_back = ($dobjext eq "NO_VALID_DOBJEXT") ? 
			$no_goback_url : "$jgscdir/download-1$dobjext";
   print <<ENDOFTEXT;
  <FONT SIZE=+1><B>Subselections: </B></FONT>$subsdisp <BR>

  <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="$go_back">
   <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 format option
#   
 print <<ENDOFTEXT;   
 <TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=6>
 <TR>
   <TD COLSPAN=3 BGCOLOR="$chdrbar1">
      <H3>Please choose a format option:</TD>
   <TD BGCOLOR="$chdrbar1">
      <A HREF="$helpaddr/download-help.html#format"><img alt="[HELP]" 
      align=absmiddle border=0 src="$webimages/helpbutton.gif"></A></TD>
 </TR></H3></TABLE>
 <DL><DD><select name="format">
 <option value="text"selected> text
ENDOFTEXT

  $ENV{'NETCDFLIBDIR'} && 
        (-x "$jgbindir/makenetcdf") &&
		print qq|<option value="netcdf"> netcdf|;
  $ENV{'MATLABLIBDIR'} && 
        (-x "$jgbindir/makemat") &&
		print qq|<option value="matlab"> matlab (default version)|;
  if ($matlab_version_list = $ENV{'MATLAB_VERSION_LIST'}) {
    foreach (split /,/,$matlab_version_list) {
      (  ($matlab_version) = /matlab(.+)/  ) &&
	  $ENV{"$_" . "LIBDIR"} &&
          (-x "$jgbindir/makemat$matlab_version") &&
		print qq|<option value="$_"> matlab (version $matlab_version)|;
    }
  }

 print <<ENDOFTEXT;   
 </select></H3></DD></DL>
 <p>
ENDOFTEXT

#
# User selects compression option if available
# Note that this is almost as much "packaging" as it is "compression" -
#   we need to deliver our product as a single file across the web, and if
#   our "break up data by selection" feature is being used, the "compression"
#   value determines how to make that single file
# Values for compression options are, at the moment, the "file type"
#   corresponding to the compression, except for tarZ (type tar.Z) and
#   targz (type tar.gz).  Don't particularly want to find out what happens
#   if I just stick the  .  in there.
#   
 if ($any_compression) {
   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">
ENDOFTEXT

#   Present options in order of "desirability" - first option actually
#   presented to use is the default option (Note interesting situation if
#   print statements fail ...)
  $selected = "selected";
  $ENV{'ZIPPROG'} && 
    (print
      qq|<option value="zip" $selected> zip - recommended for PCs and Macs (4GB data limit)|) &&
    ($selected = "");
  $ENV{'TARPROG'} && 
    (print
      qq|<option value="tar" $selected> tar - UNIX tar file (no compression)|) &&
    ($selected = "");
  $ENV{'GNUCOMPRESS'} && 
    (print
      qq|<option value="gz" $selected> gz - GNU gzip compressed text file| .
		qq| (only for "One single file" downloads)|)		     &&
    ($selected = "");
  $ENV{'TARPROG'} && $ENV{'UNIXCOMPRESS'} &&
    (print
      qq|<option value="tarZ" $selected> tar.Z - UNIX compressed tar file|) &&
    ($selected = "");
  $ENV{'TARPROG'} && $ENV{'GNUCOMPRESS'} &&
    (print
      qq|<option value="targz" $selected> tar.gz - GNU gzip compressed tar file|)
	&&
    ($selected = "");

  print <<ENDOFTEXT;   
   </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
  } else {
    print qq|<input type="hidden" name="compress" value="NONE">|;
  }
#
# Close the document cleanly.
#
  print &HtmlBot;

#   Avoid "one time use" diagnostics
  undef ($credhi);
  undef ($previous_page);
  exit 0;
}
