#!/bin/sh # In recent (relevant) versions of shellcheck busybox is a valid shell type # shellcheck shell=busybox # uci-defaults script to configure sendmail notifications for UPS monitor on # first boot after installation (if not possible during image creation) # Expects nut-notify-exec to also be run in uci-defaults. # IPKG_INSTROOT is intentionally only set when building an image and # is intentionally empty on a live OpenWrt device # Shellcheck source paths intentionally point to the location of files of # the scripts in the development environment (where shellcheck is used), not # on the live OpenWrt device # This script lives in the nut-sendmail-notify package, which is independent of # the nut-upssched package in which nut-sched.default lives. # In the nut-sendmail-notify package, this script lives in # /etc/uci-defaults/81-sendmail-notify # The separate packages limit the opportunities for code-sharing across the # scripts. # Only run this uci-defaults script on a live OpenWrt device [ -z "${IPKG_INSTROOT}" ] || exit 0 # shellcheck source=net/nut/files/functions.sh.functions . /lib/functions.sh || { logger -s -t nut-sendmail-notify "FATAL: Unable to source 'functions.sh'" || true exit 1 } # Flags for existing notifycmd SKIP_ADD_NOTIFYCMD="false" # Load and process nut_monitor (upsmon) configuration config_load nut_monitor || { logger -s -t nut-sendmail-notify "FATAL: Loading 'nut_monitor' failed." || true exit 1 } config_get val "upsmon" notifycmd # shellcheck disable=SC2154 if [ -n "${val}" ]; then SKIP_ADD_NOTIFYCMD="true" fi # If notification command is not set, set it if [ "${SKIP_ADD_NOTIFYCMD}" = "false" ]; then # Ensure upsmon type section with the name upsmon exists uci set "nut_monitor.upsmon=upsmon" || { logger -s -t nut-sendmail-notify "Failed to ensure an upsmon section named upsmon exists" || true exit 1 } # Only configure notifycmd to be nut-sendmail-notify if sendmail is present if [ -n "$(command -v sendmail)" ]; then # If nut-upssched (default script nut-sched) is also present, notifycmd will # already be set and SKIP_ADD_NOTIFYCMD will be true, so this code will # never be reached. This is intentional. upssched takes precedence over # nut-sendmail-notify, unless the user writes a script that supports both. uci set "nut_monitor.upsmon.notifycmd=/usr/bin/nut-sendmail-notify" || { logger -s -t nut-sendmail-notify "Failed to set notifycmd to be nut-sendmail-notify" || true uci revert nut_monitor || true exit 1 } fi uci commit nut_monitor || { logger -s -t nut-sendmail-notify "Failed to commit changes to nut_monitor" || true exit 1 } fi exit 0