#!/bin/bash

# Some useful functions that can be reused in sh scripts

# log: A limited logging function
#
# Usage: log ARG1 ARG2
# where ARG1: a log level (put what is sensible for you)
#       ARG2: a message (preferrably enclosed in quotes)
# The date is prepended to the log level and message
# e.g.:
#       log USELESS "A useless message" 
#    will show something like:
# 2011-04-01T08:00:00: USELESS: A useless message
function log() {
    LOGLEVEL=`printf ":%8s:" $1`
    shift
    MESSAGE=$@
    DATE=`date +"%FT%T"`
    echo "$DATE$LOGLEVEL $MESSAGE"
}

# email: Send an e-mail
#
# Usage: email SUBJECT RECIPIENT MESSAGE
# where SUBJECT: the e-mail subject
#       RECIPIENT: a valid e-mail address
#       MESSAGE: a message
# e.g.:
#       email "Monday?" mailing-list@dictators.org "Let's conquer the world"
function email() {
    SUBJECT=$1
    shift
    RECIPIENT=$1
    shift
    MESSAGE=$@
    echo "$MESSAGE" | /bin/mail -s "$SUBJECT" $RECIPIENT
}

