more recent changes than were available on zooty at the time of initial checkin.
[feisty_meow.git] / scripts / text / differ.pl
1 #!/usr/bin/perl
2
3 ###############################################################################
4 #                                                                             #
5 #  Name   : differ (with recursion support)                                   #
6 #  Author : Chris Koeritz                                                     #
7 #  Rights : Copyright (C) 1996-$now by Author                                 #
8 #                                                                             #
9 #  Purpose:                                                                   #
10 #                                                                             #
11 #    Compares files in a directory hierarchy to files in another hierarchy.   #
12 #  Any files with the same name will be compared and files that exist in one  #
13 #  but not the other are reported.                                            #
14 #                                                                             #
15 ###############################################################################
16 #  This program is free software; you can redistribute it and/or modify it    #
17 #  under the terms of the GNU General Public License as published by the Free #
18 #  Software Foundation; either version 2 of the License or (at your option)   #
19 #  any later version.  See: "http://www.gruntose.com/Info/GNU/GPL.html" for a #
20 #  version of the License.  Please send any updates to "fred@gruntose.com".   #
21 ###############################################################################
22
23 require "diff_lib.pl";
24 require "filename_helper.pl";
25
26 &install_interrupt_catcher;
27
28 # check that we received at least one parameter.
29 if ($#ARGV < 0) {
30   &print_instructions;
31   exit 1;
32 }
33
34 # get the two directories from the command line.
35 local($destination) = $ARGV[0];
36 local($source) = ".";
37 if ($#ARGV > 0) {
38   # get the location they provided.
39   $source = $ARGV[1];
40 }
41
42 # make the names a little more manipulable.
43 $source = &sanitize_name($source);
44 # print "source is now: $source\n";
45 $destination = &sanitize_name($destination);
46 # print "dest is now: $destination\n";
47
48 # call the routine that does all the work.
49 &recurse_dirs($source, $destination);
50
51 exit 0;
52
53 ############################################################################
54
55 sub print_instructions
56 {
57   print "
58 differ:\n
59 This program requires at least one directory name as a parameter.  This is
60 the location to use as the target directory.  The second parameter, if
61 provided, is the location to use as the source directory.  The recursive
62 differ script will traverse the source hierarchy and attempt to compare
63 the contents of the directories in that hierarchy against the destination
64 hierarchy.  This will fail where the hierarchies have different directory
65 structures.  But where the directories exist in both hierarchies, their
66 contents are compared against each other and documented in the output.
67 ";
68 }
69
70 ############################################################################
71
72 sub recurse_dirs
73 {
74   local($src, $dest) = @_;
75
76   if ($src =~ /\/CVS$/) { return; }  # skip comparing repositories.
77 #hmmm: why is only CVS spelled out here?  what about the others?
78
79   if (! -d $src) {
80     print "$src is not a directory.\n";
81     return;
82   } elsif (-l $src) {
83     return;
84   }
85   
86 #  print "recurse_dirs: source is $src and destination is $dest.\n";
87
88   opendir(SRC_DIR, $src);
89   local(@source_list) = readdir(SRC_DIR);
90   closedir(SRC_DIR);
91   opendir(DEST_DIR, $dest);
92   local(@dest_list) = readdir(DEST_DIR);
93   closedir(DEST_DIR);
94
95 ##  if (&same_file($src, $dest)) {
96 ##    # these appear to be the same directory.  we shouldn't recurse in.
97 ##print "found same dirs!  $src and $dest\n";
98 ##  }
99
100   # now actually call the differ.  this is a prefix traveral of the tree.
101   if (-l $src) {
102     return;
103   } elsif (-d $dest) {
104     # remember that the destination is actually the first parameter for
105     # diff_dirs, although the source is first for differ...
106 #    print "diffing src=$src against dest=$dest\n";
107     &diff_dirs($dest, $src);
108   } else {
109     print "$break_line\n";
110     print "Source exists at \"$src\", but target does not.\n";
111   }
112
113   # iterate through the directory.
114   local($name);
115   foreach $name (@source_list) {
116     local($compare_name) = $dest . "/" . $name;
117 #    print "name is $name and to compare is $compare_name.\n";
118     local($new_name) = $src . "/" . $name;
119     if ( (-d $new_name) && ($name ne ".") && ($name ne "..")
120         && ($name ne ".svn") 
121         && ($name ne ".git") 
122         && ($name ne "CVS") ) {
123 #      print "recursing on: source $name and destination $compare_name.\n";
124       &recurse_dirs($new_name, $compare_name);
125     }
126   }
127 }
128