#!/opt/bin/perl
$DATE= &getlocaldate;
$CODE= &getaccesscode;
# Print out a content-type for HTTP/1.0 compatibility
print "Content-type: text/html\n\n";

# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs
@pairs = split(/&/, $buffer);
$oldname="";
foreach $pair (@pairs)
{
    ($name, $value) = split(/=/, $pair);

    # Un-Webify plus signs and %-encoding
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    # escape backslashes in data
    $value =~ s/\\/\\\\/g;
    # escape colons in data
    $value =~ s/:/\\:/g;

    # Uncomment for debugging purposes
    # print "Setting $name to $value<P>";

    # textareas contain returns so you have to concatenate subsequent lines 
    # to the variable specified in the first line
    if ($name eq $oldname) {
       $FORM{$oldname} .= ",".$value;
    } else { 
       $FORM{$name} = $value;
       $oldname = $name;
    }
}

# holding value for blank checkboxes
$FORM{'admission'}="not specified" if ! $FORM{'admission'};
$FORM{'affiliations'}="not specified" if ! $FORM{'affiliations'};
$FORM{'flight_cert'}="not specified" if ! $FORM{'flight_cert'};
$FORM{'nonflight_cert'}="not specified" if ! $FORM{'nonflight_cert'};
$FORM{'flight_instr'}="not specified" if ! $FORM{'flight_instr'};
$FORM{'opportunities'}="not specified" if ! $FORM{'opportunities'};

if ($FORM{'key'})
{
   # Create a database file to be used for editing a survey and
   # append text file for downloading to Microsoft Access
   $DBASEFILEDIR=$FORM{'datadir'};
   $DBASEFILE=$FORM{'key'}.".".$CODE;
   $TEXTFILE="cag_survey.txt";
   open (DBASEFILE, ">$DBASEFILEDIR$DBASEFILE") || print "Can't create the database file $DBASEFILEDIR$DBASEFILE: $! <BR>\n";
   open (TEXTFILE, ">>$DBASEFILEDIR$TEXTFILE") || print "Can't append to the text file $DBASEFILEDIR$TEXTFILE: $! <BR>\n";
   foreach $key (sort keys %FORM) {
      if ($key eq "datadir" || $key eq "returnurl") {
         next;
      }
      print DBASEFILE $key,":",$FORM{$key},"\n";
      #
      # the download file needs a little cleanup work
      # convert newlines in textareas to commas and concatenate into single line
      #
      $FORM{$key} =~ s/\r\n/,/g;
      #
      # strip off trailing comma(s) if present:
      #
      # pattern: =~ = the string search operator
      #  |       ^  = beginning of line
      #  |       .* = any number of characters except newline
      #  |       ?  = match the smallest number of characters
      #  |    (...) = remember this part of pattern to use later
      #  |     ,*$  = match any number of commas at end of line                        
      #  |
      #  |-------------------------------|
      #                                  |
      #        string---|            VVVVVVVVVVV
      #                 V
      ($FORM{$key}) = $FORM{$key} =~ /^(.*?),*$/;
      #
      # ^^^^^^^^^^                     ^^^^^                                  
      #     |                            |
      #     |--------- store here -------|
      #
      print TEXTFILE "\"",$FORM{$key},"\",";
      # print TEXTFILE "\"",$key,"\",";
   }
   print TEXTFILE "\"",$CODE,"\"\r\n";
   close (DBASEFILE);
   close (TEXTFILE);
   print "<head><title> Thank you </title>\n";
   print "<h1>Thank you for your submission!</h1>\n";
   print "</head>\n";
   print "<body>\n";
   print "Your Access Code for updating your survey is $CODE.<br>\n";
   print "<P>\n"; 
   print "<a href=\"http://uaa.auburn.edu\"> Return to University Aviation Association Home Page </a><br>\n";
   print "</body>\n";
} else {
   print "<head><title> Error </title>\n";
   print "<h1>Error: You must enter the Official Name of your institution!</h1>\n";
   print "</head>\n";
   print "<body>\n";
   print "<a href=\"$FORM{returnurl}\"> Go back and try again </a><br>\n";
   print "</body>\n";
}
exit;

sub getlocaldate {
   local($sec, $min, $hour, $mday, $month, $year, $wday, $yday, $isdst) = localtime(time);
   $month++; # months start with zero
   $min="00" if $min == 0;
   $min="0$min" if ($min < 10);
   $mday=" $mday" if ($mday < 10);
   local($mon)= "MONTH";
   $mon="Jan" if $month== 1;
   $mon="Feb" if $month== 2;
   $mon="Mar" if $month== 3;
   $mon="Apr" if $month== 4;
   $mon="May" if $month== 5;
   $mon="Jun" if $month== 6;
   $mon="Jul" if $month== 7;
   $mon="Aug" if $month== 8;
   $mon="Sep" if $month== 9;
   $mon="Oct" if $month==10;
   $mon="Nov" if $month==11;
   $mon="Dec" if $month==12;
   return "$mday $mon $year $hour:$min";
}

sub getaccesscode {
   local($sec, $min, $hour, $mday, $month, $year, $wday, $yday, $isdst) = localtime(time);
   $month++; # months start with zero
   $min="0$min" if ($min < 10);
   $mday="0$mday" if ($mday < 10);
   $month="0$month" if ($month < 10);
   return "$year$month$mday$hour$min";
}

