#!/bin/sh

# This is a sample NUT notification script aimed to aid debugging of
# `upsmon` behavior, as its NOTIFYCMD (as specified in upsmon.conf).
# This script creates or appends to a log file named per user ID in
# the specified or default temporary location with an entry per line
# like this:
#   Sat Apr  6 17:23:06 UTC 2024    [uid=77(nut) gid=77(nut) groups=77(nut)]        COMMBAD [eco650]:        Communications with UPS eco650 lost     (1 tokens)
#   Sat Apr  6 17:28:01 UTC 2024    [uid=77(nut) gid=77(nut) groups=77(nut)]        NOCOMM  [eco650]:        UPS eco650 is unavailable       (1 tokens)
#
# Copyright (C) 2023-2025 by Jim Klimov <jimklimov+nut@gmail.com>
# Licensed under the terms of the Network UPS Tools source license (GPLv2+)

[ -n "${TEMPDIR-}" ] || TEMPDIR="${TMPDIR-}"
[ -n "${TEMPDIR-}" ] || { [ -d "/dev/shm" ] && TEMPDIR="/dev/shm" || TEMPDIR="/tmp" ; }

printf '%s\t[%s]\t%s\t[%s]:\t%s\t(%s tokens)\n' "`date -u`" "`id`" "${NOTIFYTYPE-}" "${UPSNAME-}" "$*" "$#" >> "${TEMPDIR}/notifyme-`id -u`.log"

if [ -n "${TOP_BUILDDIR}" -a -x "${TOP_BUILDDIR}/clients/upssched" ] ; then
	if [ -n "${NUT_DEBUG_LEVEL_UPSSCHED}" ] ; then
		NUT_DEBUG_LEVEL="${NUT_DEBUG_LEVEL_UPSSCHED}"
		export NUT_DEBUG_LEVEL
	fi
	if [ "${NUT_DEBUG_LEVEL-}" -gt 0 ] 2>/dev/null ; then
		printf '%s: %s\t%s\t[%s]:\targs: %s\t(%s arg tokens)\n' "`date -u`" "$0" "${NOTIFYTYPE-}" "${UPSNAME-}" "$*" "$#" >&2
	fi
	"${TOP_BUILDDIR}/clients/upssched" "$@"
fi
