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