#!/usr/bin/perl -w
#---------#---------#---------#---------#---------#---------#---------#---------
#
# title
#   do something with a series of files in a directory
# author
#   idc@planetlarg.net 19 july 04
# description
#   This is set up to read each HTML doc, print  file name and title,
#   and run the script file-replace-text.pl on it.
#   In general, it:
#   - Accepts the name a of a directory
#   - Makes a list of certain files.
#   - runs a script against each one (file-replace-text.pl)
#   - prints a report
#
#   Some checks are in place eg. refuse to read huge files.
#   If unknown options are encountered, explain how to use this script.
# modifications
#
#---------#---------#---------#---------#---------#---------#---------#---------
# setup
    use 5.005;
    use strict;
    use vars qw ($DEBUG);
    $DEBUG         = 0; # set to 1 (true) to print extra information to STDERR.
    my $sep = "---------+---------+---------+---------+---------+---------+---------+---------+\n";
# command line options
    # complain if 1 option was not entered
    usage() if $#ARGV != 0;
    my $dir = $ARGV[0];           # directory to examine

#---------#---------#---------#---------#---------#---------#---------#---------
#
    my $title_html;            # regex match of file contents
    my $title;                 # plain title of HTML document
    my $file          = '';    # name of current file being examined
    my $bindir        = '/opt/scripts'; # dir where these scripts are
    chdir $dir or die "Can't cd to $dir: $!\n";
    my @files  = list_files (".", "*.htm", "*.html");
# print report header
    print "HTML documents and titles\n";
    print "date: ", `date`;
    print "directory: $dir\n";
    print "total pages: ", scalar @files, "\n";
    print $sep;
# print report body
    my @args;
    foreach $file (@files) {
        @args = ("$bindir/file-read-title.pl", "$file");
        system (@args) == 0 or die "system @args failed: $?";
   }
# print report footer
    print $sep;
#
#---------#---------#---------#---------#---------#---------#---------#---------
#



# subroutine
#   list_files
# description
#  makes a list of certain types of file in a directory
#
sub list_files {
#
    my $dir    = shift;
    my @types  = @_;
    my @list   = map {glob "$dir/$_"} @types;
    return @list;
}





#
# subroutine
#   usage
# description
#   describe how this script should be used.
#   This is one way of producing a set of instructions, using simple indentation.
#   For more complex document, use mark-up. See the perldoc section below.
#
sub usage {

   use Text::Wrap;
   $Text::Wrap::columns = 72;
   my $pre1             = "   ";
   my $pre2             = "   ";
   my $name             = "$0 - print a list of HTML files and their titles in a directory";
   my $synopsis         = "$0  /DIRECTORY";
   my $description      = "
Accept the name a of a directory.
Check each HTML document.
Print the file name and title of each document.
";
   my $examples         = "
bomb.htm: How to Build an Atomic Bomb
pea.htm: How to Build a Pea Shooter
";

   print
               "NAME \n",
               wrap ($pre1, $pre2, $name), "\n",
               "SYNOPSIS \n",
               wrap ($pre1, $pre2, $synopsis), "\n",
               "DESCRIPTION \n",
               fill ($pre1, $pre2, $description), "\n",
               "EXAMPLES \n",
               wrap ($pre1, $pre2, $examples), "\n",
   ;
   exit 1;
}
