summaryrefslogtreecommitdiffstats
path: root/contrib/pacdiff.sh.in
blob: 17b0220b628b1ede77e5e0a1873e13a9217ae0c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/bash
#   pacdiff : a simple pacnew/pacorig/pacsave updater
#
#   Copyright (c) 2007 Aaron Griffin <aaronmgriffin@gmail.com>
#   Copyright (c) 2013 Pacman Development Team <pacman-dev@archlinux.org>
#
#   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
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

declare -r myname='pacdiff'
declare -r myver='@PACKAGE_VERSION@'

diffprog=${DIFFPROG:-vimdiff}
diffsearchpath=${DIFFSEARCHPATH:-/etc}
USE_COLOR='y'
declare -a oldsaves
declare -i USE_FIND=0 USE_LOCATE=0

m4_include(../scripts/library/output_format.sh)

usage() {
	cat <<EOF
$myname is a simple pacnew/pacorig/pacsave updater.

Usage: $myname [-l | -f] [--nocolor]

Search Options:     select one, default: find
  -l/--locate       scan using locate
  -f/--find         scan using find

General Options:
  --nocolor         remove colors from output

Enviroment Variables:
  DIFFPROG          override the merge program: (default: vimdiff)
  DIFFSEARCHPATH    override the search path. (only when using find)
                    (default: /etc)

Example: DIFFPROG=meld DIFFSEARCHPATH="/boot /etc /usr" $myname

EOF
}

version() {
	printf "%s %s\n" "$myname" "$myver"
	echo 'Copyright (C) 2007 Aaron Griffin <aaronmgriffin@gmail.com>'
	echo 'Copyright (C) 2013 Pacman Development Team <pacman-dev@archlinux.org>'
}

cmd() {
	if (( USE_LOCATE )); then
		locate -0 -e -b \*.pacnew \*.pacorig \*.pacsave '*.pacsave.[0-9]*'
	elif (( USE_FIND )); then
		find $diffsearchpath \( -name \*.pacnew -o -name \*.pacorig -o -name \*.pacsave -o -name '*.pacsave.[0-9]*' \) -print0
	fi
}

while [[ -n "$1" ]]; do	
	case "$1" in
		-l|--locate)
			USE_LOCATE=1;;
		-f|--find)
			USE_FIND=1;;
		--nocolor)
			USE_COLOR='n' ;;
		-V|--version)
			version; exit 0;;
		-h|--help)
			usage; exit 0;;
		*)
			usage; exit 1;;
	esac
	shift
done

m4_include(../scripts/library/term_colors.sh)

case $(( USE_FIND + USE_LOCATE )) in
	0) USE_FIND=1;; # set the default search option
	[^1]) error "Only one search option may be used at a time"
	 	usage; exit 1;;
esac

# see http://mywiki.wooledge.org/BashFAQ/020
while IFS= read -u 3 -r -d '' pacfile; do
	file="${pacfile%.pac*}"
	file_type="pac${pacfile##*.pac}"

	# add matches for pacsave.N to oldsaves array, do not prompt
	if [[ $file_type = pacsave.+([0-9]) ]]; then
		oldsaves+=("$pacfile")
		continue
	fi

	msg "%s file found for %s" "$file_type" "$file"
	if [ ! -f "$file" ]; then
		warning "$file does not exist"
		rm -iv "$pacfile"
		continue
	fi

	if cmp -s "$pacfile" "$file"; then
		msg2 "Files are identical, removing..."
		rm -v "$pacfile"
	else
		ask "(V)iew, (S)kip, (R)emove %s, (O)verwrite with %s, (Q)uit: [v/s/r/o/q] " "$file_type" "$file_type"
		while read c; do
			case $c in
				q|Q) exit 0;;
				r|R) rm -v "$pacfile"; break ;;
				o|O) mv -v "$pacfile" "$file"; break ;;
				v|V)
				$diffprog "$pacfile" "$file"
				rm -iv "$pacfile"; break ;;
				s|S) break ;;
				*) ask "Invalid answer. Try again: [v/s/r/o/q] "; continue ;;
			esac
		done
	fi
done 3< <(cmd)

(( ${#oldsaves[@]} )) && warning "Ignoring %s" "${oldsaves[@]}"

exit 0

# vim: set ts=2 sw=2 noet: