system-helpers/usr/local/bin/check_updates

90 lines
2 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
if [[ -z ${CHECK_UPDATES_MAIL_ADDRESS} ]]; then
echo "CHECK_UPDATES_MAIL_ADDRESS is required"
exit 1
fi
type hostname &> /dev/null || { echo "Requiring 'hostname' but it's not installed"; exit 1; }
type mailx &> /dev/null || { echo "Requiring 'mailx' but it's not installed"; exit 1; }
}
source_config "$HOME/.check_updates.conf" "/etc/check_updates.conf"
apply_defaults
check_required
HOSTNAME=$(hostname)
CHECK_UPDATES_UPDATES=$(eval "${CHECK_UPDATES_UPDATES_CMD}")
CHECK_UPDATES_UPDATES_AMOUNT=$(eval "${CHECK_UPDATES_UPDATES_AMOUNT_CMD}")
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
)
echo "$MESSAGE" | mailx -Ssendwait -s "$SUBJECT" "$CHECK_UPDATES_MAIL_ADDRESS"
fi