#!perllocation
#
# Name:         $OPTHOME/optbin/download-3
# Purpose:      part of JGOFS data download package
#		get user to confirm options selected so far
#
# Modification history:
# Dec 98.clc. under development.
# 981217.clc. cleanup determine# section.
# 981018.clc. add build-download-env.plx
# 981223.clc. work on bulk=onebig, maxlev=0
# 990204.clc. work on maxlev > params selected; OK done.
# 990210.clc. compress variable; bulleted selections list
# 990211.clc. option to name datapkg file; break main into subrs
# 990222.clc. more option to name datapkg file
# 990224.clc. remove debug footer.
# 990308-09.clc. add listm cmd line options; verbage mods; 
#		table of confirm choices; tar only (no compression);
# 990311.clc. if no params selected force user to go back 
# 990312.clc. asst. appearance mods.; fix one-big/no params bug; 
#		opt_sep bug; color pallette
# 990322.clc. add subselection capability for one big file pkg type.
# 990326.clc. try 3 button end-of-form. Yes No help (Submit)
# 990614.clc. remove DBG lines.
# 990616.clc. reword pkgname verbage (no code changes).
# ###### download version 1.0
# 990621. clc. port to OO server for installation/testing.
# 991122-26.clc. mods for merge with rest of oopt software;
#		dobject;subsdisp;dtempdir
# 991201.clc. merge/debug with ooptserver on synthesis; dispobj; fmt header
# 991206.clc. use OBJEXT to fix remote object bug
# 991209.clc. allow object with subsels to be pkgd into multiple files
# ###### download version 1.1  (991209)
#
# =====================================================================

require "cgi-lib.pl";

MAIN:
{
  print STDERR " DWNLD: using script: $0 \n";
  
  # color palette
  $credhi = "indianred";
  $chdrbar1 = "skyblue";
  $cgobar = "mediumaquamarine";
  
  $paramsg = "Pick one parameter at level";
#
# 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
#
# Read in all the variables set by the form
#
  &ReadParse(*input);
  
  # Get some information from input string
  $dobject = "$input{'dobject'}";	# jgofs object string
  $dobjext = "$input{'dobjext'}";	# jgofs object string with extension
  $dispobj = "$input{'dispobj'}"; 	# jgofs object+(QS) for display
  $objnameonly=substr($dobject,rindex($dobject,"/")+1);
  $subsels = "$input{'subsels'}";
  $subsdisp = "$input{'subsdisp'}";
  $maxlev = "$input{'maxlev'}";
  $bulk = "$input{'bulk'}";
  $compress = "$input{'compress'}";
  $listm_user = "$input{'opt_defs'}"."$input{'opt_sep'}"."$input{'opt_brief'}"."$input{'opt_msval'}";

  # Print the header
  print &PrintHeader;
  
  if ($bulk eq "onebig") {
    $pkgtype="one single file";
  } else {
    $pkgtype="multiple small files";
  }
      
  print STDERR " DWNLD: working on object: $dobject \n";

# What file extension will be appended onto filename
  $fullext = &fileext($compress); 

# Prompt user with options selected so far:
  print <<ENDOFTEXT;
   <BODY BGCOLOR="#FBFBFF">
   <h1>Data download continuing ...</h1>
   <b>Current object is:  $dispobj</b></br>
   <hr noshade><p>
  <TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=6>
  <TR><TD COLSPAN=3 BGCOLOR="$chdrbar1">
    <H3>You have requested:
  </TR></H3></TABLE>
        
  <UL>
  <LI><FONT color="$credhi">$pkgtype</FONT> for $objnameonly data<BR>
  <LI>Packaged as a <FONT color="$credhi">$fullext</FONT> file<BR>
ENDOFTEXT

  print "<LI>subselection list is: $subsdisp<BR> \n" if $subsels;

&list_options;

#
# List parameters from user-selected level determinants
# returns % separated string of selected parameters (paramstr)
# paramstr="" (null string) in the case of onebigfile 
  $paramstr = "";
  $paramstr = &list_selected_params if ($bulk eq "moresmall");
#
# if user hasn't selected any parameters to split data into "moresmall" 
# files, then force them to go back and do so
  if ($paramstr eq "" && $bulk eq "moresmall") {
    &go_back 
  } else {
#
# endtags for any lists still open (just to be safe)
#  
  print "<\/UL><\/UL><\/DL>  \n"; 
#
# Offer option to name packaged data file
# 
  print "<form method=\"post\" action=\"$jgscdir\/download-4\"> \n";
  &name_datapkgfile($paramstr);
#
# Prompt for user confirmation of selections
#  
  &ask_user_to_confirm($paramstr);
} 

#
# Close the document cleanly.
#
  print &HtmlBot;
}


sub fileext {
  my ($compress) = @_;
  
# Determine which package type user has selected (zip, tar, etc.):
  $fullext = "tar.Z";
  if ( index($compress,"tar") > -1 ) {
  # UNIX type utilities selected
  # UNIX compress selected by default
    if ( $compress eq "targz" ) {
      $fullext = "tar.gz";
    } else {
      if ( $compress eq "tar" ) {
        $fullext = "tar";
      }
    }
  } else {
  # PC/Mac type utilities selected
      $fullext = "zip";
  }
  return $fullext;
}


sub list_options {
# Determine which listm options user has selected.
# Build hash of optional command line listm options and definitions: 

  %opt_txt = (  "s" => "space-separated, data aligned in columns, ",
  		"t" => "tab-separated, data aligned in columns, ",
  		"b" => "no header info, ",
  		"m" => "missing value converted to -9999.0, ",
  		"z" => "comma-separated, not aligned in columns, ");

  $opt_defaults = "comma-separated flat file, data aligned in columns, with header info";
  $opt_txt_str = "";
  foreach $opt ( keys %opt_txt ) {
    substr($opt_txt_str,0,0) = $opt_txt{$opt} if ( $listm_user =~ /$opt/ );
  }
  if ( $opt_txt_str eq "" ) {
     $opt_txt_str = $opt_defaults;	# still using default options
  } else {
     $opt_txt_str =~ s/, $//;		# remove trailing comma&blank
  }
  print "<LI>Special formatting: $opt_txt_str <BR> \n";
}


sub list_selected_params {
#
# Build list of parameters from user-selected level determinants
#
  print "<LI>Determined by $maxlev parameter(s) which is (are)<BR><DL>\n";     
  
  $determnum = "";
  $levelparams = "";
  foreach $num (0 .. $maxlev-1) {
    $determnum = "determine"."$num";
    $determinant = "$input{$determnum}";
    # check whether user has made a selection for each parameter
    #   if selected parameter is still 'Pick one ...', 
    #   decrement maxlev and shuffle params
    if ( ($found = index($determinant,$paramsg)) > -1 ) {
       $maxlev = --$maxlev;
       $determinant = "";
    }
    # if user made a selection at this level, 
    # add it to levelparams, the final selection parameter list
    if ("$determinant") {
      print "<DT><FONT color=\"$credhi\">$determinant</FONT> &nbsp;&nbsp;&nbsp; [at level $num]<BR>\n";
      $levelparams .= "%"."$determinant";
    } else {
      print "[ no parameter selected at level $num ] <BR>\n";
    }
  }
#
# remove leading % character from final list of parameters
#
  $levelparams =~ s/^%//;
  
  return $levelparams;
}


sub name_datapkgfile {
   my ($paramlist) = @_;
#
# Present user with option to name packaged data file ($pkgname)
# default is $dobject, but might also be a selected param/determinant
# offer drop down list of whatever I know about the selections
#
# This has to be inside a FORM tag
#
  print <<ENDOFTEXT;
  <TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=6>
  <TR><TD COLSPAN=3 BGCOLOR="$chdrbar1">
    <H3>What would you like to name the datafile?</TD>
    <TD BGCOLOR="$chdrbar1">
      <A HREF="$helpaddr/download-help.html#pkgname"><img alt="[HELP]" 
      align=absmiddle border=0 src="$webimages/helpbutton.gif"></A></TD>
  </TR></H3></TABLE>
  <P> 
  The selected 'name' will be used in naming the final packaged data file.
  For example, the default is to name the file according to the selected 
  object, <FONT COLOR="$credhi">$objnameonly</FONT>,  
  which has been preselected from the drop-down list below.
  In this case the resultant file would be named: 
  <B><FONT COLOR="$credhi">$objnameonly</FONT>.</B><FONT COLOR="$credhi">$fullext</FONT><BR>
  Notice, the appropriate file extension 
  (<FONT COLOR="$credhi">$fullext</FONT>) has been appended to 'name'.  
  <dl>
  <dt><dd><B>Choose a name for your datafile: </B> &nbsp
  <select name="pkgname">
  <option value="$objnameonly" selected> $objnameonly
ENDOFTEXT

  foreach $param (split(/%/,$paramlist)) {
    print "<option value=\"$param\"> $param \n";
  }
  
print <<ENDOFTEXT;  
  </select>
  <dt><dd><B>or type another name in this box</B> &nbsp
  <input name="customname"><p>
  </dl></dl><p>
ENDOFTEXT
}

sub ask_user_to_confirm {
#
# Ask user to confirm selections so far
#
  print <<ENDOFSUB;

 <TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=6>
 <TR><TD COLSPAN=3 BGCOLOR="$chdrbar1">
   <H3>Are you satisfied with these selections?
 </TR></H3></TABLE>
 
 <input type="hidden" name="levelparams" value="$levelparams">
 <input type="hidden" name="bulk" value="$input{'bulk'}">
 <input type="hidden" name="dobject" value="$input{'dobject'}">
 <input type="hidden" name="dobjext" value="$input{'dobjext'}">
 <input type="hidden" name="dispobj" value="$dispobj">
 <input type="hidden" name="maxlev" value="$maxlev">
 <input type="hidden" name="compress" value="$input{'compress'}">
 <input type="hidden" name="pkgname" value="$pkgname">
 <input type="hidden" name="subsels" value="$subsels">
 <input type="hidden" name="subsdisp" value="$input{'subsdisp'}">
 <input type="hidden" name="dtempdir" value="$input{'dtempdir'}">
 <input type="hidden" name="listm_user" value="$listm_user">

 <TABLE BORDER=0 CELLSPACING=20 CELLPADDING=0>
 <TR>
  <TD WIDTH="100">
    <B><input type="submit" width="80" style="background:$cgobar" name="confirm" value="yes"></B></TD>
  <TD WIDTH="290">
    <B><input type="submit" width="80" style="background:$credhi" name="confirm" value="no"></B><FONT COLOR="$credhi">  Go back and try again.</FONT></TD>
  <TD WIDTH="210">
    <B><input type="submit" width="80" style="background:blue;color:white" ="white" name="confirm" value="help"></B><FONT COLOR="blue">  me please!</FONT></TD>
  </B></TR></TABLE>
 </form>

ENDOFSUB
}


sub go_back {
   # Need to redirect user if they requested multi file output, 
   # but neglected to select any params by which we can divide data
   $previous_page=$ENV{'HTTP_REFERER'};
   print <<ENDOFBACK;
   </UL></UL></DL>
   <HR NOSHADE WIDTH="100%"> 
   <H3>
   You have not selected any parameters which can be used to subdivide 
   your data. Please use your browser's 'Back button' to go back
   and select at least one parameter.</H3><p>  
   <HR SIZE=4 NOSHADE WIDTH="100%"><br>   
      
ENDOFBACK

}
