first check-in of feisty meow codebase. many things broken still due to recent
[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
78   if (! -d $src) {
79     print "$src is not a directory.\n";
80     return;
81   } elsif (-l $src) {
82     return;
83   }
84   
85 #  print "recurse_dirs: source is $src and destination is $dest.\n";
86
87   opendir(SRC_DIR, $src);
88   local(@source_list) = readdir(SRC_DIR);
89   closedir(SRC_DIR);
90   opendir(DEST_DIR, $dest);
91   local(@dest_list) = readdir(DEST_DIR);
92   closedir(DEST_DIR);
93
94 ##  if (&same_file($src, $dest)) {
95 ##    # these appear to be the same directory.  we shouldn't recurse in.
96 ##print "found same dirs!  $src and $dest\n";
97 ##  }
98
99   # now actually call the differ.  this is a prefix traveral of the tree.
100   if (-l $src) {
101     return;
102   } elsif (-d $dest) {
103     # remember that the destination is actually the first parameter for
104     # diff_dirs, although the source is first for differ...
105 #    print "diffing src=$src against dest=$dest\n";
106     &diff_dirs($dest, $src);
107   } else {
108     print "$break_line\n";
109     print "Source exists at \"$src\", but target does not.\n";
110   }
111
112   # iterate through the directory.
113   local($name);
114   foreach $name (@source_list) {
115     local($compare_name) = $dest . "/" . $name;
116 #    print "name is $name and to compare is $compare_name.\n";
117     local($new_name) = $src . "/" . $name;
118     if ( (-d $new_name) && ($name ne ".") && ($name ne "..")
119         && ($name ne ".svn") ) {
120 #      print "recursing on: source $name and destination $compare_name.\n";
121       &recurse_dirs($new_name, $compare_name);
122     }
123   }
124 }
125