#!/usr/bin/perl -w
use strict;

=README

   This code reads in a number from a file (let's call it "current.txt")
   and then takes that number and copies an html document (let's call it
   "the-number-from-the-file.html") to "page.html".

   Directly after this operation, the number is incremented and re-saved to
   "current.txt".  This goes on until the number 5 is reached, and then the
   number in "current.txt" would be reset to 1.

      Tommy Butler - 2/14/03, for "Andrew" (Happy St. Valentine's Day!)

   This program is free software, you may redistribute and/or modify it under
   the terms of the GNU GPL. <URL: http://www.gnu.org/licenses/gpl.txt>

=cut

use vars qw/ $COUNTER_FILE $TARGET_FILE $ROTATE_DIR $CUTOFF /;

# system path + filename of the counter file
$COUNTER_FILE = './current.txt';

# system path + filename for the file which will be replaced when a new file
# is selected from the rotation
$TARGET_FILE  = './page.html';

# directory where all pages in the rotation are located.
$ROTATE_DIR = '../any/old/directory/where/you/keep/those/numbered/html/files';

# stop the rotation and start over at the beginning when this number is reached
# in the rotation.  If, for example, you want to stop at 10.html and start back
# over, set this variable to a value of ten (10)
$CUTOFF = 10;

# -------/ No more editing is necessary now. / ----------------------------

# standard distribution Perl extension library for use with file copying ops
use File::Copy;

# handle CGI-based execution environment protocol if necessary
print qq[Content-Type: text/html; charset=ISO-8859-1\n\n]
   if $ENV{'REQUEST_METHOD'};

# get rotation position from counter file, advance position.  create counter
# file if necessary, initializing the rotation at 1.
open(POS, '>' . $COUNTER_FILE)
   or die qq[Can't create "$COUNTER_FILE", "$!"]
      unless -e $COUNTER_FILE;

open(POS,$COUNTER_FILE) or die qq[Can't open "$COUNTER_FILE", "$!"];
my($pos) = ((<POS>)[0] || 0); close(POS);
$pos += 1; $pos = 1 if $pos == $CUTOFF + 1;

# update counter file
open(POS,'>' . $COUNTER_FILE) or die qq[Can't open "$COUNTER_FILE", "$!"];
print(POS $pos) and close(POS);

# replace or create the target file with the file selected from the rotation
copy(qq[$ROTATE_DIR/$pos.html], $TARGET_FILE)
   or die qq[Can't copy "$ROTATE_DIR/$pos.html" to "$TARGET_FILE", "$!"];

# print out a report of what was done.
print <<__REPORT__;
Copied "$ROTATE_DIR/$pos.html" to "$TARGET_FILE"
__REPORT__

exit;