#!/usr/bin/perl -w
{
#   list-via-web.pl   WJS Aug 15
#   Provide access to  list  output in a web environment

#   Takes a PATH_INFO of an object spec w/o sel/proj list; QUERY_STRING of comma-separated sel/proj list plus an
#   @LIST_SWITCHES@=list-of-list-switches item, also comma-separated within the QUERY_STRING.  QUERY_STRING
#   item order is irrelevant.  If the @LIST_SWITCHES@ item is missing, program
#   prints list's help.  list-of-list-switches is a string consisting of list switches elided together with
#   "their" hyphens (not sure if a hyphen is, properly, part of a command line switch); eg, to ask for flat,
#   nocomment, delete-extra-spaces, use
#         @LIST_SWITCHES@=-f-c-z
#   Not all list switches are permitted; excluded are -errout, -m, -errprefix, and ?

#   Code stolen from listfznm.  Whole issue w/this program is getting
#   the list of list switches in the query string in a way that might jibe with other such
#   query strings.  Again, the issue is that a query string item of  "string"  or  "string=value"
#   can be construed as a projection or selection.  Since "string" represents a varname, the idea
#   is to make strings that are NOT legal varnames ... and hope that other software checks when
#   it needs to !
  $version = "list_via_web v 1.1b  3 Aug 2016";
#   3 Aug 16  v 1.1b  WJS
#       Check switch list for legal characters
#   1 Jul 16  v 1.1a  WJS
#       Bug fix: substitute some ""s for undefined variables to avoid diagnostic about non-definition
#  18 Jan 16  v 1.1  WJS
#       Bug fix: when splitting on comma, need to be sure that last piece has all the comma-
#   separated fields of the sel/proj list

  require ("cgi-lib.pl");
  require ("wjs_web_perl_utilities.pl");

#   MUCH easier to do bulk of work BEFORE calling build-opt-env.pl, so do NOT "do" build-opt-env after setting
#   it up here.   Accordingly we do NOT have access to all the stuff in *utilities.pl
#   (until we do the call, of course) (hmm, not sure of this! but we certainly can't use the "standard
#   env vars)
#     Assume .pl routine is in our directory
  $build_opt_env = "./build-opt-env.pl";
  &check_r_access($build_opt_env);

  $illegal_outer_char = "@";
  $QS_keyword = $illegal_outer_char . "LIST_SWITCHES" . $illegal_outer_char;
  $qm_QS_keyword = quotemeta($QS_keyword);
  $within_QS_value_sep_char = "-";
  $qm_within_QS_value_sep_char = quotemeta($within_QS_value_sep_char);

#   First switch prohibited because users shouldn't be spraying files around OOserver
#   Next 2 switches prohibited because I don't want to deal with their values
#   Last switch prohibited because I don't want to deal with issues propagating question marks
  @array_of_prohibited_switches = ("errout", "m", "errprefix", '?');

#   Coordinate names w/query_string pkg.  Ramifications of "PREFERRED" not
#   really appreciated at this point
  $PREFERRED_QS_SEPARATOR = ',';
#   Shorter version of above for coding convenience
  $qs_sep = $PREFERRED_QS_SEPARATOR;
  $qm_qs_sep = quotemeta($qs_sep);

  ($part1,$part2,$part3) = split (/$qm_QS_keyword/,$ENV{"QUERY_STRING"});
  $part3 && &quitx ("$QS_keyword specified more than once in QUERY_STRING ",$ENV{"QUERY_STRING"});
  if ($part2) {
    ($list_switches,$remainder) = split (/$qm_qs_sep/,$part2,2);
    $list_switches || &quitx("Could not get set of list switches from $QS_keyword $part2");
    $tmp = $list_switches;
    ($list_switches) = ($tmp =~ /\s*=\s*(.+)/);
    $list_switches || &quitx("Could not get set of list switches from $tmp");
    ($list_switches =~ /^[\-A-Za-z]*$/) || &quitx("Illegal character in switch list.  Only hyphens and letters allowed");
    @array_of_list_switches = split(/$qm_within_QS_value_sep_char/,$list_switches);
    foreach (@array_of_list_switches) {
      $candidate = $_;
      foreach (@array_of_prohibited_switches) {
        ($candidate eq $_) && &quitx ("list switch $_ not supported in web interface");
      }
    }
#       Hyphens below are "unix command line hyphens"; logically different from the hyphen-as-value-sep-char
#       (but choice as value-sep-char was hardly coincidental)
    $space_separated_list_switches = join (" -", @array_of_list_switches);
    defined ($part1) || ($part1 = "");
    defined ($remainder) || ($remainder = "");
    $new_query_string = $part1 . $remainder;
    $ENV{"QUERY_STRING"} = $new_query_string;
  } else {
    $space_separated_list_switches = "-h";
  }

#   Set up environment.
  do $build_opt_env;

  &list_as_text_plain($space_separated_list_switches,$build_opt_env);

  undef $version;
  exit(0);

sub quitx
{
  do $build_opt_env;
  &quit(@_);
}
}
