You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

75 lines
1.7 KiB

#!/bin/bash
## Send a notification with NTFY and check if the config file is complete
if [[ "$UID" == "0" ]]; then
NTFY_CONFIG_FILE="/etc/ntfy/ntfy.conf"
else
NTFY_CONFIG_FILE="$HOME/.config/ntfy/ntfy.conf"
fi
SERVER="https://ntfy.0k.io/"
if ! [ -e "$NTFY_CONFIG_FILE" ]; then
mkdir -p "${NTFY_CONFIG_FILE%/*}"
## default option to change if needed
echo "SERVER=$SERVER" > "$NTFY_CONFIG_FILE"
## else if $NTFY_CONFIG_FILE exist but SERVER is not defined
elif ! grep -q "^SERVER=" "$NTFY_CONFIG_FILE"; then
echo "SERVER=$SERVER" >> "$NTFY_CONFIG_FILE"
fi
source "$NTFY_CONFIG_FILE"
for var in SERVER LOGIN PASSWORD; do
if ! [ -v "$var" ]; then
echo "Error: missing $var in $NTFY_CONFIG_FILE"
exit 1
fi
done
exname=${0##*/}
default_channel="main"
usage="Usage: $exname [-c CHANNEL] MESSAGE
----------------------------------------------
--- Send MESSAGE to the specified CHANNEL. ---
----------------------------------------------
If no CHANNEL is provided, the message will be sent to the default channel
Default CHANNEL is format as follow : ConfiguredLOGIN_${default_channel}"
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-c|--channel)
channel="$2"
message="$3"
shift # past argument
shift # past value
;;
*) # unknown option
if [ $# -eq 1 ]; then
message="$1"
else
echo "Unknown option $key or missing message!" >&2
echo "$usage" >&2
exit 1
fi
break
;;
esac
done
if [ -z "$channel" ]; then
channel="$default_channel"
fi
if [ "$#" -eq 0 ]; then
echo "$usage" >&2
exit 1
fi
curl -s -u $LOGIN:$PASSWORD \
-d "$message" "$SERVER/${LOGIN}_$channel"