#!/data/data/com.termux/files/usr/bin/bash
set -e -u -f

SCRIPTNAME=termux-notification
show_usage () {
    echo "Usage: termux-notification [options]"
    echo "Display a system notification. Content text is specified using -c/--content or read from stdin."
    echo "Please read --help-actions for help with action arguments."
    echo "  --action action          action to execute when pressing the notification"
    echo "  --alert-once             do not alert when the notification is edited"
    echo "  --button1 text           text to show on the first notification button"
    echo "  --button1-action action  action to execute on the first notification button"
    echo "  --button2 text           text to show on the second notification button"
    echo "  --button2-action action  action to execute on the second notification button"
    echo "  --button3 text           text to show on the third notification button"
    echo "  --button3-action action  action to execute on the third notification button"
    echo "  -c/--content content     content to show in the notification. Will take precedence over stdin."
    echo "  --group group            notification group (notifications with the same group are shown together)"
    echo "  -h/--help                show this help"
    echo "  --help-actions           show the help for actions"
    echo "  -i/--id id               notification id (will overwrite any previous notification with the same id)"
    echo "  --image-path path        absolute path to an image which will be shown in the notification"
    echo "  --led-color rrggbb       color of the blinking led as RRGGBB (default: none)"
    echo "  --led-off milliseconds   number of milliseconds for the LED to be off while it's flashing (default: 800)"
    echo "  --led-on milliseconds    number of milliseconds for the LED to be on while it's flashing (default: 800)"
    echo "  --on-delete action       action to execute when the the notification is cleared"
    echo "  --ongoing                pin the notification"
    echo "  --priority prio          notification priority (high/low/max/min/default)"
    echo "  --sound                  play a sound with the notification"
    echo "  -t/--title title         notification title to show"
    echo "  --vibrate pattern        vibrate pattern, comma separated as in 500,1000,200"
    echo "  --type type              notification style to use (default/media)"
    echo "Media actions (available with --type \"media\"):"
    echo "  --media-next             action to execute on the media-next button"
    echo "  --media-pause            action to execute on the media-pause button"
    echo "  --media-play             action to execute on the media-play button"
    echo "  --media-previous         action to execute on the media-previous button"
    exit 0
}

show_help_actions () {
    echo "This help refers to the arguments to options like --action, --on-delete, --button-1-action and --media-next."
    echo
    echo "All these commands take an action string as their argument, which is fed to \`dash -c\`."
    echo "A few important things must be kept in mind when using actions:"
    echo
    echo "You should use actions that do things outside of the terminal, like --action \"termux-toast hello\"."
    echo "Anything that outputs to the terminal is useless, so the output should either be redirected (--action \"ls > ~/ls.txt\") or shown to the user in a different way (--action \"ls|termux-toast\")."
    echo
    echo "Running more than one command in a single action is as easy as"
    echo "--action \"command1; command2; command3\""
    echo "or"
    echo "--action \"if [ -e file ]; then termux-toast yes; else termux-toast no; fi\"."
    echo
    echo "For anything more complex, you should put your script in a file, make it executable, and use that as the action:"
    echo "--action ~/bin/script"
    echo
    echo "The action is run in a different environment (not a subshell). Thus your environment is lost (most notably \$PATH), and ~/.profile is not sourced either. So if you need your \$PATH you should either:"
    echo " - if the action is a script, set it explicitly in the script (e.g. export PATH=\"\$HOME/bin:\$PATH\")"
    echo " - or use something like --action \"bash -l -c 'command1; command2'\")."
}

OPT_ACTION=""
OPT_ALERT_ONCE=""
OPT_BUTTON1_ACTION=""
OPT_BUTTON1_TEXT=""
OPT_BUTTON2_ACTION=""
OPT_BUTTON2_TEXT=""
OPT_BUTTON3_ACTION=""
OPT_BUTTON3_TEXT=""
OPT_CONTENT=""
OPT_GROUP=""
OPT_ID=""
OPT_IMAGE_PATH=""
OPT_LED_COLOR=""
OPT_LED_OFF=""
OPT_LED_ON=""
OPT_MEDIA_NEXT=""
OPT_MEDIA_PAUSE=""
OPT_MEDIA_PLAY=""
OPT_MEDIA_PREVIOUS=""
OPT_ONGOING=""
OPT_ON_DELETE_ACTION=""
OPT_PRIORITY=""
OPT_SOUND=""
OPT_TITLE=""
OPT_TYPE=""
OPT_VIBRATE=""

TEMP=`busybox getopt \
     -n $SCRIPTNAME \
     -o hc:i:t: \
     --long action:,alert-once,\
button1:,button1-action:,\
button2:,button2-action:,\
button3:,button3-action:,\
content:,group:,help,help-actions,id:,image-path:,\
led-color:,led-on:,led-off:,\
media-previous:,media-next:,media-play:,media-pause:,\
on-delete:,ongoing,\
priority:,\
sound,title:,type:,vibrate: \
     -s bash \
     -- "$@"`
eval set -- "$TEMP"

while true; do
	case "$1" in
		--action) OPT_ACTION="$2"; shift 2;;
		--alert-once) OPT_ALERT_ONCE="true"; shift 1;;
		--button1) OPT_BUTTON1_TEXT="$2"; shift 2;;
		--button1-action) OPT_BUTTON1_ACTION="$2"; shift 2;;
		--button2) OPT_BUTTON2_TEXT="$2"; shift 2;;
		--button2-action) OPT_BUTTON2_ACTION="$2"; shift 2;;
		--button3) OPT_BUTTON3_TEXT="$2"; shift 2;;
		--button3-action) OPT_BUTTON3_ACTION="$2"; shift 2;;
		-c | --content) OPT_CONTENT="$2"; shift 2;;
		--group) OPT_GROUP="$2"; shift 2;;
		-h | --help) show_usage;;
		--help-actions) show_help_actions; exit 0;;
		-i | --id) OPT_ID="$2"; shift 2;;
		--image-path) OPT_IMAGE_PATH="$2"; shift 2;;
		--led-color) OPT_LED_COLOR="$2"; shift 2;;
		--led-off) OPT_LED_OFF="$2"; shift 2;;
		--led-on) OPT_LED_ON="$2"; shift 2;;
		--media-next) OPT_MEDIA_NEXT="$2"; shift 2;;
		--media-pause) OPT_MEDIA_PAUSE="$2"; shift 2;;
		--media-play) OPT_MEDIA_PLAY="$2"; shift 2;;
		--media-previous) OPT_MEDIA_PREVIOUS="$2"; shift 2;;
		--on-delete) OPT_ON_DELETE_ACTION="$2"; shift 2;;
		--ongoing) OPT_ONGOING="true"; shift 1;;
		--priority) OPT_PRIORITY="$2"; shift 2;;
		--sound) OPT_SOUND="true"; shift 1;;
		-t | --title) OPT_TITLE="$2"; shift 2;;
		--type) OPT_TYPE="$2"; shift 2;;
		--vibrate) OPT_VIBRATE="$2"; shift 2;;
		--) shift; break ;;
	esac
done

if [ $# != 0 ]; then echo "$SCRIPTNAME: too many arguments"; exit 1; fi

if [ "$OPT_ONGOING" == true ] && [ -z "$OPT_ID" ]; then
	echo "Ongoing notifications without an ID are not removable."
	echo "Please set an id with --id \"string\"."
	exit 1
fi

set --
if [ -n "$OPT_ACTION" ]; then set -- "$@" --es action "$OPT_ACTION"; fi
if [ -n "$OPT_ALERT_ONCE" ]; then set -- "$@" --ez alert-once "$OPT_ALERT_ONCE"; fi
if [ -n "$OPT_BUTTON1_ACTION" ]; then set -- "$@" --es button_action_1 "$OPT_BUTTON1_ACTION"; fi
if [ -n "$OPT_BUTTON1_TEXT" ]; then set -- "$@" --es button_text_1 "$OPT_BUTTON1_TEXT"; fi
if [ -n "$OPT_BUTTON2_ACTION" ]; then set -- "$@" --es button_action_2 "$OPT_BUTTON2_ACTION"; fi
if [ -n "$OPT_BUTTON2_TEXT" ]; then set -- "$@" --es button_text_2 "$OPT_BUTTON2_TEXT"; fi
if [ -n "$OPT_BUTTON3_ACTION" ]; then set -- "$@" --es button_action_3 "$OPT_BUTTON3_ACTION"; fi
if [ -n "$OPT_BUTTON3_TEXT" ]; then set -- "$@" --es button_text_3 "$OPT_BUTTON3_TEXT"; fi
if [ -n "$OPT_GROUP" ]; then set -- "$@" --es group "$OPT_GROUP"; fi
if [ -n "$OPT_ID" ]; then set -- "$@" --es id "$OPT_ID"; fi
if [ -n "$OPT_IMAGE_PATH" ]; then set -- "$@" --es image-path "$OPT_IMAGE_PATH"; fi
if [ -n "$OPT_LED_COLOR" ]; then set -- "$@" --es led-color "$OPT_LED_COLOR"; fi
if [ -n "$OPT_LED_OFF" ]; then set -- "$@" --ei led-off "$OPT_LED_OFF"; fi
if [ -n "$OPT_LED_ON" ]; then set -- "$@" --ei led-on "$OPT_LED_ON"; fi
if [ -n "$OPT_MEDIA_NEXT" ]; then set -- "$@" --es media-next "$OPT_MEDIA_NEXT"; fi
if [ -n "$OPT_MEDIA_PAUSE" ]; then set -- "$@" --es media-pause "$OPT_MEDIA_PAUSE"; fi
if [ -n "$OPT_MEDIA_PLAY" ]; then set -- "$@" --es media-play "$OPT_MEDIA_PLAY"; fi
if [ -n "$OPT_MEDIA_PREVIOUS" ]; then set -- "$@" --es media-previous "$OPT_MEDIA_PREVIOUS"; fi
if [ -n "$OPT_ONGOING" ]; then set -- "$@" --ez ongoing "$OPT_ONGOING"; fi
if [ -n "$OPT_ON_DELETE_ACTION" ]; then set -- "$@" --es on_delete_action "$OPT_ON_DELETE_ACTION"; fi
if [ -n "$OPT_PRIORITY" ]; then set -- "$@" --es priority "$OPT_PRIORITY"; fi
if [ -n "$OPT_SOUND" ]; then set -- "$@" --ez sound "$OPT_SOUND"; fi
if [ -n "$OPT_TITLE" ]; then set -- "$@" --es title "$OPT_TITLE"; fi
if [ -n "$OPT_TYPE" ]; then set -- "$@" --es type "$OPT_TYPE"; fi
if [ -n "$OPT_VIBRATE" ]; then set -- "$@" --ela vibrate "$OPT_VIBRATE"; fi

if [ -n "$OPT_CONTENT"  ] || [ -t 0 ]; then
	# we either have some content, so it takes precedence over STDIN
	# or have no STDIN, so we must use content even if empty
	echo "$OPT_CONTENT" | /data/data/com.termux/files/usr/libexec/termux-api Notification "$@"
else # use STDIN
	/data/data/com.termux/files/usr/libexec/termux-api Notification "$@"
fi
