]> wolfpit.net Git - tool/Arch-pacman/.git/blame - contrib/pacdiff.sh.in
pacdiff: make pacmandb search the default type.
[tool/Arch-pacman/.git] / contrib / pacdiff.sh.in
CommitLineData
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
21shopt -s extglob
22
1038e669 23declare -r myname='pacdiff'
a77e638c 24declare -r myver='@PACKAGE_VERSION@'
1038e669 25
1dbe2781 26diffprog=${DIFFPROG:-vimdiff}
6ed7d001 27diffsearchpath=${DIFFSEARCHPATH:-/etc}
f8ed8620 28USE_COLOR='y'
c4d8da47 29declare -a oldsaves
a7966122 30declare -i USE_FIND=0 USE_LOCATE=0 USE_PACDB=0
dce7aa85 31
f8ed8620
WG
32m4_include(../scripts/library/output_format.sh)
33
dce7aa85 34usage() {
bcde0270
JF
35 cat <<EOF
36$myname is a simple pacnew/pacorig/pacsave updater.
37
a7966122 38Usage: $myname [-l | -f | -p] [--nocolor]
bcde0270 39
2d9dad5e 40Search 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
45General Options:
b1b9ca01 46 --nocolor remove colors from output
bcde0270
JF
47
48Enviroment Variables:
49 DIFFPROG override the merge program: (default: vimdiff)
50 DIFFSEARCHPATH override the search path. (only when using find)
51 (default: /etc)
52
53Example: DIFFPROG=meld DIFFSEARCHPATH="/boot /etc /usr" $myname
54
55EOF
dce7aa85
XC
56}
57
a77e638c
LF
58version() {
59 printf "%s %s\n" "$myname" "$myver"
60 echo 'Copyright (C) 2007 Aaron Griffin <aaronmgriffin@gmail.com>'
23f93118 61 echo 'Copyright (C) 2013 Pacman Development Team <pacman-dev@archlinux.org>'
a77e638c
LF
62}
63
a7966122
JF
64print_existing() {
65 [[ -f "$1" ]] && printf '%s\0' "$1"
66}
67
68print_existing_pacsave(){
69 for f in "${1}"?(.+([0-9])); do
70 [[ -f $f ]] && printf '%s\0' "$f"
71 done
72}
73
dce7aa85 74cmd() {
15eea825 75 if (( USE_LOCATE )); then
c4d8da47 76 locate -0 -e -b \*.pacnew \*.pacorig \*.pacsave '*.pacsave.[0-9]*'
15eea825 77 elif (( USE_FIND )); then
c4d8da47 78 find $diffsearchpath \( -name \*.pacnew -o -name \*.pacorig -o -name \*.pacsave -o -name '*.pacsave.[0-9]*' \) -print0
a7966122
JF
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
dce7aa85
XC
90 fi
91}
92
05e954d2
JF
93while [[ -n "$1" ]]; do
94 case "$1" in
dce7aa85 95 -l|--locate)
15eea825
JF
96 USE_LOCATE=1;;
97 -f|--find)
98 USE_FIND=1;;
a7966122
JF
99 -p|--pacmandb)
100 USE_PACDB=1;;
b1b9ca01 101 --nocolor)
a7966122 102 USE_COLOR='n';;
a77e638c 103 -V|--version)
15eea825 104 version; exit 0;;
a77e638c 105 -h|--help)
15eea825 106 usage; exit 0;;
a77e638c 107 *)
15eea825 108 usage; exit 1;;
dce7aa85 109 esac
05e954d2
JF
110 shift
111done
dce7aa85 112
b1b9ca01
JF
113m4_include(../scripts/library/term_colors.sh)
114
a7966122 115case $(( USE_FIND + USE_LOCATE + USE_PACDB )) in
2d9dad5e 116 0) USE_PACDB=1;; # set the default search option
15eea825
JF
117 [^1]) error "Only one search option may be used at a time"
118 usage; exit 1;;
119esac
120
a7966122
JF
121if (( 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
133fi
134
dce7aa85
XC
135# see http://mywiki.wooledge.org/BashFAQ/020
136while IFS= read -u 3 -r -d '' pacfile; do
137 file="${pacfile%.pac*}"
dd3762ed
FP
138 file_type="pac${pacfile##*.pac}"
139
c4d8da47
JF
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
f8ed8620 146 msg "%s file found for %s" "$file_type" "$file"
dce7aa85 147 if [ ! -f "$file" ]; then
f8ed8620 148 warning "$file does not exist"
6a804d55 149 rm -iv "$pacfile"
dce7aa85
XC
150 continue
151 fi
495460d7
FP
152
153 if cmp -s "$pacfile" "$file"; then
f8ed8620 154 msg2 "Files are identical, removing..."
6a804d55 155 rm -v "$pacfile"
dce7aa85 156 else
9108bfe0 157 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
158 while read c; do
159 case $c in
9108bfe0 160 q|Q) exit 0;;
6a804d55 161 r|R) rm -v "$pacfile"; break ;;
163ba401 162 o|O) mv -v "$pacfile" "$file"; break ;;
dce7aa85 163 v|V)
a7966122
JF
164 $diffprog "$pacfile" "$file"
165 rm -iv "$pacfile"; break ;;
dce7aa85 166 s|S) break ;;
9108bfe0 167 *) ask "Invalid answer. Try again: [v/s/r/o/q] "; continue ;;
dce7aa85
XC
168 esac
169 done
170 fi
171done 3< <(cmd)
172
c4d8da47
JF
173(( ${#oldsaves[@]} )) && warning "Ignoring %s" "${oldsaves[@]}"
174
dce7aa85
XC
175exit 0
176
177# vim: set ts=2 sw=2 noet: