#!/usr/bin/perl -w
#---------#---------#---------#---------#---------#---------#---------#---------
#
# Date:         11 Sep 2000
# Title:        list the differences between two directories on two hosts.
# Author:       idc@planetlarg.net
# Description:
#   Use telnet to gather directory information from two different hosts.
#   Compare the directories and report differences.
#   If anything goes wrong, an error is printed.
# Modifications:
#
#---------#---------#---------#---------#---------#---------#---------#---------

# what's a script to do?

   # 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';

   # login details
   my $host01     = "10.2.4.34"; # ldap slave 01
   my $username01 = "isp01";
   my $password01 = "passw0rd";

   my $host02     = "10.2.4.59"; # ldap slave 02
   my $username02 = "isp01";
   my $password02 = "passw0rd";

# environment setup
   use Net::Telnet ();
   my $obj = new Net::Telnet (
      Timeout  => 15
   );
   $obj->prompt( '/.*[\$%#>] $/' );
   # debug
   my $fhi            = $obj->input_log("input.log");
   my $fhd            = $obj->dump_log("dump.log");

# gather info on the first directory
   print STDERR "logging into $host01 ... \n";
   $obj->open("$host01");

   # login
   &isp01_login( $username01, $password01, $obj);

   # gather directory info
   my @files01 = &gather( $dir01, $command, $obj );

   # finished with first host
   $obj->close;

# gather info on the second directory
   print STDERR "logging into $host02 ... \n";
   $obj->open("$host02");

   # login
   &isp01_login( $username02, $password02, $obj);

   # gather directory info
   my @files02 = &gather( $dir02, $command, $obj );

   # finished with the second host
   $obj->close;

# 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 "\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";

# clean up
#   unlink "files-${host01}.txt";
#   unlink "files-${host02}.txt";

#---------#---------#---------#---------#---------#---------#---------#---------
# subroutine: isp01_login
# supply authorisation info to a isp01 host
# isp01 hosts ask for username, password AND agreement to a contract
# if needed, frig the prompt to cater for this three part login
#
sub isp01_login {

   my ($username, $password, $obj) = @_;

   # This is a bit like saying  $prompt = '/[\$%#>] $/'
#   my $default_prompt = $obj->prompt;
# if a isp01 host, add this to the login
#      Prompt    => "/Are you authorised.*/"

   $obj->login(
      Name      => $username,
      Password  => $password
   );

   # agree to the qyestion
   # reset the prompt regexp first so the returning prompt will be spotted
#   $obj->prompt( $default_prompt );
#   $obj->cmd("y");
}

#---------#---------#---------#---------#---------#---------#---------#---------
# 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: list_to_file
# dump a list into a file
#
sub list_to_file {

    my ($file, @list) = @_;

    open (OUT, ">$file") or die "Can't store the file list: $!";
    print OUT @list;
    close OUT;
}
