checkupdates.sh.in 3.08 KB
Newer Older
Johannes Löthberg's avatar
Johannes Löthberg committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/bin/bash
#
#   checkupdates: Safely print a list of pending updates.
#
#   Copyright (c) 2013 Kyle Keen <keenerd@gmail.com>
#
#   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='checkupdates'
declare -r myver='@PACKAGE_VERSION@'

Eli Schwartz's avatar
Eli Schwartz committed
24
25
LIBRARY=${LIBRARY:-'@libmakepkgdir@'}

26
DOWNLOAD_CACHE=0
Eli Schwartz's avatar
Eli Schwartz committed
27
USE_COLOR=0
28

Eli Schwartz's avatar
Eli Schwartz committed
29
30
31
# Import libmakepkg
source "$LIBRARY"/util/message.sh
source "$LIBRARY"/util/parseopts.sh
32

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
die() {
	error "$@"
	exit 1
}

runcmd() {
	if (( EUID != 0 )); then
		msg 'Escalating privileges using sudo'
		if sudo -v &>/dev/null && sudo -l &>/dev/null; then
			sudo "$@"
		else
			die 'Failed to escalate'
		fi
	else
		"$@"
	fi
}

51
52
53
54
55
56
57
58
59
60
usage() {
	cat << __EOF__
${myname} v${myver}

Safely print a list of pending updates

Usage: ${myname} [options]

  Options:
    -d, --download        download pending updates to the pacman cache.
Sam B's avatar
Sam B committed
61
    -n, --nosync          do not sync the temporary database.
62
63
64
    -h, --help            display this help message and exit.

Note: Export the "CHECKUPDATES_DB" variable to change the path of the temporary database.
Johannes Löthberg's avatar
Johannes Löthberg committed
65

66
67
68
__EOF__
}

Sam B's avatar
Sam B committed
69
OPT_SHORT='dnh'
70
OPT_LONG=('download' 'nosync' 'help' 'nocolor')
71
72
73

if ! parseopts "$OPT_SHORT" "${OPT_LONG[@]}" -- "$@"; then
	exit 1
Johannes Löthberg's avatar
Johannes Löthberg committed
74
fi
75
76
77
set -- "${OPTRET[@]}"
unset OPT_SHORT OPT_LONG OPTRET

Sam B's avatar
Sam B committed
78
SYNC=1
79
80
81
82
while :; do
	case $1 in
		-d|--download)
			DOWNLOAD_CACHE=1 ;;
Sam B's avatar
Sam B committed
83
84
		-n|--nosync)
			SYNC=0 ;;
85
86
87
		-h|--help)
			usage
			exit 0 ;;
Eli Schwartz's avatar
Eli Schwartz committed
88
89
		--nocolor)
			USE_COLOR='n';;
90
91
92
93
94
95
		--)
			shift
			break ;;
	esac
	shift
done
Johannes Löthberg's avatar
Johannes Löthberg committed
96

Eli Schwartz's avatar
Eli Schwartz committed
97
98
99
100
101
102
103
# check if messages are to be printed using color
if [[ -t 2 && $USE_COLOR != "n" ]]; then
	colorize
else
	unset ALL_OFF BOLD BLUE GREEN RED YELLOW
fi

Johannes Löthberg's avatar
Johannes Löthberg committed
104
if ! type -P fakeroot >/dev/null; then
105
	die 'Cannot find the fakeroot binary'
Johannes Löthberg's avatar
Johannes Löthberg committed
106
107
108
fi

if [[ -z $CHECKUPDATES_DB ]]; then
109
	CHECKUPDATES_DB="${TMPDIR:-/tmp}/checkup-db-${UID}/"
Johannes Löthberg's avatar
Johannes Löthberg committed
110
111
112
113
fi

trap 'rm -f $CHECKUPDATES_DB/db.lck' INT TERM EXIT

114
DBPath="$(pacman-conf DBPath)"
115
116
117
if [[ -z "$DBPath" ]] || [[ ! -d "$DBPath" ]]; then
	DBPath="@localstatedir@/lib/pacman/"
fi
Johannes Löthberg's avatar
Johannes Löthberg committed
118

Sam B's avatar
Sam B committed
119
120
121
122
123
124
if (( ${SYNC} )); then
	mkdir -p "$CHECKUPDATES_DB"
	ln -s "${DBPath}/local" "$CHECKUPDATES_DB" &> /dev/null
	if ! fakeroot -- pacman -Sy --dbpath "$CHECKUPDATES_DB" --logfile /dev/null &> /dev/null; then
	    die 'Cannot fetch updates'
	fi
125
fi
126
mapfile -t updates < <(pacman -Qu --dbpath "$CHECKUPDATES_DB" 2> /dev/null | grep -v '\[.*\]')
Johannes Löthberg's avatar
Johannes Löthberg committed
127

128
129
130
if (( ${#updates[@]} )); then
	printf '%s\n' "${updates[@]}"
	if (( DOWNLOAD_CACHE )); then
131
		runcmd pacman -Sw --noconfirm "${updates[@]%% *}" --dbpath "$CHECKUPDATES_DB" --logfile /dev/null
132
133
	fi
else
134
	exit 2
135
fi
Johannes Löthberg's avatar
Johannes Löthberg committed
136
137

# vim: set noet: