]> wolfpit.net Git - tool/Arch-pacman/.git/blob - contrib/pacdiff.sh.in
ce7de5292d9f09b54c9506943038d60e2928a6d9
[tool/Arch-pacman/.git] / contrib / pacdiff.sh.in
1 #!/bin/bash
2 # pacdiff : a simple pacnew/pacorig/pacsave updater
3 #
4 # Copyright (c) 2007 Aaron Griffin <aaronmgriffin@gmail.com>
5 # Copyright (c) 2013 Pacman Development Team <pacman-dev@archlinux.org>
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #
20
21 declare -r myname='pacdiff'
22 declare -r myver='@PACKAGE_VERSION@'
23
24 diffprog=${DIFFPROG:-vimdiff}
25 diffsearchpath=${DIFFSEARCHPATH:-/etc}
26 locate=0
27 USE_COLOR='y'
28 declare -a oldsaves
29
30 m4_include(../scripts/library/output_format.sh)
31
32 m4_include(../scripts/library/term_colors.sh)
33
34 usage() {
35 cat <<EOF
36 $myname is a simple pacnew/pacorig/pacsave updater.
37
38 Usage: $myname [-l]
39
40 Options:
41 -l/--locate scan using locate (default: find)
42
43 Enviroment Variables:
44 DIFFPROG override the merge program: (default: vimdiff)
45 DIFFSEARCHPATH override the search path. (only when using find)
46 (default: /etc)
47
48 Example: DIFFPROG=meld DIFFSEARCHPATH="/boot /etc /usr" $myname
49
50 EOF
51 }
52
53 version() {
54 printf "%s %s\n" "$myname" "$myver"
55 echo 'Copyright (C) 2007 Aaron Griffin <aaronmgriffin@gmail.com>'
56 echo 'Copyright (C) 2013 Pacman Development Team <pacman-dev@archlinux.org>'
57 }
58
59 cmd() {
60 if [ $locate -eq 1 ]; then
61 locate -0 -e -b \*.pacnew \*.pacorig \*.pacsave '*.pacsave.[0-9]*'
62 else
63 find $diffsearchpath \( -name \*.pacnew -o -name \*.pacorig -o -name \*.pacsave -o -name '*.pacsave.[0-9]*' \) -print0
64 fi
65 }
66
67 while [[ -n "$1" ]]; do
68 case "$1" in
69 -l|--locate)
70 locate=1;;
71 -V|--version)
72 version; exit 0;;
73 -h|--help)
74 usage; exit 0;;
75 *)
76 usage; exit 1;;
77 esac
78 shift
79 done
80
81 # see http://mywiki.wooledge.org/BashFAQ/020
82 while IFS= read -u 3 -r -d '' pacfile; do
83 file="${pacfile%.pac*}"
84 file_type="pac${pacfile##*.pac}"
85
86 # add matches for pacsave.N to oldsaves array, do not prompt
87 if [[ $file_type = pacsave.+([0-9]) ]]; then
88 oldsaves+=("$pacfile")
89 continue
90 fi
91
92 msg "%s file found for %s" "$file_type" "$file"
93 if [ ! -f "$file" ]; then
94 warning "$file does not exist"
95 rm -iv "$pacfile"
96 continue
97 fi
98
99 if cmp -s "$pacfile" "$file"; then
100 msg2 "Files are identical, removing..."
101 rm -v "$pacfile"
102 else
103 ask "(V)iew, (S)kip, (R)emove %s, (O)verwrite with %s, (Q)uit: [v/s/r/o/q] " "$file_type" "$file_type"
104 while read c; do
105 case $c in
106 q|Q) exit 0;;
107 r|R) rm -v "$pacfile"; break ;;
108 o|O) mv -v "$pacfile" "$file"; break ;;
109 v|V)
110 $diffprog "$pacfile" "$file"
111 rm -iv "$pacfile"; break ;;
112 s|S) break ;;
113 *) ask "Invalid answer. Try again: [v/s/r/o/q] "; continue ;;
114 esac
115 done
116 fi
117 done 3< <(cmd)
118
119 (( ${#oldsaves[@]} )) && warning "Ignoring %s" "${oldsaves[@]}"
120
121 exit 0
122
123 # vim: set ts=2 sw=2 noet: