#!/bin/bash
#
# kuwatcher helper function: send email to one or more recipient
#
# __copy1__
# __copy2__
#
CMD=${CMD:-"$0"}
DEBUG=${DEBUG:-"false"}

[ $# != 3 ] && {
	echo "usage: $0 recipient(s) subject message" >&2
	exit 255
}

recipients=`echo "$1" | tr ',' ' '`
subject="$2"
message="$3"
stat=0

for recipient in $recipients
do
	echo -e "$message\n\n---\n   * via [$CMD] v.$CMDVERSTRING on $(uname -n)" | mail -s "$subject" $recipient
	st=$?
	logger -s -t $CMD "email $recipient, sub='$subject' msg='$message' st=$st"
	[ $st != 0 ] && stat=$?
done

exit $stat
