#!/bin/sh # In recent (relevant) versions of shellcheck busybox is a valid shell type # shellcheck shell=busybox # uci-defaults script to setup upsmon notifications using upssched # applied during install or on first boot after install, if setting on install # fails. 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-upssched package, which is independent of the # nut-sendmail-notify package in which nut-sendmail-notify.default lives # In the nut-upssched package, this script lives in # /etc/uci-defaults/80-nut-sched # 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-sched "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-sched "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-sched "Failed to ensure an upsmon section named upsmon exists" || true exit 1 } uci set "nut_monitor.upsmon.notifycmd=/usr/sbin/upssched" || { logger -s -t nut-sched "Failed to set notifycmd to upssched" || true uci revert nut_monitor || true exit 1 } uci commit nut_monitor || { logger -s -t nut-sched "Failed to commit changes to nut_monitor" || true exit 1 } fi exit 0