first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / scripts / files / whack_dupes.sh
1 #!/bin/bash
2
3 # whacks the files in the current directory which are duplicates of the
4 # files in the directory passed as a parameter.
5 # if there is a second parameter, then it is used as the "current directory".
6
7 exemplar_dir="$1"; shift
8 whack_dir="$1"; shift
9
10 # make sure they gave us a good directory to start with.
11 if [ -z "$exemplar_dir" ]; then
12   echo "whack_dupes"
13   echo "-----------"
14   echo ""
15   echo "This program needs at least one directory parameter.  The files in the"
16   echo "current directory will be removed if a file in the specified directory"
17   echo "already exists.  So... the current directory is the less important one"
18   echo "and is presumed to have duplicates AND the directory given as parameter"
19   echo "is considered important and has the best versions of the files."
20   echo "If there is an optional second parameter, then that is used as the"
21   echo "\"current\" directory where we start from; it will be the less important"
22   echo "directory and will have its entries cleaned if they're duplicates."
23   exit 42;
24 fi
25
26 # check to make sure they gave us a good directory.
27 if [ ! -z "$whack_dir" -a ! -d "$whack_dir" ]; then
28   echo "the directory $whack_dir does not exist."
29   exit 3
30 fi
31
32 # test the tasty remote location with the better contents.
33 pushd "$exemplar_dir" &>/dev/null
34 if [ $? -ne 0 ]; then
35   # an error getting to this directory means its no good for us.
36   echo "the directory $exemplar_dir is inaccessible."
37   exit 2
38 fi
39 the_good_place="$(pwd)"
40 popd &>/dev/null
41
42 if [ ! -z "$whack_dir" ]; then
43   # use the directory as our "current" location.
44   pushd "$whack_dir" &>/dev/null
45 fi
46
47 current_dir="$(pwd)"
48
49 echo "currdir=$current_dir gooddir=$the_good_place"
50
51 if [ "$current_dir" == "$the_good_place" ]; then
52   # this is not good; they're the same location.
53   echo "the request would whack all the files in the current directory; ignoring."
54   exit 4
55 fi
56
57 # do the real work now...
58 for i in *; do
59   if [ -f "$exemplar_dir/$i" ]; then
60     echo "whacking $i"
61     rm -f "$i"
62   fi
63 done
64
65 if [ ! -z "$whack_dir" ]; then
66   popd &>/dev/null
67 fi
68