#!/usr/bin/perl

# journal.cgi - redirect script for journal entries.
#
$version = "1.5.1, 2006-09-20";
#
# checks to see if entry is on front page; otherwise, routes to appropriate
# monthly archive page.  very simple for vers1.  no error checking to
# see if entry exists, only to make sure it's not future date or <2000.
#
# 1.0.1: Fixed "sep" to "sept" for month names
# 1.1:   Added support for multi posts per day (letter suffix after date)
# 1.3:   No longer checks for (broken) home.html current post any more --
#      and bounces post-April 2004 to MType archives
# 1.3.1: Updated for Dchaos, also tyr* files refound/rebuilt
# 1.3.2: Hopefully working again?  finally?
# 1.4:   Working again! And also uses new TyrFuncs addFooter for errors.
# 1.5:   Now also $styleSheets for bonus non-whiteness; also, redirects
#      to journal.html if called with no arguments; also, autocorrects 
#      user input if the "date=" is forgotten or YYYYMMDD by accident
# 1.5.1: Per user notification, date checking now allows 04342004 ;-)

require "/home/baxil/public_html/cgisrc/tyrforms.pl";  #also gets tyrfuncs

$journdir = "";  # subdirectory, if any, that journal archive stored in
$indexPage = "journal.html"; # where to go if called blank
$currentjournal = "index.html";  #location of current journal entry

$jokearg = "javascript"; #set to something (anything) to access alt files

# ---- subroutines -- main script down below --------------

# #############
sub badArgs {
  # bad argument or missing argument passed to url.
  if (@_) {
    $printme = join(" <p>\n", @_);
  } else {
    $printme = "None given.";
  }
  # and a few special cases:
  if ($printme eq "noargs" or $printme eq "badarg") {
    $printme = q~
  This script, to perform its functions properly, requires the date of the 
journal entry it's meant to redirect to.  The date needs to be supplied in 
the format MMDDYYYY, for example 01032002 (January 3, 2002).  The URL 
should end like this:  <b>journal.cgi?date=01032002</b>. ~;
  } elsif ($printme eq "baddate") {
    $printme = q~
  Ha, ha.  Very funny.  When I said the script needed a date in the format
MMDDYYYY, I meant a date <b>in the past</b> and on which a journal entry
might conceivably have been written.  I appreciate your attempt at humor, 
though. ~;
  } elsif ($printme eq "impolite") {
    $printme = "Please ask more nicely.  I feel unloved.";
  }

  htmlHeader(500, "Unexpected input");
  print <<EOF;
<html><head><title>Bad script!  Bad!</title>
$styleSheets
</head>
<body> <h2> Sorry </h2>
  I was unable to process this script request due to some sort of problem 
with the server or with the way the request was phrased.  Additional 
information follows:
 <p>
<tt> $printme </tt>
 <p>
  If you reached this error message by clicking on a link, please notify 
the webmaster who provided the faulty link.  (If the link was on 
Tomorrowlands, <A HREF="/contact.html">let me know</A>.)  Otherwise, you 
may have better luck finding the entry in question by manually searching 
through the <A HREF="/journal.html">journal archives</A>.

EOF
  addFooter();
  exit();
}

# #############
# returns three scalars: month, day, year
sub getDate($) {
  my ($datestring) = @_;
  my ($month, $day, $year);
  $month = substr($datestring, 0, 2);
# easy way to check if YYYYMMDD has been tried:
  if ($month > 12) {
    $year = substr($datestring, 0, 4);
    # if ($year < 1900 or $year > 2099) { badArgs("badarg"); #redundant
    $month = substr($datestring, 4, 2);
    $day = substr($datestring, 6, 2);
  } else {
    $day = substr($datestring, 2, 2);
    $year = substr($datestring, 4, 4);
  }
  $thisyear = (localtime)[5] + 1900;
  if ($month < 1 or $month > 12) { badArgs("badarg"); }
  if ($day < 1 or $day > 31) { 
    badArgs("badarg") unless ($year == 2004 and $month == 4 and $day == 34); 
  }
  if ($year < 1977 or $year > 2200) { badArgs("badarg"); }
  # we'll hardcode date of first journal entry as june 1, 2000
  if ($year < 2000 or $year > $thisyear) { badArgs("baddate"); }
  if ($year == 2000 && $month < 6) { badArgs("baddate"); }
  if ($year == $thisyear) {
    if ($month > ( (localtime)[4] + 1 )) { badArgs("baddate"); }
    if ($month == ( (localtime)[4] + 1 )) { 
      if ($day > ( (localtime)[3] )) { badArgs("baddate"); }
    }
  }
  return ($month, $day, $year);
}

# -------------------------- main script body ------------------------

%myargs = parseForm();

# DEBUG
# htmlHeader();
# @foo = %myargs;
# @fook = keys(%myargs);
# $fookle = keys(%myargs);
# $fookletwo = scalar %myargs;
# print "<html><body> \%myargs <p> @foo <p>";
# print "keys <p> @fook <p> numkeys <p> $fookle";
# print "<p> fookletwo <p> $fookletwo";
# print "<p> allArgs <p> $allArgs";
# exit;

# First things first.  If called blank, pop out to journal index:
hardRedirect("/" . $journdir . $indexPage) unless (keys(%myargs));

# Next things next, let's see if they forgot the date= :
$allArgs = join('', keys(%myargs));
if ( ! exists($myargs{"date"}) and
     keys(%myargs) == 1 ) {
  $myargs{"date"} = $allArgs;
}

# hack to allow multi-posts-same-day
if ( (exists $myargs{"date"}) &&
     (defined $myargs{"date"}) &&
     (length($myargs{"date"}) == 9) ) {
  $elSuffix = chop $myargs{"date"};  
} else {
  $elSuffix = "";
}

if ( ! exists($myargs{"date"}) or 
     ! defined($myargs{"date"}) or
     length($myargs{"date"}) != 8 ) {
  badArgs("noargs");
} else {
  ($m, $d, $y) = getDate($myargs{"date"});
}

# check to see if we're in new MT territory:
# FOR NOW, HARDCODED W/ DAY ARCHIVE HERE
  if ($y > 2004 or 
      $y == 2004 && $m > 4) { 
    if (-e ("/home/web/baxil/journal/${y}_${m}_$d.html")) {
      redirect("/journal/${y}_${m}_$d.html");
    } else {

#      redirect("/journal/${y}_$m.html");
# HAVE TO REDIRECT TO INDEX - NEWER STUFF NOT BACK ON SITE YET
       redirect("/index.html");
    }
  }

# OTHERWISE - GO TO OLD PAGES

  # find which page to redirect to.  
  $mname = ("jan","feb","mar","apr","may","june","july","aug","sept","oct","nov","dec")[$m - 1];
  if ($d > 15 or ($d == 15 and $m == 2)) {
    $isB = (-e ($rootdir . $journdir . $mname . $y . "b" . ".html")) ? "b" : "";
  } else { 
    $isB = "";
  }

  # and a special little hack for my dissociated nov2001 journal:
  $nAlt = "";
  if ( (exists $myargs{$jokearg}) and (defined $myargs{$jokearg}) ) {
    if ($mname eq "nov" and $isB eq "" and $y == 2001) {
      $nAlt = "alt";
    }
  }

  $daPage = "/" . $journdir . $nAlt . $mname . $y . $isB . ".html#" . $m . $d . $y . $elSuffix;
  hardRedirect($daPage);

exit;
