#!perl #
#
# Perl script 'compdel.pl' to compare files in the source directory with
# the files in the working directory and to delete the identical files
# from the working directory
#
# Version: 5.40
# Date: 2000, January 3
#
# Coded by: Ludek Klimes
#     Department of Geophysics, Charles University Prague,
#     Ke Karlovu 3, 121 16 Praha 2, Czech Republic,
#     E-mail: klimes@seis.karlov.mff.cuni.cz
#
# ......................................................................
#
# Usage:
#     perl compdel.pl path1/ path2/
# where
#     path1/ is the path to the source directory terminated by a slash,
#     path2/ is the path to the working directory terminated by a slash.
#     The paths should be typed in the Unix form.
# The files in the working directory identical to the corresponding
# files of the source directory will be deleted.
#
# ======================================================================
  if (scalar(@ARGV)<1) {
    die "Arguments (paths) missing. Error";
  }
  opendir(LU,$ARGV[0]) || die "Directory '$ARGV[0]' not found. Error";
  @DIR1=readdir(LU);
  closedir(LU);
  opendir(LU,$ARGV[1]) || die "Directory '$ARGV[1]' not found. Error";
  @DIR2=readdir(LU);
  closedir(LU);
#
  foreach $NAME1 (@DIR1) {
    $PATH1=$ARGV[0].$NAME1;
    unless ($NAME1 eq '.') {
      unless ($NAME1 eq '..') {
        unless (-d $PATH1) {
          foreach $NAME2 (@DIR2) {
            $PATH2=$ARGV[1].$NAME2;
            if ($NAME1 eq $NAME2) {
              open(LU,$PATH1) || die "Cannot open '$PATH1'. Error";
              @FILE1=;
              close(LU) || die "Error";
              open(LU,$PATH2) || die "Cannot open '$PATH2'. Error";
              @FILE2=;
              close(LU) || die "Error";
#             Check for the number of lines:
              if (scalar(@FILE1)==scalar(@FILE2)) {
#               Check for the contents:
                foreach $LINE1 (@FILE1) {
                  $LINE2=shift(@FILE2);
                  if ($LINE1 ne $LINE2) {
                    print "$NAME1: different lines\n";
                    goto EndCheck;
                  }
                }
                print "$NAME1: identical - deleted\n";
#               *** Very dangerous command ***
                unlink($PATH2);
#               ******************************
                EndCheck:
              } else {
                print "$NAME1: different number of lines\n";
              }
            }
          }
        }
      }
    }
  }
# ======================================================================
1;                                                               #