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

11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
  1. #!/bin/bash
  2. ## Send a notification with NTFY and check if the config file is complete
  3. if [[ "$UID" == "0" ]]; then
  4. NTFY_CONFIG_FILE="/etc/ntfy/ntfy.conf"
  5. else
  6. NTFY_CONFIG_FILE="$HOME/.config/ntfy/ntfy.conf"
  7. fi
  8. SERVER="https://ntfy.0k.io/"
  9. if ! [ -e "$NTFY_CONFIG_FILE" ]; then
  10. mkdir -p "${NTFY_CONFIG_FILE%/*}"
  11. ## default option to change if needed
  12. echo "SERVER=$SERVER" > "$NTFY_CONFIG_FILE"
  13. ## else if $NTFY_CONFIG_FILE exist but SERVER is not defined
  14. elif ! grep -q "^SERVER=" "$NTFY_CONFIG_FILE"; then
  15. echo "SERVER=$SERVER" >> "$NTFY_CONFIG_FILE"
  16. fi
  17. source "$NTFY_CONFIG_FILE"
  18. for var in SERVER LOGIN PASSWORD; do
  19. if ! [ -v "$var" ]; then
  20. echo "Error: missing $var in $NTFY_CONFIG_FILE"
  21. exit 1
  22. fi
  23. done
  24. exname=${0##*/}
  25. default_channel="main"
  26. usage="Usage: $exname [-c CHANNEL] MESSAGE
  27. ----------------------------------------------
  28. --- Send MESSAGE to the specified CHANNEL. ---
  29. ----------------------------------------------
  30. If no CHANNEL is provided, the message will be sent to the default channel
  31. Default CHANNEL is format as follow : ConfiguredLOGIN_${default_channel}"
  32. while [[ $# -gt 0 ]]; do
  33. key="$1"
  34. case $key in
  35. -c|--channel)
  36. channel="$2"
  37. message="$3"
  38. shift # past argument
  39. shift # past value
  40. ;;
  41. *) # unknown option
  42. if [ $# -eq 1 ]; then
  43. message="$1"
  44. else
  45. echo "Unknown option $key or missing message!" >&2
  46. echo "$usage" >&2
  47. exit 1
  48. fi
  49. break
  50. ;;
  51. esac
  52. done
  53. if [ -z "$channel" ]; then
  54. channel="$default_channel"
  55. fi
  56. if [ "$#" -eq 0 ]; then
  57. echo "$usage" >&2
  58. exit 1
  59. fi
  60. curl -s -u $LOGIN:$PASSWORD \
  61. -d "$message" "$SERVER/${LOGIN}_$channel"