#!/bin/sh -eu

usage() {
	echo "Usage: makoctl <command> [options...]"
	echo ""
	echo "Commands:"
	echo "  dismiss [-a|--all]       Dismiss the last or all notifications"
	echo "  invoke [-n id] [action]  Invoke an action on the notification"
	echo "                           with the given id, or the last"
	echo "                           notification if none is given"
	echo "  list                     List notifications"
	echo "  reload                   Reload the configuration file"
	echo "  help                     Show this help"
}

call() {
	busctl -j --user call org.freedesktop.Notifications /fr/emersion/Mako \
		fr.emersion.Mako "$@"
}

if [ $# -eq 0 ] || [ $# -gt 5 ]; then
   usage
   exit 1
fi

case "$1" in
"dismiss")
	[ $# -lt 2 ] && action="" || action="$2"
	case "$action" in
	"-a"|"--all")
		call DismissAllNotifications
		;;
	"")
		call DismissLastNotification
		;;
	*)
		echo "makoctl: unrecognized option '$2'"
		exit 1
		;;
	esac
	;;
"invoke")
	id=0
	if [ $# -gt 1 ] && [ $2 == "-n" ]; then
		id="$3"
		shift 2
	fi

	action="default"
	if [ $# -gt 1 ]; then
		action="$2"
	fi

	call InvokeAction "us" "$id" "$action"
	;;
"list")
	call ListNotifications
	;;
"reload")
	call Reload
	;;
"help"|"--help"|"-h")
	usage
	;;
*)
	echo "makoctl: unrecognized command '$1'"
	exit 1
	;;
esac
