1. Cameron Simpson
  2. css

Source

css / bin / alert

#!/bin/sh
#
# Write a possibly-colourised string to the alert log.
#	- Cameron Simpson <cs@zip.com.au>
#

set -ue

: ${OS:=`uname -s | tr '[A-Z]' '[a-z]'`}
: ${LOGDIR:=$HOME/var/log}
: ${ALERTLOG:=$LOGDIR/alert-local}

cmd=$0
usage="Usage: $cmd [-c colour] [-G] [-s subtitle] [-t title] {-|echo-args...]}"

colour=
growl=
tnotify=1
title=
subtitle=
case "$OS" in
  darwin)
    growl=1
    tnotify=1
    ;;
esac
if flag -w GROWL_DISABLE
then  growl=
fi

badopts=

while [ $# -gt 0 ]
do
  case $1 in
    -c) colour=$2; shift ;;
    -G) growl=1 ;;
    +G) growl= ;;
    -t) title=$2; shift ;;
    -s) subtitle=$2; shift ;;
    --) shift; break ;;
    -?*)echo "$cmd: unrecognised option: $1" >&2
        badopts=1
        ;;
    *)  break ;;
  esac
  shift
done

if [ $# = 0 ]
then
  echo "$cmd: missing \"-\" or echo-args" >&2
  badopts=1
else
  case "$*" in
    -)  set -- "`cat`" ;;
  esac
fi

[ $badopts ] && { echo "$usage" >&2; exit 2; }

xit=0

message=$*
[ -n "$subtitle" ] && set -- "$subtitle;" ${1+"$@"}
[ -n "$title" ] && set -- "$title:" ${1+"$@"}

exec >>"$ALERTLOG"

if [ -n "$colour" ]
then  colour_echo "$colour" "$*"
else  echo "$*"
fi || xit=1

dlog "ALERT: $*"

if [ $growl ]
then
  # try growlnotify, otherwise hope /dev/tty is an iTerm
  growlnotify -m "$*" || echo $'\e]9;$*\a' >/dev/tty || xit=1
fi

if [ $tnotify ]
then
  terminal-notifier -title "$title" -subtitle "$subtitle" -message "$message"
fi

exit $xit