#!/usr/local/bin/perl
# rotator.pl
# Author:  Ian Graham
#          Information Commons, University of Toronto
#          <ian.graham@utoronto.ca>
# Version: 1.1.    Date:    April 13 2000

print "Content-type: text/html\r\n\r\n"; # content-type header

$include_path="/svc/www/InsTest";    # Directory containing files
                                     # Second Block: Get listing for
if( !opendir(DIR, $include_path)) {  # directory containing inserts
  &f_error("Unable to open notices directory\n", __LINE__, __FILE__); }
@tmp = readdir(DIR);          # Read list of filenames; then check
foreach (@tmp) {              # to see they are files, and not dirs
                              # (-T tests for "real" files
  push(@filenames, $include_path."/".$_) if -T $include_path."/".$_; }
close(DIR);
$last_index = $#filenames;    # Get index of last entry 
$last_index += 1;             # in array of filenames

if($last_index < 0) { 
  print " no files to insert ....\n"; die; # no stuff -- do nuthin'
}
else {                        # If there are files to be 
                              # inserted, select one at random
  srand(time);                # and print it to stdout.
  $rand_index = int(rand($last_index));
  open(TEMP, $filenames[$rand_index]) ||   # Open selected file --
     &f_error("Unable to open insertion file.\n", __LINE__,__FILE__);
  @insertion = <TEMP>; close(TEMP);
  print @insertion;           # Print contents to output
}      
# ----------- FINISHED ---------  FINISHED ------------
# Error Handling Subroutine

sub f_error {                 # What to do if there is an error 
  print "Content-type text/html\n\n";
  print "Fatal error  at line $_[1] in file $_[2].\n";
  print "Please send mail to: <br />\n";
  print "<a href=\"mailto:web\@comm.ut.ca\">webm\@comm.ut.ca</a><br />\n";
  print "to inform us of this error. Please, quote the URL\n";
  print "of the page that gave this error.\n<hr />\n";
  die "Fatal Error: $_[0] at line $_[1] in file $_[2] \n";
}

