2011-06-30 11:07:54 -04:00
|
|
|
#!/bin/bash
|
2014-10-01 01:57:44 -04:00
|
|
|
# pacdiff : a simple pacnew/pacsave updater
|
2007-02-23 03:03:50 -05:00
|
|
|
#
|
|
|
|
# Copyright (c) 2007 Aaron Griffin <aaronmgriffin@gmail.com>
|
2014-01-01 05:24:48 -05:00
|
|
|
# Copyright (c) 2013-2014 Pacman Development Team <pacman-dev@archlinux.org>
|
2007-02-23 03:03:50 -05:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2007-12-10 23:55:22 -05:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2007-02-23 03:03:50 -05:00
|
|
|
#
|
|
|
|
|
2013-07-18 17:12:32 -04:00
|
|
|
shopt -s extglob
|
|
|
|
|
2011-12-06 16:29:32 -05:00
|
|
|
declare -r myname='pacdiff'
|
2011-12-12 16:36:57 -05:00
|
|
|
declare -r myver='@PACKAGE_VERSION@'
|
2011-12-06 16:29:32 -05:00
|
|
|
|
2014-06-29 03:55:06 -04:00
|
|
|
diffprog=${DIFFPROG:-'vim -d'}
|
2009-10-21 17:46:47 -04:00
|
|
|
diffsearchpath=${DIFFSEARCHPATH:-/etc}
|
2013-03-04 03:42:34 -05:00
|
|
|
USE_COLOR='y'
|
2013-07-07 09:45:02 -04:00
|
|
|
declare -a oldsaves
|
2013-07-18 18:05:58 -04:00
|
|
|
declare -i USE_FIND=0 USE_LOCATE=0 USE_PACDB=0 OUTPUTONLY=0
|
2009-02-17 17:40:11 -05:00
|
|
|
|
2013-03-04 03:42:34 -05:00
|
|
|
m4_include(../scripts/library/output_format.sh)
|
|
|
|
|
2009-02-17 17:40:11 -05:00
|
|
|
usage() {
|
2013-07-15 11:00:30 -04:00
|
|
|
cat <<EOF
|
2013-11-08 02:53:33 -05:00
|
|
|
${myname} (pacman) v${myver}
|
|
|
|
|
2014-10-01 01:57:44 -04:00
|
|
|
A simple program to merge or remove pacnew/pacsave files.
|
2013-07-15 11:00:30 -04:00
|
|
|
|
2013-07-18 17:12:32 -04:00
|
|
|
Usage: $myname [-l | -f | -p] [--nocolor]
|
2013-07-15 11:00:30 -04:00
|
|
|
|
2013-11-14 23:06:24 -05:00
|
|
|
Search Options: select one (default: --pacmandb)
|
2013-07-16 12:38:15 -04:00
|
|
|
-l/--locate scan using locate
|
|
|
|
-f/--find scan using find
|
2013-11-08 02:53:33 -05:00
|
|
|
-p/--pacmandb scan active config files from pacman database
|
2013-07-16 12:38:15 -04:00
|
|
|
|
|
|
|
General Options:
|
2013-07-18 18:05:58 -04:00
|
|
|
-o/--output print files instead of merging them
|
2013-07-15 13:02:33 -04:00
|
|
|
--nocolor remove colors from output
|
2013-07-15 11:00:30 -04:00
|
|
|
|
2013-11-08 02:53:33 -05:00
|
|
|
Environment Variables:
|
2014-06-29 03:55:06 -04:00
|
|
|
DIFFPROG override the merge program: (default: 'vim -d')
|
2013-07-15 11:00:30 -04:00
|
|
|
DIFFSEARCHPATH override the search path. (only when using find)
|
|
|
|
(default: /etc)
|
|
|
|
|
|
|
|
Example: DIFFPROG=meld DIFFSEARCHPATH="/boot /etc /usr" $myname
|
2013-07-18 18:05:58 -04:00
|
|
|
Example: $myname --output --locate
|
2013-07-15 11:00:30 -04:00
|
|
|
|
|
|
|
EOF
|
2009-02-17 17:40:11 -05:00
|
|
|
}
|
|
|
|
|
2011-12-12 16:36:57 -05:00
|
|
|
version() {
|
|
|
|
printf "%s %s\n" "$myname" "$myver"
|
|
|
|
echo 'Copyright (C) 2007 Aaron Griffin <aaronmgriffin@gmail.com>'
|
2014-01-01 05:24:48 -05:00
|
|
|
echo 'Copyright (C) 2013-2014 Pacman Development Team <pacman-dev@archlinux.org>'
|
2011-12-12 16:36:57 -05:00
|
|
|
}
|
|
|
|
|
2013-07-18 17:12:32 -04:00
|
|
|
print_existing() {
|
|
|
|
[[ -f "$1" ]] && printf '%s\0' "$1"
|
|
|
|
}
|
|
|
|
|
|
|
|
print_existing_pacsave(){
|
|
|
|
for f in "${1}"?(.+([0-9])); do
|
|
|
|
[[ -f $f ]] && printf '%s\0' "$f"
|
|
|
|
done
|
2013-11-14 23:06:24 -05:00
|
|
|
}
|
2013-07-18 17:12:32 -04:00
|
|
|
|
2009-02-17 17:40:11 -05:00
|
|
|
cmd() {
|
2013-07-16 12:38:15 -04:00
|
|
|
if (( USE_LOCATE )); then
|
2013-07-07 09:45:02 -04:00
|
|
|
locate -0 -e -b \*.pacnew \*.pacorig \*.pacsave '*.pacsave.[0-9]*'
|
2013-07-16 12:38:15 -04:00
|
|
|
elif (( USE_FIND )); then
|
2013-07-07 09:45:02 -04:00
|
|
|
find $diffsearchpath \( -name \*.pacnew -o -name \*.pacorig -o -name \*.pacsave -o -name '*.pacsave.[0-9]*' \) -print0
|
2013-07-18 17:12:32 -04:00
|
|
|
elif (( USE_PACDB )); then
|
|
|
|
awk '/^%BACKUP%$/ {
|
|
|
|
while (getline) {
|
|
|
|
if (/^$/) { nextfile }
|
|
|
|
print $1
|
|
|
|
}
|
|
|
|
}' "${pac_db}"/*/files | while read -r bkup; do
|
|
|
|
print_existing "/$bkup.pacnew"
|
|
|
|
print_existing "/$bkup.pacorig"
|
|
|
|
print_existing_pacsave "/$bkup.pacsave"
|
|
|
|
done
|
2009-02-17 17:40:11 -05:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2013-11-14 23:06:24 -05:00
|
|
|
while [[ -n "$1" ]]; do
|
2013-07-15 12:56:03 -04:00
|
|
|
case "$1" in
|
2009-02-17 17:40:11 -05:00
|
|
|
-l|--locate)
|
2013-07-16 12:38:15 -04:00
|
|
|
USE_LOCATE=1;;
|
|
|
|
-f|--find)
|
|
|
|
USE_FIND=1;;
|
2013-07-18 17:12:32 -04:00
|
|
|
-p|--pacmandb)
|
|
|
|
USE_PACDB=1;;
|
2013-07-18 18:05:58 -04:00
|
|
|
-o|--output)
|
|
|
|
OUTPUTONLY=1;;
|
2013-07-15 13:02:33 -04:00
|
|
|
--nocolor)
|
2013-07-18 17:12:32 -04:00
|
|
|
USE_COLOR='n';;
|
2011-12-12 16:36:57 -05:00
|
|
|
-V|--version)
|
2013-07-16 12:38:15 -04:00
|
|
|
version; exit 0;;
|
2011-12-12 16:36:57 -05:00
|
|
|
-h|--help)
|
2013-07-16 12:38:15 -04:00
|
|
|
usage; exit 0;;
|
2011-12-12 16:36:57 -05:00
|
|
|
*)
|
2013-07-16 12:38:15 -04:00
|
|
|
usage; exit 1;;
|
2009-02-17 17:40:11 -05:00
|
|
|
esac
|
2013-07-15 12:56:03 -04:00
|
|
|
shift
|
|
|
|
done
|
2009-02-17 17:40:11 -05:00
|
|
|
|
2013-07-15 13:02:33 -04:00
|
|
|
m4_include(../scripts/library/term_colors.sh)
|
|
|
|
|
2014-06-29 03:55:06 -04:00
|
|
|
if ! type -p ${diffprog%% *} >/dev/null; then
|
2013-10-14 02:12:00 -04:00
|
|
|
error "Cannot find the $diffprog binary required for viewing differences."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2013-07-18 17:12:32 -04:00
|
|
|
case $(( USE_FIND + USE_LOCATE + USE_PACDB )) in
|
2013-07-18 17:50:44 -04:00
|
|
|
0) USE_PACDB=1;; # set the default search option
|
2013-07-16 12:38:15 -04:00
|
|
|
[^1]) error "Only one search option may be used at a time"
|
|
|
|
usage; exit 1;;
|
|
|
|
esac
|
|
|
|
|
2013-07-18 17:12:32 -04:00
|
|
|
if (( USE_PACDB )); then
|
|
|
|
if [[ ! -r @sysconfdir@/pacman.conf ]]; then
|
|
|
|
error "unable to read @sysconfdir@/pacman.conf"
|
|
|
|
usage; exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
eval $(awk '/DBPath/ {print $1$2$3}' @sysconfdir@/pacman.conf)
|
|
|
|
pac_db="${DBPath:-@localstatedir@/lib/pacman/}local"
|
|
|
|
if [[ ! -d "${pac_db}" ]]; then
|
2013-11-14 23:06:24 -05:00
|
|
|
error "unable to read pacman database %s". "${pac_db}"
|
2013-07-18 17:12:32 -04:00
|
|
|
usage; exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2009-02-17 17:40:11 -05:00
|
|
|
# see http://mywiki.wooledge.org/BashFAQ/020
|
|
|
|
while IFS= read -u 3 -r -d '' pacfile; do
|
|
|
|
file="${pacfile%.pac*}"
|
2012-12-19 17:20:13 -05:00
|
|
|
file_type="pac${pacfile##*.pac}"
|
2013-11-14 23:06:24 -05:00
|
|
|
|
2013-07-18 18:05:58 -04:00
|
|
|
if (( OUTPUTONLY )); then
|
|
|
|
echo "$pacfile"
|
|
|
|
continue
|
|
|
|
fi
|
2012-12-19 17:20:13 -05:00
|
|
|
|
2013-07-07 09:45:02 -04:00
|
|
|
# add matches for pacsave.N to oldsaves array, do not prompt
|
|
|
|
if [[ $file_type = pacsave.+([0-9]) ]]; then
|
|
|
|
oldsaves+=("$pacfile")
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
2013-03-04 03:42:34 -05:00
|
|
|
msg "%s file found for %s" "$file_type" "$file"
|
2009-02-17 17:40:11 -05:00
|
|
|
if [ ! -f "$file" ]; then
|
2013-03-04 03:42:34 -05:00
|
|
|
warning "$file does not exist"
|
2012-12-19 17:21:06 -05:00
|
|
|
rm -iv "$pacfile"
|
2009-02-17 17:40:11 -05:00
|
|
|
continue
|
|
|
|
fi
|
2012-12-19 17:21:56 -05:00
|
|
|
|
|
|
|
if cmp -s "$pacfile" "$file"; then
|
2013-03-04 03:42:34 -05:00
|
|
|
msg2 "Files are identical, removing..."
|
2012-12-19 17:21:06 -05:00
|
|
|
rm -v "$pacfile"
|
2009-02-17 17:40:11 -05:00
|
|
|
else
|
2013-07-11 13:03:36 -04:00
|
|
|
ask "(V)iew, (S)kip, (R)emove %s, (O)verwrite with %s, (Q)uit: [v/s/r/o/q] " "$file_type" "$file_type"
|
2009-02-17 17:40:11 -05:00
|
|
|
while read c; do
|
|
|
|
case $c in
|
2013-07-11 13:03:36 -04:00
|
|
|
q|Q) exit 0;;
|
2012-12-19 17:21:06 -05:00
|
|
|
r|R) rm -v "$pacfile"; break ;;
|
2012-12-19 17:22:22 -05:00
|
|
|
o|O) mv -v "$pacfile" "$file"; break ;;
|
2009-02-17 17:40:11 -05:00
|
|
|
v|V)
|
2013-07-18 17:12:32 -04:00
|
|
|
$diffprog "$pacfile" "$file"
|
2013-11-17 22:01:38 -05:00
|
|
|
ask "(V)iew, (S)kip, (R)emove %s, (O)verwrite with %s, (Q)uit: [v/s/r/o/q] " "$file_type" "$file_type";
|
|
|
|
continue ;;
|
2009-02-17 17:40:11 -05:00
|
|
|
s|S) break ;;
|
2013-07-11 13:03:36 -04:00
|
|
|
*) ask "Invalid answer. Try again: [v/s/r/o/q] "; continue ;;
|
2009-02-17 17:40:11 -05:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
done 3< <(cmd)
|
|
|
|
|
2013-07-07 09:45:02 -04:00
|
|
|
(( ${#oldsaves[@]} )) && warning "Ignoring %s" "${oldsaves[@]}"
|
|
|
|
|
2009-02-17 17:40:11 -05:00
|
|
|
exit 0
|
|
|
|
|
2014-01-22 18:06:11 -05:00
|
|
|
# vim: set noet:
|