#!/bin/sh
#
# $Revision: 1.1 $
# Copyright 2016 Teleflora
#
# systememail	start/stop email notifier system service
#
# chkconfig: 2345 97 03
# description: Sends an email at system start and shutdown
#
#####################################################
#                                                   #
# Send an email on system start/stop to a recepient #
#                                                   #
# Teleflora Managed Services                        #
#                                                   #
#####################################################

EMAIL="sjackson@teleflora.com;mgreen@teleflora.com"
RESTARTSUBJECT="["`hostname`"] - System Startup Complete"
SHUTDOWNSUBJECT="["`hostname`"] - System Shutdown Started"
RESTARTBODY="This is an automated message to notify you that "`hostname`" started successfully.
Start up Date and Time: "`date`
SHUTDOWNBODY="This is an automated message to notify you that "`hostname`" is shutting down.
Shutdown Date and Time: "`date`
LOCKFILE=/var/lock/subsys/systememail
RETVAL=0

# Source function library.
. /etc/init.d/functions

stop() {
    echo -n $"Sending Shutdown Email: "
    echo "${SHUTDOWNBODY}" | mutt -s "${SHUTDOWNSUBJECT}" ${EMAIL}
    sleep 10
    RETVAL=$?

    if [ ${RETVAL} -eq 0 ]
    then
	rm -f ${LOCKFILE}
	success
    else
	failure
    fi
    echo
    return ${RETVAL}
}

start() {
    echo -n $"Sending Startup Email: "
    echo "${RESTARTBODY}" | mutt -s "${RESTARTSUBJECT}" ${EMAIL}
    RETVAL=$?

    if [ ${RETVAL} -eq 0 ]
    then
	touch ${LOCKFILE}
	success
    else
	failure
    fi
    echo
    return ${RETVAL}
}

case $1 in
    stop)
	stop
	;;

    start)
	start
	;;

    *)
        echo $"Usage: $0 {start|stop}"
        exit 2
esac

exit ${RETVAL} 
