#!/bin/sh

# For Red Hat chkconfig
# chkconfig: - 30 80
# description: Jabber AIM Transport

PROG_UID=`id -u jive`
PROG_GID=`id -g jive`

case "$1" in
  start)
    echo "Starting pymsnt"

    mkdir -p /var/run/pymsnt
    chown $PROG_UID:$PROG_GID /var/run/pymsnt

    touch /var/log/pymsnt
    chown $PROG_UID:$PROG_GID /var/log/pymsnt

    mkdir -p /var/pymsnt
    chown $PROG_UID:$PROG_GID /var/pymsnt

    su - jive -c "/usr/lib/python2.3/site-packages/pymsnt/PyMSNt.py -b -c /etc/pymsnt/config.xml -d -D -l /var/log/pymsnt"

    if [ -d /var/lock/subsys ]; then
      touch /var/lock/subsys/pymsnt
    fi
    ;;

  stop)
    echo "Stopping pymsnt..."

    if [ -f /var/run/pymsnt/pymsnt.pid ]; then
      PID=`cat /var/run/pymsnt/pymsnt.pid`
      if [ "$PID"x != "x" ]; then
        kill `cat /var/run/pymsnt/pymsnt.pid`
      fi

      # Killing the process also removes the PID file, no need to remeove it.
      # rm -rf /var/run/pymsnt/pymsnt.pid
    fi

    if [ -f /var/lock/subsys/pymsnt ]; then
      rm /var/lock/subsys/pymsnt
    fi
    ;;
  restart)
    /etc/init.d/pymsnt stop
    sleep 3
    /etc/init.d/pymsnt start
    ;;
  help)
    cat <<HELP
   stop -- stops mail service (smtp connections refused, nothing goes out)
  start -- starts mail service (smtp connection accepted, mail can go out)
restart -- stops and restarts smtp, sends qmail-send a TERM & restarts it
   help -- prints this help message
HELP
    ;;
  *)
    echo "Usage: $0 {help|start|stop|restart}"
    exit 1
    ;;
esac

exit 0

