Rehash efficiently
[pacman-ng.git] / contrib / pacdiff.in
blobac4ce89304ef18dbf3c41805f6b3e3580de4df57
1 #!@BASH_SHELL@
2 #   pacdiff : a simple pacnew/pacorig/pacsave updater
4 #   Copyright (c) 2007 Aaron Griffin <aaronmgriffin@gmail.com>
6 #   This program is free software; you can redistribute it and/or modify
7 #   it under the terms of the GNU General Public License as published by
8 #   the Free Software Foundation; either version 2 of the License, or
9 #   (at your option) any later version.
11 #   This program is distributed in the hope that it will be useful,
12 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #   GNU General Public License for more details.
16 #   You should have received a copy of the GNU General Public License
17 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 diffprog=${DIFFPROG:-vimdiff}
21 diffsearchpath=${DIFFSEARCHPATH:-/etc}
22 locate=0
24 usage() {
25         echo "pacdiff : a simple pacnew/pacorig/pacsave updater"
26         echo "Usage : pacdiff [-l]"
27         echo "  -l/--locate makes pacdiff use locate rather than find"
28         echo "  DIFFPROG variable allows to override the default vimdiff"
29         echo "  DIFFSEARCHPATH allows to override the default /etc path"
30         echo "Example : DIFFPROG=meld DIFFSEARCHPATH=\"/boot /etc /usr\" pacdiff"
33 cmd() {
34         if [ $locate -eq 1 ]; then
35                 locate -0 -e -b \*.pacnew \*.pacorig \*.pacsave
36         else
37                 find $diffsearchpath \( -name \*.pacnew -o -name \*.pacorig -o -name \*.pacsave \) -print0
38         fi
41 if [ $# -gt 0 ]; then
42         case $1 in
43                 -l|--locate)
44                 locate=1;;
45                 *)
46                 usage; exit 0;;
47         esac
50 # see http://mywiki.wooledge.org/BashFAQ/020
51 while IFS= read -u 3 -r -d '' pacfile; do
52         file="${pacfile%.pac*}"
53         echo "File: $file"
54         if [ ! -f "$file" ]; then
55                 echo "  $file does not exist"
56                 rm -i "$pacfile"
57                 continue
58         fi
59         check="$(cmp "$pacfile" "$file")"
60         if [ -z "${check}" ]; then
61                 echo "  Files are identical, removing..."
62                 rm "$pacfile"
63         else
64                 echo -n "  File differences found. (V)iew, (S)kip, (R)emove: [v/s/r] "
65                 while read c; do
66                         case $c in
67                                 r|R) rm "$pacfile"; break ;;
68                                 v|V)
69                                 $diffprog "$pacfile" "$file"
70                                 rm -i "$pacfile"; break ;;
71                                 s|S) break ;;
72                                 *) echo -n "  Invalid answer. Try again: [v/s/r] "; continue ;;
73                         esac
74                 done
75         fi
76 done 3< <(cmd)
78 exit 0
80 # vim: set ts=2 sw=2 noet: