#!/usr/bin/perl -w
#---------#---------#---------#---------#---------#---------#---------#---------
#
# Date:         19 july 04
# Title:        directory subroutines
# Creator:      idc@planetlarg.net
# Description:
#   various  subroutines about directories
# Modifications:
#
#---------#---------#---------#---------#---------#---------#---------#---------
#
#
# list certain files in directories
#
    @list = list_files ("/export/home/larg", "*.txt", "*.zip");
    print map {print "file: $_\n"} @list;
#


#
# compare two directories
#
# action to carry out
   my $command = '/usr/bin/ls -1';
# directories to compare
   my $dir01    = '/data/netscape/ds-slave/slapd-lds01';
   my $dir02    = '/data/netscape/ds-slave/slapd-lds02';
# gather info on the first directory
   my @files01 = &gather( $dir01, $command, $obj );
# gather info on the second directory
   my @files02 = &gather( $dir02, $command, $obj );
# write the info to file
   &list_to_file( "files-${host01}.txt", @files01 );
   &list_to_file( "files-${host02}.txt", @files02 );
# compare the files
   my @diffs = `diff files-${host01}.txt files-${host02}.txt`;
   my $count = @diffs;
# print a report
   print "\nDifferences in running the command $command\n";
   print "'<' refers to the directory $dir01 on host $host01 \n";
   print "'>' refers to the directory $dir02 on host $host02 \n";
   print "------------------------------------------- \n";
   if ($count == 0 ) {
      print "No difference! \n";
   } else {
      print @diffs;
   }
   print "------------------------------------------- \n";



# recursively build a directory path
   if ( -d $path ) {
      print "\nThe path $opt_path already exists. \n\n";
      exit;
   }
# get busy
   use File::Basename;
   my @created = &make_path( $path );
# print report
   print " \n"; # missing from some sub error prints below.
   if ( scalar @created == 0 ) {
      print "no directories were created. \n";
   } else {
      print "created these directories: \n";
      foreach (@created) {print "   $_ \n";};
   }
   print " \n"; # for super neatness




# create an HTML page containing a a directory file list
# variables
    dir=/var/www/www.isp01.com/pubfactsheets
    out=$dir/index.html
# main
    # header
    echo "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>dir links</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
" > $out
    # body
    for i in `ls $dir`
    do
        echo "<a href=$i> $i </a> <br>" > $out
    done

    # footer
    echo "
</body>
</html>
" > $out
#
# clean up
   chown user01:user01 $out
   chmod 644 $out


#---------#---------#---------#---------#---------#---------#---------#---------
# subroutine: gather
# gather information about a directory
# this info is checksums, file sizes and file names
#
sub gather {

   my ($dir01, $command, $obj) = @_;

   $obj->cmd("cd $dir01");
   my @list = $obj->cmd(" $command "); # only works for root user

   return @list;
}

# subroutine
#   latest_modified
# description
#  find the, er, latest file in a directory and return its name.
#  Latest means the one that was most recently modified.
#  Every file is examined.
#  readdir returns ONLY the file basename, not the path.
#
sub latest_modified {

   my $dir = shift;           # directory to examine
   opendir (DIR, $dir) or die "can't open directory $dir: $!";

   my $latest_mtime  = 0;     # most recent last modified time
   my $latest_file   = '';    # name of file with $latest_mtime
   my $file          = '';    # name of current file being examined
   while (defined ($file   = readdir (DIR))) {
      next if $file =~ m/^\.\.?$/; # skip . (this) and .. (parent) entries
      my (
                  $dev, $ino, $mode, $nlink, $uid, $gid, $rdev,
                  $size, $atime, $mtime, $ctime, $blksize, $blocks
      ) = stat "$dir/$file";
      if ($latest_mtime < $mtime) {
         $latest_mtime  = $mtime;
         $latest_file   = $file;
      }
      print STDERR "   \$file    \t$file  \n" if $DEBUG;
      print STDERR "   \$mtime   \t$mtime \n" if $DEBUG;
   }
   return $latest_file;
}



# subroutine
#   make_path
# description
#  take a list of directories that need to be created
#  make them
#
sub make_path {
#
   my ($path) = @_; # a path string eg. dir01/dir02/dir03
   my @created; # list of directories that are made by this sub
   my $mode = 0777; # the first character MUST be the number mode
   # see "perldoc -f chmod"
# if the parent directory does not exist,
# recurse this sub with the parent dir
   my $parent = File::Basename::dirname($path);
   push (@created, &make_path($parent)) unless (-d $parent);
# create the directory
   if ( mkdir("$path", $mode) ) {
      push(@created, $path);
   } else {
      my $e = $!;
      print STDERR "cannot create $path: $e \n";
   }
   @created;
}




# 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;
}


#---------#---------#---------#---------#---------#---------#---------#---------
