#!/usr/local/bin/perl

use CGI qw(:all);
use CGI::Carp qw/fatalsToBrowser/;

chdir "/www/ozwv/cgi-bin" or die "Error 1:  Couldn't \'chdir\'";
$htmldir = "/www/ozwv/dobson/adhoc/";

$index = param('file');
$thots = param('thoughts');
$name =  param('name');

@files = ( "onlineforum.html",    # 0
           "committee.html",      # 1
           "homepages.html",      # 2
           "newsletter.html",     # 3
           "revisions.html",      # 4
           "zenithobs.html",      # 5
           "upgrades.html",       # 6
           "reduction.html",      # 7
           "calibration.html",    # 8
           "dataQ.html",          # 9
           "workshops.html"       # 10
         );

@title = ( "Online Ad Hoc Forum",          # 0 
           "Role of the Adhoc Committee",  # 1
           "Committee Web Pages",          # 2
           "Dobson Newsletter",            # 3
           "Revisions to REPORT NO. 6",    # 4
           "Discussion of Zenith Obs",     # 5
           "Instrument Upgrades",          # 6
           "Reduction of Total Ozone",     # 7
           "Dobson Calibrations",          # 8
           "Data Quality",                 # 9
           "Workshops"                     # 10
         );

if ($index eq "") { print redirect('http://www.cmdl.noaa.gov/ozwv/dobson') } 

$in = $htmldir . @files[$index];
$out = "../dobson/adhoc/temp.html";

open IN, "$in"    or die "Error 2:  open input file \'$in\' - $!.\n";
open OUT, ">$out" or die "Error 3:  open output file \'$out\' - $!.\n";

print 
  header, 
#  " name $name \n thots $thots \n index $index", 
  start_html( -title=> @title[$index], 
              -author=> 'Mark.Clark@NOAA.gov',
              -meta=> {'generator'=>'CGI.pm, Perl 5.004, and BBEdit Lite 4.6'},
              -bgcolor=> '#eeeeee'),
  "\n",
  basefont( { -size=>4 } ),
  p, 
  font( { -size=>"+1" } ), 
  center ( $line = <IN> ),             # center the first line of the file
  font( { -size=>"+0" } ) , 
  p, hr,
  "<BLOCKQUOTE>\n"; 

print OUT $line or die "Error 4:  print to $out failed - $!.\n";

while ($line = <IN>) {
  if ($line eq "<!-- Mark -->\n") { last }
  if ( $line ne "\n" )
    { print $line }
   else
    { print p, "\n" }
  print OUT $line;
}

if ( $thots && $name ) {
  $subj = @files[$index];
  $file = "/www/ozwv/dobson/adhoc/temp.mail";
  open MAIL, ">$file" or die "Error 5:  \"open $file\" - $!.\n";
  print MAIL "$subj \n$name commented: \n $thots\n";
  close MAIL;
#  system "/www/ozwv/dobson/adhoc/mail.pl >> /dev/null" or carp "System called failed - $!.\n";
  system "/www/ozwv/dobson/adhoc/mail.pl >> carp.out" or carp "System called failed - $!.\n";

  @m = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
      "Aug", "Sep", "Oct", "Nov", "Dec");

  #  0    1    2     3     4    5     6     7     8
  ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time);

  if (length($min) == 1) { $min = "0".$min }
  if (length($sec) == 1) { $sec = "0".$sec }

  print hr, p, "\n", 
    "On ", @m[$mon], " ", $mday, ", ", $year+1900,
    " at ", $hour, ":", $min, ":", $sec, "UTC&nbsp;&nbsp;&nbsp;&nbsp;",
    $name, " commented <P>\n", $thots, "<P>\n";
  print OUT hr, p, "\n",  
    "On ", @m[$mon], " ", $mday, ", ", $year+1900, 
    " at ", $hour, ":", $min, ":", $sec, "UTC&nbsp;&nbsp;&nbsp;&nbsp;",
    $name, " commented <P>\n", $thots, "<P>\n";
}
print OUT "<!-- Mark -->\n";

while ($line = <IN>) {
  print $line;
  print OUT $line;
}

rename $out, $in or die "Error 6: coudn't rename $out to $in - $!.\n";
close IN;
close OUT;

chmod 0666, $in or die "Error 7:  couldn't chmod \'$out\' - $!.\n";

param ( -name=>'thoughts', -value=>'');
param ( -name=>'name', -value=>'');

print 
  hr, p, "\n",
  start_form( -method=>POST, -action=>"revs.cgi"), 
  hidden ( -name=>'file', -value=>$index ),
  blockquote("Please enter any comments you have. <BR>
    When you are finished, click the submit button to add your thoughts
    to this page.", p, "\n",
    textarea( -name=>'thoughts', -rows=>7, -cols=>72, -wrap=>physical ),
    p, "\nEnter your name here - no anonymous comments.", p,
    textfield( -name=>'name', -size=>30), p, "\n",
    center(submit( -value=>"Submit Comment" ), "&nbsp;&nbsp; or &nbsp;&nbsp;",
    reset('Reset Form')), "\n"),
  end_form, p, hr,
  blockquote( "To view the list of topics again click \n",
    a({ -href=>'adhoc.cgi' }, "here." )),
  end_html, "\n";