#! /bin/sh
# make-cgi-input --- simulate CGI script input format
# Author: Noah Friedman <friedman@prep.ai.mit.edu>
# Created: 1995-08-08
# Public domain

# $Id: make-cgi-input,v 1.2 1995/09/23 22:02:24 friedman Exp $

# Commentary:
# Code:

# Name by which this script was invoked.
progname=`echo "$0" | sed -e 's/[^\/]*\///g'`

# To prevent hairy quoting and escaping later.
bq='`'
eq="'"

default_file=",cgi-test-data"

usage="Usage: $progname {options} [name1=value1] {name2=value2} {...}

Options are:
-D, --debug                  Turn on shell debugging ($bq${bq}set -x$eq$eq).
-h, --help                   You're looking at it.
-o, --output-file   FILE     Send output to FILE.
                             By default, output goes to $bq$default_file$eq.
                             If FILE is $bq-$eq, output goes to stdout.
"

# Initialize variables.
# Don't use `unset' since old bourne shells don't have this command.
# Instead, assign them an empty value.
debug=
verbose=
output_file="$default_file"

# Usage: eval "$getopt"; value=$optarg
# or     optarg_optional=t; eval "$getopt"; value=$optarg
#
# This function automatically shifts the positional args as appropriate.
# The argument to an option is optional if the variable `optarg_optional'
# is non-empty.  Otherwise, the argument is required and getopt will cause
# the program to exit on an error.  optarg_optional is reset to be empty
# after every call to getopt.  The argument (if any) is stored in the
# variable `optarg'.
#
# Long option syntax is `--foo=bar' or `--foo bar'.  2nd argument
# won't get used if first long option syntax was used.
#
# Note: because of broken bourne shells, using --foo=bar syntax can
# actually screw the quoting of args that end with trailing newlines.
# Specifically, most shells strip trailing newlines from substituted
# output, regardless of quoting.
getopt='
  {
    optarg=
    case "$1" in
      --*=* )
        optarg=`echo "$1" | sed -e "1s/^[^=]*=//"`
        shift
       ;;
      * )
        case ${2+set} in
          set )
            optarg="$2"
            shift
            shift
           ;;
          * )
            case "$optarg_optional" in
              "" )
                case "$1" in
                  --*=* ) option=`echo "$1" | sed -e "1s/=.*//;q"` ;;
                  * ) option="$1" ;;
                esac
                exec 1>&2
                echo "$progname: option $bq$option$eq requires argument."
                echo "$progname: use $bq--help$eq to list option syntax."
                exit 1
               ;;
           esac
         ;;
        esac
     ;;
    esac
    optarg_optional=
  }'

# Parse command line arguments.
# Make sure that all wildcarded options are long enough to be unambiguous.
# It's a good idea to document the full long option name in each case.
# Long options which take arguments will need a `*' appended to the
# canonical name to match the value appended after the `=' character.
while : ; do
  case $# in 0) break ;; esac
  case "$1" in
    -D | --debug | --d* )
      # This flag is passed to perl for interactive debugging.
      debug=-d
      shift
     ;;
    -h | --help | --h* )
      echo "$usage" 1>&2
      exit 0
     ;;
    -o | --output-file* | --o* )
      eval "$getopt"
      output_file="$optarg"
     ;;
    -- )     # Stop option processing
      shift
      break
     ;;
    -? | --* )
      case "$1" in
        --*=* ) arg=`echo "$1" | sed -e 's/=.*//'` ;;
        * )     arg="$1" ;;
      esac
      exec 1>&2
      echo "$progname: unknown or ambiguous option $bq$arg$eq"
      echo "$progname: Use $bq--help$eq for a list of options."
      exit 1
     ;;
    -??* )
      # Split grouped single options into separate args and try again
      optarg="$1"
      shift
      set fnord `echo "x$optarg" | sed -e 's/^x-//;s/\(.\)/-\1 /g'` ${1+"$@"}
      shift
     ;;
    * )
      break
     ;;
  esac
done

case "$debug" in -d ) set -x ;; esac

case $# in
  0 )
    echo "$usage" 1>&2
    exit 1
   ;;
esac

case "$output_file" in
  - ) : ;;
  * ) exec > "$output_file" || exit $? ;;
esac

exec perl $debug - ${1+"$@"} <<'__EOF__'

$outstring = "";

foreach $arg (@ARGV)
  {
    local ($key, $data) = split (/=/, $arg, 2);

    # For any char that is not a space or alphanumeric character, convert
    # it to its hexadecimal equivalent.
    # Spaces are included among the untranslated chars since they are
    # handled specially later.
    $key  =~ s/([^ \-.\/0-9:\@A-Za-z])/sprintf("%%%.2X", unpack("C",$1))/ge;
    $data =~ s/([^ \-.\/0-9:\@A-Za-z])/sprintf("%%%.2X", unpack("C",$1))/ge;

    # Now convert any spaces to `+'
    $key  =~ s/ /+/go;
    $data =~ s/ /+/go;

    $outstring = "$outstring$key=$data&";
  }

# Remove trailing `&' appended in last iteration of loop.
chop $outstring;

print $outstring;

__EOF__

# make-cgi-input ends here
