#!/bin/ksh
######################################################################
# Program:	growlnotify (a replacement)
# Purpose:	Translate growlnotify calls into notification center messages.
# Author:	Perette Barella
# Caveats:	Does not handle long-form options passed to growlnotify.
#---------------------------------------------------------------------



arg0="$(basename "$0")"
DEBUG=true

wait=false
sticky=false
identifier=""
priority=0
appicon=""
message=""
title=""

while getopts 'hsvi:I:a:p:H:u:P:n:m:t:d:?' option
do
	case "$option" in
		P|H)
			print "$arg0: Remote options (-P and -H) not supported." 1>&2
			exit 1
		;;
		w)
			wait=true
			;;
		d)
			identifier="$OPTARG"
			;;
		s)
			sticky=true
			;;
		p)
			priority="$OPTARG"
			# ZZZ Should sanity check number 0-9
			;;
		I)
			icon="$OPTARG"
			;;
		a)
			appicon="$OPTARG"
			;;
		m)
			message="$OPTARG"
			;;
		t)
			title="$OPTARG"
			;;
	esac
done
shift $(($OPTIND - 1))
subtitle="$*"

# display notification "message" with title "title" subtitle "subtitle"

if whence terminal-notifier >/dev/null
then
	options=""
	[ "$title" ] && options="-title \"$title\""
	[ "$message" ] && options="$options -message \"$message\""
	[ "$subtitle" ] && options="$options -subtitle \"$subtitle\""
	[ "$identifier" ] && options="$options -group \"$identifier\""
	$DEBUG && print "Executing: terminal-notifier $options"
	eval "terminal-notifier $options"
else
	script="display notification \"$message\""
	[ "$title" != "" ] &&
		script="$script with title \"$title\""
	if [ "$subtitle" != "" ]
	then
		[ "$title" = "" ] && script="$script with title \"$title\""
		[ "$title" != "" ] && script="$script subtitle \"$subtitle\""
	fi
	$DEBUG && print "Script: $script"
	osascript -e "$script"

fi

