ProxSpace/msys2/usr/bin/checkupdates
2023-09-22 00:10:50 +02:00

143 lines
3.2 KiB
Bash

#!/usr/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='1.9.1'
LIBRARY=${LIBRARY:-'/usr/share/makepkg'}
DOWNLOAD_CACHE=0
USE_COLOR='y'
# Import libmakepkg
source "$LIBRARY"/util/message.sh
source "$LIBRARY"/util/parseopts.sh
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
}
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
--nocolor do not colorize output
-n, --nosync do not sync the temporary database
-h, --help display this help message and exit
-V, --version display version information and exit
Environment Variables:
CHECKUPDATES_DB override the path of the temporary database
(default: "\${TMPDIR:-/tmp}/checkup-db-\${UID}")
TMPDIR override the temporary directory (default: '/tmp')
EOF
}
version() {
printf "%s %s\n" "$myname" "$myver"
}
OPT_SHORT='dnhV'
OPT_LONG=('download' 'nosync' 'help' 'nocolor' 'version')
if ! parseopts "$OPT_SHORT" "${OPT_LONG[@]}" -- "$@"; then
exit 1
fi
set -- "${OPTRET[@]}"
unset OPT_SHORT OPT_LONG OPTRET
SYNC=1
while :; do
case $1 in
-d|--download)
DOWNLOAD_CACHE=1 ;;
--nocolor)
USE_COLOR='n';;
-n|--nosync)
SYNC=0 ;;
-h|--help)
usage
exit 0 ;;
-V|--version)
version
exit 0 ;;
--)
shift
break ;;
esac
shift
done
# 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
if [[ -z $CHECKUPDATES_DB ]]; then
CHECKUPDATES_DB="${TMPDIR:-/tmp}/checkup-db-${UID}/"
fi
trap 'rm -f $CHECKUPDATES_DB/db.lck' INT TERM EXIT
DBPath="$(pacman-conf DBPath)"
if [[ -z "$DBPath" ]] || [[ ! -d "$DBPath" ]]; then
DBPath="/var/lib/pacman/"
fi
if (( ${SYNC} )); then
mkdir -p "$CHECKUPDATES_DB"
ln -s "${DBPath}/local" "$CHECKUPDATES_DB" &> /dev/null
if ! pacman -Sy --dbpath "$CHECKUPDATES_DB" --logfile /dev/null &> /dev/null; then
die 'Cannot fetch updates'
fi
fi
mapfile -t updates < <(pacman -Qu --dbpath "$CHECKUPDATES_DB" 2> /dev/null | grep -v '\[.*\]')
if (( ${#updates[@]} )); then
printf '%s\n' "${updates[@]}"
if (( DOWNLOAD_CACHE )); then
runcmd pacman -Sw --noconfirm "${updates[@]%% *}" --dbpath "$CHECKUPDATES_DB" --logfile /dev/null
fi
else
exit 2
fi