|
#!/bin/bash |
|
|
|
# notify-send adapter for terminal-notify (or growlnotify), as used by |
|
# vagrant-notify <https://github.com/fgrehm/vagrant-notify#os-x> |
|
# |
|
# JSB / 2015-06-09 |
|
# ---- |
|
|
|
### BEGIN: boilerplate bash |
|
|
|
# get full path to current script/file |
|
# http://stackoverflow.com/questions/4774054/reliable-way-for-a-bash-script-to-get-the-full-path-to-itself#comment15185627_4774063 |
|
SCRIPT_PATH=$( cd $(dirname $0) ; pwd -P ) |
|
SCRIPT_NAME=`basename ${BASH_SOURCE[0]}` |
|
|
|
# stop execution if anything fails and exit with non-zero status |
|
set -o errexit # -e |
|
trap "{ echo -e \"\nError: Something went wrong in $SCRIPT_NAME. ($*)\">&2; exit 1; }" ERR |
|
|
|
# allow DEBUG env variable to trigger simple bash debugging when it is set to |
|
# anything but 'false' |
|
if [ ${DEBUG:=false} != 'false' ] ; then |
|
# useful for debugging how exactly this script is called. |
|
echo "$*" |
|
echo "" |
|
set -o verbose # -v |
|
set -o xtrace # -x |
|
fi |
|
|
|
### END: boilerplate bash |
|
|
|
|
|
# use terminal-notify for notifications (preferred) |
|
# https://github.com/alloy/terminal-notifier |
|
TERMINAL_NOTIFIER='terminal-notifier' |
|
|
|
# if no terminal notifier, try growlnotify |
|
# http://growl.info/downloads#generaldownloads |
|
GROWL_NOTIFY='growlnotify' |
|
|
|
|
|
# comes from grunt. used to detect success. |
|
SUCCESS_MSG="Done, without errors." |
|
|
|
# right now, just holds custom icons |
|
ICON_DIR="$HOME/.notify-send" |
|
|
|
# FIXME: allow overrriding filenames somehow. could be something other than |
|
# PNG, for example. |
|
ICON_SUCCESS="success.png" |
|
ICON_FAILURE="failure.png" |
|
|
|
# provide a prefix |
|
context="" |
|
|
|
|
|
# NOTE: grunt-notify thinks we're actually notify-send (due to vagrant-notify's |
|
# use of notify-send) and therefore calls us with args intended for notify-send, |
|
# so we have to jettison all the irrelevant args here. |
|
# |
|
# for more background, take a look at |
|
# https://github.com/fgrehm/vagrant-notify/blob/master/files/notify-send.erb |
|
# and the output of grunt -v to see what is actually passed to this script. |
|
# |
|
# FIXME: better way of detecting grunt-notify's call |
|
if [[ "$1" = "-h" ]]; then |
|
context="Grunt" |
|
|
|
# first, grab icon path (the fourth arg passed in) |
|
icon_path="$4" |
|
|
|
# then shift args 7 positions over to clear out unused cruft. |
|
shift 7 |
|
|
|
# grab title, which is now the first arg, and summarily shift args again |
|
title="$1" |
|
shift 1 |
|
|
|
# at last, the remaining args are the message |
|
message="$*" |
|
|
|
else |
|
# remove '--' |
|
shift 1 |
|
|
|
if [[ $# -gt 1 ]]; then |
|
# grab title, which is now the first arg, and summarily shift args again |
|
title="$1" |
|
shift 1 |
|
fi |
|
|
|
# at last, the remaining args are the message |
|
message="$*" |
|
fi |
|
|
|
|
|
# whew. all done. moving on... |
|
|
|
|
|
|
|
# set notification icon to represent pass/fail status |
|
content_img="${ICON_DIR}/${ICON_FAILURE}" |
|
|
|
if [[ "$message" = "$SUCCESS_MSG" ]]; then |
|
content_img="${ICON_DIR}/${ICON_SUCCESS}" |
|
fi |
|
|
|
|
|
# add prefix to title if there's a context |
|
if [[ "$context" ]]; then |
|
title="${context}: ${title}" |
|
fi |
|
|
|
|
|
## call terminal-notifier if it exists in PATH |
|
if [ `which $TERMINAL_NOTIFIER` ]; then |
|
$TERMINAL_NOTIFIER \ |
|
-activate "com.apple.Terminal" \ |
|
-sender "com.apple.Terminal" \ |
|
-appIcon "file://${icon_path}" \ |
|
-title "Vagrant" \ |
|
`[[ "$context" = 'Grunt' ]] && echo -e "-contentImage \"file://${content_img}\""` \ |
|
-subtitle "${title}" \ |
|
-message "${message}" |
|
|
|
## else call growlnotify if it exists in PATH |
|
elif [ `which $GROWL_NOTIFY` ]; then |
|
$GROWL_NOTIFY \ |
|
-n "Vagrant" \ |
|
-t "${title}" \ |
|
`[[ "$context" == "Grunt" ]] && echo -e "--image \"${content_img}\""` \ |
|
-d "com.apple.Terminal" \ |
|
-m "${message}" |
|
|
|
else |
|
# no tool? exit with nonzero status to signify failure. |
|
echo "Error: No notifier tool." |
|
exit 1 |
|
fi |
|
|
|
exit 0 |