system-helpers/usr/local/bin/check_updates
Alexander Schäferdiek 13c7335ee3
All checks were successful
continuous-integration/drone/push Build is passing
Fix check_updates and update man reference to point to correct examples
2023-01-27 00:53:23 +01:00

85 lines
1.8 KiB
Bash
Executable file

#!/usr/bin/env bash
# usage
usage() {
USAGE=$(
cat <<EOF
Usage: check_updates
Sends out update notifications. Please see man check_updates.
EOF
)
echo "$USAGE"
}
set -e
# check for config file
apply_config() {
local config=$1
if [[ ! -f "$config" ]]; then
echo "No config file specified"
echo ""
usage
exit 1
fi
set -a
# shellcheck disable=SC1090
source "$config"
set +a
}
source_config() {
local configFallback=$1
local configGlobalFallback=$2
if [[ -f "$configFallback" ]]; then
apply_config "$configFallback"
return
fi
if [[ -f "$configGlobalFallback" ]]; then
apply_config "$configGlobalFallback"
return
fi
}
apply_defaults() {
[[ -z ${CHECK_UPDATES_UPDATES_CMD} ]] && CHECK_UPDATES_UPDATES_CMD="/usr/bin/checkupdates"
[[ -z ${CHECK_UPDATES_UPDATES_AMOUNT_CMD} ]] && CHECK_UPDATES_UPDATES_AMOUNT_CMD="/usr/bin/checkupdates | wc -l"
}
check_required() {
if [[ -z ${CHECK_UPDATES_UPDATES_CMD} ]]; then
echo "CHECK_UPDATES_UPDATES_CMD is required"
exit 1
fi
if [[ -z ${CHECK_UPDATES_UPDATES_AMOUNT_CMD} ]]; then
echo "CHECK_UPDATES_UPDATES_AMOUNT_CMD is required"
exit 1
fi
type hostname &> /dev/null || { echo "Requiring 'hostname' but it's not installed"; exit 1; }
}
source_config "$HOME/.check_updates.conf" "/etc/check_updates.conf"
apply_defaults
check_required
CHECK_UPDATES_UPDATES=$(eval "${CHECK_UPDATES_UPDATES_CMD}")
CHECK_UPDATES_UPDATES_AMOUNT=$(eval "${CHECK_UPDATES_UPDATES_AMOUNT_CMD}")
HOSTNAME=$(hostname)
if [[ "$CHECK_UPDATES_UPDATES_AMOUNT" -gt "0" ]]; then
SUBJECT="[updates ${HOSTNAME}]"
MESSAGE=$(cat <<EOF
There are ${CHECK_UPDATES_UPDATES_AMOUNT} updates available on ${HOSTNAME}.
${CHECK_UPDATES_UPDATES};
EOF
)
notifier "${SUBJECT}" "${MESSAGE}"
fi