#!/bin/sh
#
# chkconfig: 2345 50 50
# description: E-MailRelay - Um proxy e armazenador SMTP flexível e portátil
# processname: emailrelay
# pidfile: /var/run/emailrelay.pid
# config: /etc/emailrelay.conf

export PATH="/sbin:/usr/sbin:/bin:/usr/bin"

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

[ -f /usr/sbin/emailrelay ] || exit 0
[ -f /etc/emailrelay.conf ] || exit 0

RETVAL=0
prog="emailrelay"
prog_args="/etc/emailrelay.conf"

start() {
    echo -n $"Starting $prog:"

    /usr/sbin/emailrelay /etc/emailrelay.conf >/dev/null 2>&1
    RETVAL=$?
    
    sleep 0.5
    if [ $RETVAL -eq 0 ] && [ -f /var/run/emailrelay/emailrelay.pid ] && kill -0 $(cat /var/run/emailrelay/emailrelay.pid) 2>/dev/null; then
        echo_success
        echo
        touch /var/lock/subsys/emailrelay
        return 0
    else
        echo_failure
        echo
        return 1
    fi
}

stop() {
    echo -n $"Shutting down $prog:"
    killproc emailrelay
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/emailrelay /var/run/emailrelay/emailrelay.pid
    return $RETVAL
}

condstop() {
    status emailrelay &>/dev/null && stop
}

restart() {
    stop
    start
}

# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  condstop)
    condstop
    ;;
  condrestart)
    status emailrelay &>/dev/null && restart
    RETVAL=$?
    ;;
  status)
    status emailrelay
    RETVAL=$?
    ;;
  restart)
    restart
    RETVAL=$?
    ;;
  reload)
    killproc emailrelay -HUP
    RETVAL=$?
    ;;
  *)
    echo "Usage: emailrelay {start|stop|status|restart|condrestart|condstop|reload}"
    exit 1
esac

exit $RETVAL