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