#  scoopcomics.pl
#
#  Creates an HTML page with the day's comics and no other crap.
#  Currently playing:
#
#     Bad Tech
#     Bizarro
#     The Boondocks
#     Calvin & Hobbes
#     Dilbert
#     Doonesbury
#     For Better or For Worse
#     Get Fuzzy
#     Jerkcity
#     Peanuts
#     Rose is Rose
#     Sherman's Lagoon
#     Sluggy Freelance
#     Sylvia (but not on Sundays)
#     Zippy  (but not on Sundays)
#
#  Mark L. Irons
#
#
#   Change log
#   ==========================================================
#
#   2001-07-30  Created script. Initially scooped comics on
#               ucomics.com site, which have simple URLs.
#               (Doonesbury, The Boondocks, Sylvia, etc.)
#
#   2001-08-14  Added obfuscated comics.com strips.
#               (Dilbert, Get Fuzzy, Peanuts, Rose is Rose)
#
#   2001-08-15  Incorporated Julian day into comics.com
#               obfuscation scheme. Added Jerkcity. Considered
#               adding User Friendly (for friends), but
#               decided against it due to its poor quality.
#
#   2001-08-17  Changed comics.com deobfuscation scheme. Added
#               Bizarro.
#
#   2001-08-22  Added more comics for friends (Sluggy
#               Freelance, Bad Tech).
#
#   2001-08-29  Added Sherman's Lagoon (for a friend, mind you.)
#
#   ==========================================================
#
#
#--------------------------------------------------------------------
use Time::Local 'timelocal_nocheck';
#--------------------------------------------------------------------
#
#  Get current date and convert it to yymmdd format

($s,$m,$h,$day,$month,$year,$wday,$y,$d) = localtime;

# save unpadded dates
$rawyear  = $year;
$rawmonth = $month;
$rawday   = $day;

$longyear = $year+1900;
$year=$year-100;
$month=$month+1;                            # Jan is 1st month, not 0th

if ($year  < 10) { $year  = "0".$year;  }   # pad year to two digits
if ($month < 10) { $month = "0".$month; }   # pad month to two digits
if ($day   < 10) { $day   = "0".$day;   }   # pad day to two digits

$shortdate =     $year.$month.$day;         # put it all together,   yymmdd
$longdate  = $longyear.$month.$day;         # put it all together, yyyymmdd

#--------------------------------------------------------------------
#
#  Special 11 year offset date for Calvin & Hobbes

$chyear = $year + 89;
$chlongyear = $longyear - 11;
$chshortdate = $chyear.$month.$day;

#--------------------------------------------------------------------
#
#  Sherman's Lagoon separates yy.mm.dd with periods

$sl_date = $year.".".$month.".".$day;

#--------------------------------------------------------------------
#
#   Special obfuscated filename calculation for comics.com strips
#   (Dilbert, Get Fuzzy, Peanuts, Rose is Rose, etc.)

#  Start by calculating Julian day
#  Algorithm from http://www.capecod/net/~pbaum/date/jdalg2.htm.

$z = $longyear;
$z-- if ($month < 3);
@running_days_in_months = (306, 337, 0, 31, 61, 92, 122, 153, 184, 214, 245, 275);
$f = $running_days_in_months[$month-1];
$today_jd = $day + $f + 365*$z + int($z/4) - int($z/100) + int($z/400) + 1721118;

#  subtract Julian day corresponding to 1 Jan 0.

$offset_jd = $today_jd - 1721059;

# save the day of the week

$cc_weekday = $wday;
if ($cc_weekday == 0) { $cc_weekday = 7; }              # Sunday is treated as 7th day, not 0th

# Yet another attempt to calculate the obfuscation number.
# First we calculate the month's "seed", which is the offset
# Julian day divided by the month. Then we count down from
# the current day to the first day of the month, adding one
# to the seed for each Wednesday we encounter. Finally, we
# divide the modified seed value by the current day of the
# week.

$cc_obnum = int( ($offset_jd - $day + 1) / $month );    # calculate monthly seed
$j = $day;                                              # counter holds day of month
$k = $wday;                                             # counter holds day of week
while ($j-- >= 1) {                                     # count down days of the month from today
  $cc_obnum++ if ($k--%7 == 3);                         # increment seed each Wednesday
  $k = 7 if ($k == 0);                                  # wrap day of week counter
}
$cc_obnum = int( $cc_obnum / $cc_weekday );             # divide seed by the day of the week

#  splice the obfuscation number into the yyyymmdd date at the
#  day-of-the-weekth position (Monday = 1, etc.)

$cc_front = substr($longdate,0,$cc_weekday);  # save front of date
$cc_tail  = substr($longdate,$cc_weekday);    # save end of date
$cc_date  = $cc_front.$cc_obnum.$cc_tail;     # insert obfuscation number in between

#--------------------------------------------------------------------
#
#   Sequential counter for Jerkcity
#
# The counter is just an offset from the current Julian day.
# Knowing jerkcity.com, the counter will eventually go out of sync.

$jc_counter = $today_jd - 2451087;

#--------------------------------------------------------------------
#
#  Dates in BadTech URLs aren't padded. Of course.

$btyear   = $rawyear - 100;
$btmonth  = $rawmonth + 1;

#--------------------------------------------------------------------
#
#  Create the day's URLs

$badtechURL       = "http://badtech.com/a/$btyear/$btmonth/$rawday.jpg";
$bizarroURL       = "http://images.ucomics.com/comics/bz/$longyear/bz$shortdate.gif";
$boondocksURL     = "http://images.ucomics.com/comics/bo/$longyear/bo$shortdate.gif";
$calvinhobbesURL  = "http://images.ucomics.com/comics/ch/$chlongyear/ch$chshortdate.gif";
$dilbertURL       = "http://www.comics.com/comics/dilbert/archive/images/dilbert$cc_date.gif";
$doonesburyURL    = "http://images.ucomics.com/comics/db/$longyear/db$shortdate.gif";
$forbetterURL     = "http://www.comics.com/comics/forbetter/archive/images/forbetter$cc_date.gif";
$getfuzzyURL      = "http://www.comics.com/comics/getfuzzy/archive/images/getfuzzy$cc_date.gif";
$jerkcityURL      = "http://www.jerkcity.com/jerkcity$jc_counter.gif";
$peanutsURL       = "http://www.comics.com/comics/peanuts/archive/images/peanuts$cc_date.gif";
$roseisroseURL    = "http://www.comics.com/comics/roseisrose/archive/images/roseisrose$cc_date.gif";
$shermansURL      = "http://www.slagoon.com/dailies/SL$sl_date.gif";
$sluggyURL        = "http://pics.sluggy.com/comics/$shortdate"."a.gif";
$sylviaURL        = "http://images.ucomics.com/comics/tmsyl/$longyear/tmsyl$shortdate.gif";
$zippyURL         = "http://www.sfgate.com/comics/graphics/zippy/$wday.gif";

#--------------------------------------------------------------------
#
#  Write a bare bones HTML page with these links

print "<HTML><HEAD><TITLE>Today's comics</TITLE></HEAD><BODY>";
print "<DIV ALIGN=CENTER><TABLE><TR><TD><FONT FACE=\"Haettenschweiler\">";
print "<P>Doonesbury<BR><IMG SRC=\"$doonesburyURL\"></P>\n";
print "<P>The Boondocks<BR><IMG SRC=\"$boondocksURL\"></P>\n";
print "<P>Get Fuzzy<BR><IMG SRC=\"$getfuzzyURL\"></P>\n";
print "<P>Rose is Rose<BR><IMG SRC=\"$roseisroseURL\"></P>\n";
print "<P>For Better or For Worse<BR><IMG SRC=\"$forbetterURL\"></P>\n";
print "<P>Bizarro<BR><IMG SRC=\"$bizarroURL\"></P>\n";
print "<P>Bad Tech<BR><IMG SRC=\"$badtechURL\"></P>\n";
print "<P>Dilbert<BR><IMG SRC=\"$dilbertURL\"></P>\n";
print "<P><IMG SRC=\"$sluggyURL\"></P>\n";
print "<P>Peanuts<BR><IMG SRC=\"$peanutsURL\"></P>\n";
print "<P>Calvin and Hobbes<BR><IMG SRC=\"$calvinhobbesURL\"></P>\n";
print "<P><IMG SRC=\"$shermansURL\"></P>\n";
if ($wday != 0) {
  print "<P>Sylvia<BR><IMG SRC=\"$sylviaURL\"></P>\n";
  print "<P><IMG SRC=\"$zippyURL\"></P>\n";
}
print "<P><IMG SRC=\"$jerkcityURL\"></P>\n";
print "</FONT></TD></TR></TABLE></DIV></BODY></HTML>\n";