#!/bin/bash

# ntfy - Execute a command and send ntfy notification on failure
# Usage: ntfy [-t <topic>] [-m <message>] <command> [args...]
#        ntfy -m <message> [-t <topic>]  # Direct message mode

set -o pipefail

NTFY_SERVER="https://ntfy.carosi.nl"
TOPIC="terminal"
MESSAGE=""

# Parse options
while getopts "t:m:" opt; do
  case $opt in
    t)
      TOPIC="$OPTARG"
      ;;
    m)
      MESSAGE="$OPTARG"
      ;;
    \?)
      echo "Usage: ntfy [-t <topic>] [-m <message>] <command> [args...]"
      echo "       ntfy -m <message> [-t <topic>]"
      exit 1
      ;;
  esac
done
shift $((OPTIND - 1))

# If -m is provided, just send the message and exit
if [ -n "$MESSAGE" ]; then
  HOSTNAME=$(hostname -s)
  curl -s \
    -H "Title: ${HOSTNAME}" \
    -H "Priority: default" \
    -d "$MESSAGE" \
    "${NTFY_SERVER}/${TOPIC}"
  exit 0
fi

if [ "$#" -lt 1 ]; then
  echo "Usage: ntfy [-t <topic>] [-m <message>] <command> [args...]"
  echo "       ntfy -m <message> [-t <topic>]"
  exit 1
fi

# Execute the command and capture output
OUTPUT=$(mktemp)
"$@" 2>&1 | tee "$OUTPUT"
EXIT_CODE=$?

# Send notification based on exit code
COMMAND_STR="$*"
HOSTNAME=$(hostname -s)

if [ $EXIT_CODE -ne 0 ]; then
  # Get last 10 lines of output for context
  TAIL_OUTPUT=$(tail -10 "$OUTPUT")

  if [ -n "$TOPIC" ]; then
    curl -s \
      -H "Title: Command Failed on ${HOSTNAME}" \
      -H "Priority: high" \
      -H "Tags: warning" \
      -d "Command: ${COMMAND_STR}
Exit Code: ${EXIT_CODE}

Last 10 lines of output:
${TAIL_OUTPUT}" \
      "${NTFY_SERVER}/${TOPIC}"
  fi
else
  # Command succeeded - send low priority notification
  if [ -n "$TOPIC" ]; then
    curl -s \
      -H "Title: Command Succeeded on ${HOSTNAME}" \
      -H "Priority: low" \
      -H "Tags: white_check_mark" \
      -d "Command: ${COMMAND_STR}" \
      "${NTFY_SERVER}/${TOPIC}"
  fi
fi

# Cleanup
rm -f "$OUTPUT"

exit $EXIT_CODE
