]> wolfpit.net Git - tool/Arch-pacman/.git/blob - contrib/pacdiff.sh.in
pacdiff: make pacmandb search the default type.
[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 shopt -s extglob
22
23 declare -r myname='pacdiff'
24 declare -r myver='@PACKAGE_VERSION@'
25
26 diffprog=${DIFFPROG:-vimdiff}
27 diffsearchpath=${DIFFSEARCHPATH:-/etc}
28 USE_COLOR='y'
29 declare -a oldsaves
30 declare -i USE_FIND=0 USE_LOCATE=0 USE_PACDB=0
31
32 m4_include(../scripts/library/output_format.sh)
33
34 usage() {
35 cat <<EOF
36 $myname is a simple pacnew/pacorig/pacsave updater.
37
38 Usage: $myname [-l | -f | -p] [--nocolor]
39
40 Search Options: select one, default: pacmandb
41 -l/--locate scan using locate
42 -f/--find scan using find
43 -p/--pacmandb scan active config files from pacman db
44
45 General Options:
46 --nocolor remove colors from output
47
48 Enviroment Variables:
49 DIFFPROG override the merge program: (default: vimdiff)
50 DIFFSEARCHPATH override the search path. (only when using find)
51 (default: /etc)
52
53 Example: DIFFPROG=meld DIFFSEARCHPATH="/boot /etc /usr" $myname
54
55 EOF
56 }
57
58 version() {
59 printf "%s %s\n" "$myname" "$myver"
60 echo 'Copyright (C) 2007 Aaron Griffin <aaronmgriffin@gmail.com>'
61 echo 'Copyright (C) 2013 Pacman Development Team <pacman-dev@archlinux.org>'
62 }
63
64 print_existing() {
65 [[ -f "$1" ]] && printf '%s\0' "$1"
66 }
67
68 print_existing_pacsave(){
69 for f in "${1}"?(.+([0-9])); do
70 [[ -f $f ]] && printf '%s\0' "$f"
71 done
72 }
73
74 cmd() {
75 if (( USE_LOCATE )); then
76 locate -0 -e -b \*.pacnew \*.pacorig \*.pacsave '*.pacsave.[0-9]*'
77 elif (( USE_FIND )); then
78 find $diffsearchpath \( -name \*.pacnew -o -name \*.pacorig -o -name \*.pacsave -o -name '*.pacsave.[0-9]*' \) -print0
79 elif (( USE_PACDB )); then
80 awk '/^%BACKUP%$/ {
81 while (getline) {
82 if (/^$/) { nextfile }
83 print $1
84 }
85 }' "${pac_db}"/*/files | while read -r bkup; do
86 print_existing "/$bkup.pacnew"
87 print_existing "/$bkup.pacorig"
88 print_existing_pacsave "/$bkup.pacsave"
89 done
90 fi
91 }
92
93 while [[ -n "$1" ]]; do
94 case "$1" in
95 -l|--locate)
96 USE_LOCATE=1;;
97 -f|--find)
98 USE_FIND=1;;
99 -p|--pacmandb)
100 USE_PACDB=1;;
101 --nocolor)
102 USE_COLOR='n';;
103 -V|--version)
104 version; exit 0;;
105 -h|--help)
106 usage; exit 0;;
107 *)
108 usage; exit 1;;
109 esac
110 shift
111 done
112
113 m4_include(../scripts/library/term_colors.sh)
114
115 case $(( USE_FIND + USE_LOCATE + USE_PACDB )) in
116 0) USE_PACDB=1;; # set the default search option
117 [^1]) error "Only one search option may be used at a time"
118 usage; exit 1;;
119 esac
120
121 if (( USE_PACDB )); then
122 if [[ ! -r @sysconfdir@/pacman.conf ]]; then
123 error "unable to read @sysconfdir@/pacman.conf"
124 usage; exit 1
125 fi
126
127 eval $(awk '/DBPath/ {print $1$2$3}' @sysconfdir@/pacman.conf)
128 pac_db="${DBPath:-@localstatedir@/lib/pacman/}local"
129 if [[ ! -d "${pac_db}" ]]; then
130 error "unable to read pacman db %s". "${pac_db}"
131 usage; exit 1
132 fi
133 fi
134
135 # see http://mywiki.wooledge.org/BashFAQ/020
136 while IFS= read -u 3 -r -d '' pacfile; do
137 file="${pacfile%.pac*}"
138 file_type="pac${pacfile##*.pac}"
139
140 # add matches for pacsave.N to oldsaves array, do not prompt
141 if [[ $file_type = pacsave.+([0-9]) ]]; then
142 oldsaves+=("$pacfile")
143 continue
144 fi
145
146 msg "%s file found for %s" "$file_type" "$file"
147 if [ ! -f "$file" ]; then
148 warning "$file does not exist"
149 rm -iv "$pacfile"
150 continue
151 fi
152
153 if cmp -s "$pacfile" "$file"; then
154 msg2 "Files are identical, removing..."
155 rm -v "$pacfile"
156 else
157 ask "(V)iew, (S)kip, (R)emove %s, (O)verwrite with %s, (Q)uit: [v/s/r/o/q] " "$file_type" "$file_type"
158 while read c; do
159 case $c in
160 q|Q) exit 0;;
161 r|R) rm -v "$pacfile"; break ;;
162 o|O) mv -v "$pacfile" "$file"; break ;;
163 v|V)
164 $diffprog "$pacfile" "$file"
165 rm -iv "$pacfile"; break ;;
166 s|S) break ;;
167 *) ask "Invalid answer. Try again: [v/s/r/o/q] "; continue ;;
168 esac
169 done
170 fi
171 done 3< <(cmd)
172
173 (( ${#oldsaves[@]} )) && warning "Ignoring %s" "${oldsaves[@]}"
174
175 exit 0
176
177 # vim: set ts=2 sw=2 noet: