#! /usr/bin/perl -s
################################################################################
$VERSION = "slideshow.pl v1.0"; # 26st Feb '96 Dale Bewley <dale@bewley.net>
# slideshow v.97 18 December 1995       
# slideshow v.96 20 November 1995 
#-------------------------------------------------------------------------------
# This script and others found at http://www.bewley.net/perl/
#
#  Description
#  	Creates a client pull slide show from a series of html documents.
#
#  Usage
#	slideshow [switches] *.html
#	see http://www.bewley.net/perl/slideshow.html
#
#  Notes
#	o Now does frames!
#	o Now with tons of command line options. Are they too long? 
#	o HTML documents must pre-exist and must contain <HEAD> and <BODY> tags.
#	o Output files have the same file name prepended with "S-"
#	o If you have a directory full of graphics you would like to put
# 	  in a slideshow pick up my gif2html.pl	(soon to be included here)
#
#  Todo
#	o Shorten length of command line options.
#	o Output to same name file, not to S-file.html.
#	o Add the funtionality of gif2html.
#	o Make it CGIable?
#
#
# Copyright (C) 1996 Dale Bewley <dale@bewley.net>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
################################################################################


#- User configurable variables ------------------------------------------------#
$DELAY	= 60 unless $DELAY;
 	# Delay in seconds between page refreshes.
$BASEREF = "./" unless $BASEREF;
	# Where are the html pages located? 
$FINAL_PAGE = "http://musicalpeace.org/vyoma/Photos/picturegallery/mushrooms/" unless $FINAL_PAGE;	
	# Set this to the page to load after all are shown.
$FRAMESET = "slideshow.html" unless $FRAMESET;
	# Name of file to write frameset to.
$INDEX_FRAME= "index-frame.html" unless $INDEX_FRAME;
	# Name of file in index frame which lists picture names.
$INDEX_WIDTH = "10%" unless $INDEX_WIDTH;
	# Width of frame which lists files.
$ORIENTATION = "ROWS" unless $ORIENTATION;
	# Display frames side by side or stacked. ROWS or COLS.
$PICTURE_FRAME= "picture-frame" unless $PICTURE_FRAME;
	# Name of frame which pictures display in.
$PICTURE_WIDTH = "90%" unless $PICTURE_WIDTH;
	# Width of frame which pictures display in.
$STOP_PAGE = $BASEREF unless $STOP_PAGE;
	# Set this to the page to load on slideshow abort.
#------------------------------------------------------------------------------#

if ($h || !$ARGV[0]) { &HELP }


#- Main Program ---------------------------------------------------------------#
$current = 0;
if ($f) {
	&writeFRAMESET;
	open (indexFRAME, ">>$INDEX_FRAME") || 
		die "Can't open $INDEX_FRAME for appending $!. Stopped";
}

while ($ARGV[$current]) {
	# Open read/write html files.
	open (inDOC,"<$ARGV[$current]") || die 
		"Can't open $ARGV[$current] for input $!. Stopped";
	print (STDERR "$ARGV[$current]\tto-> ") if $v;
	open (outDOC,">S-$ARGV[$current]") || die 
		"Can't open S-$ARGV[$current] for output $!. Stopped";
	print (STDERR "S-$ARGV[$current]\n") if $v;

	while (<inDOC>) {
		# Find end of HEAD.
		m/(.*)(\<\/head\>)(.*)/i && (&HEAD($1,$3));
		# Print rest of document.  
		print (outDOC $_); 
	}
	close (inDOC);
	# old document is processed now add nav bar and continue

	# print navigation bar, this part needs a touch up
	print (outDOC "<CENTER><EM>");
	print (outDOC " <A HREF=\"${BASEREF}S-$ARGV[$current-1]\">") if $ARGV[$current-1];
	print (outDOC "Previous</A> | \n") if $ARGV[$current-1];
	print (outDOC " <A HREF=$STOP_PAGE TARGET=_top>Stop Slideshow</A> ");
	if ($ARGV[$current+1]) {
		print (outDOC "|\n <A HREF=\"${BASEREF}S-$ARGV[$current+1]\">");
		print (outDOC "Next</A>");
	}
	print (outDOC "</EM>\n</CENTER>\n");
	close (outDOC);

	#if (someSwitch) {
		# make this a command line option
		#unlink ($ARGV[$current]) || warn "Can't remove $ARGV[$current]";
		#print (STDERR " Removing $ARGV[$current]\n") if $v;
		#rename ("S-$ARGV[$current]", "$ARGV[$current]") || warn "Can't rename S-$ARGV[$current] $!";
		# More Verbose stuff
		#if ($v) {
			#print (STDERR "Renaming: S-$ARGV[$current-1] -> ");
			#print (STDERR "$ARGV[$current-1]\n"); 
		#}
	#}

	if ($f) {
		# Add HREF to the index-frame
		print (indexFRAME "<CENTER><DIV ALIGN=center>\n") 
			if ($current==0);
		print (indexFRAME "<A HREF=\"S-$ARGV[$current]\"");
		print (indexFRAME "TARGET=\"$PICTURE_FRAME\">$ARGV[$current]</A>");
		print (indexFRAME "<BR>\n") if ($ORIENTATION =~ /COLS/i);
		print (indexFRAME " | \n") if (($ORIENTATION =~ /ROWS/i)  
			&& (-e $ARGV[$current+1]));
		print (STDERR "Added $ARGV[$current] to the $INDEX_FRAME\n") if $v;
		print (indexFRAME "\n</CENTER></DIV>\n") if ($current==$#ARGV);
	}

	# go to next html file in $ARGV
	$current++;
} # End while
#------------------------------------------------------------------------------#

#- Write Frameset -------------------------------------------------------------#
sub writeFRAMESET {
	open (FRAMESET, ">>$FRAMESET") || 
		die "Can't open $FRAMESET for output $!. Stopped";
	$oldFH = (select(FRAMESET));

	if ($ORIENTATION =~ /COLS/i) {
		print "<FRAMESET $ORIENTATION=$INDEX_WIDTH,$PICTURE_WIDTH>\n";
		print "\t<FRAME NAME=$INDEX_FRAME SRC=$INDEX_FRAME>\n";
		print "\t<FRAME NAME=$PICTURE_FRAME SRC=S-$ARGV[0]>\n";
		print "</FRAMESET>\n";
	} elsif ($ORIENTATION =~ /ROWS/i) {
		print "<FRAMESET $ORIENTATION=$PICTURE_WIDTH,$INDEX_WIDTH>\n";
		print "\t<FRAME NAME=$PICTURE_FRAME SRC=S-$ARGV[0]>\n";
		print "\t<FRAME NAME=$INDEX_FRAME SRC=$INDEX_FRAME>\n";
		print "</FRAMESET>\n";
	} else {
		die "ORIENTATION must be either COLS or ROWS.\n";
	}

	print "\n<NOFRAMES>\n";
	# pass correct filehandle to print plug to
	&shamelessPLUG(*FRAMESET);
	print "\n<META HTTP-EQUIV=\"Refresh\" ";
	print "CONTENT=\"$DELAY\;";
	print " URL=${BASEREF}"."S-"."$ARGV[$current]\">\n</NOFRAMES>";
	select($oldFH);
	close FRAMESET;
} # end sub writeFRAMESET
#------------------------------------------------------------------------------#

#- Print head with meta refresh info ------------------------------------------#
sub HEAD {
	# we found </head> 
	($preHEAD, $postHEAD) = @_;
	# print portion of record before </head>
	print (outDOC $preHEAD);

	# Wipe out $_ for printing after return from sub HEAD
	$_ =~ s/.*//;

	# Print HEAD of HTML file containing META info
	#  checking if this is the last html in slideshow.
  	if ($current != $#ARGV) {
		# There are more html files to process.
		print (outDOC "\n<META HTTP-EQUIV=\"Refresh\" ");
		print (outDOC "CONTENT=\"$DELAY\;");
		print (outDOC " URL=${BASEREF}"."S-"."$ARGV[$current+1]\">");
	} else {
		# This is the last html file to write.
		print (outDOC "\n<META HTTP-EQUIV=\"Refresh\" ");
		print (outDOC "CONTENT=\"$DELAY\;");
		print (outDOC " URL=$FINAL_PAGE TARGET=_top\">");
	}

	# Print shameless plug. and finish up the head
	&shamelessPLUG(*outDOC);
	# print new </head> and portion of record after old </head> 
	print (outDOC "</HEAD>$postHEAD\n");
} # end sub head
#------------------------------------------------------------------------------#

#- Help -----------------------------------------------------------------------#
sub HELP {
	print <<"END_OF_HELP";
$VERSION
See: http://www.bewley.net/perl/slideshow.html
Usage: $0 [switches] <file spec>
 File spec is html files. Example:
 slideshow -v *.html

Switches:
  -f		  	Frames mode. Create $FRAMESET and $INDEX_FRAME.
  -h		  	Help!
  -v		  	Verbose progress printouts.
  -BASEREF=URL    	Base URL for each slide. [$BASEREF]
  -DELAY=#		Delay between slides in seconds. [$DELAY]
  -FINAL_PAGE=URL	URL to load when slideshow is complete. 
			[$FINAL_PAGE]
  -FRAMESET=filename	Name of file to write frameset to. [$FRAMESET]
  -INDEX_FRAME=filename Name of file to write index frame to. [$INDEX_FRAME]
  -INDEX_WIDTH=#	Width of frame which lists files. [$INDEX_WIDTH]
  -ORIENTATION=ROWS	Display frames in ROWS or COLS. [$ORIENTATION]
  -PICTURE_FRAME=string Name of frame which pictures display in. 
			[$PICTURE_FRAME]
  -PICTURE_WIDTH=#	Width of frame which pictures display in. [$PICTURE_WIDTH]
  -STOP_PAGE=URL	URL to load on slideshow abort. [same as \$BASEREF]
END_OF_HELP
	exit;
}
#------------------------------------------------------------------------------#

#- Print Shameless Plug -------------------------------------------------------#
sub shamelessPLUG {
	local(*FILEHANDLE) = @_;
	print FILEHANDLE "\n<!-- This file created by $VERSION by ";
	print FILEHANDLE "Dale Bewley dale\@bewley.net\n"; 
	print FILEHANDLE "\thttp://www.bewley.net/perl/ -->\n\n"; 
}
#------------------------------------------------------------------------------#



