#!/bin/sh

#
# This script allows to launch manderlbot and control it while running
#

# $Id: manderlbot.sh.in,v 1.1 2003/08/26 14:24:21 dim Exp $

INSTALL_DIR=/usr/lib/erlang/
VERSION=0.9.1

BOOT_SCRIPT="$INSTALL_DIR/lib/manderlbot-$VERSION/priv/manderlbot"
RUN_OPTIONS="-noshell -setcookie mdb"

usage() {
    prog=`basename $1`
    echo "$prog start|stop|restart|status"
}

start() {
    erl -sname manderlbot $RUN_OPTIONS -boot $BOOT_SCRIPT
}

stop() {
    erl -sname control $RUN_OPTIONS -s mdb_control stop
}

status() {
    pid=`ps -edaf | awk '/-[s]name manderlbot/ {print $2}'`

    if [ "zz$pid" != "zz" ]; then
	erl -sname control $RUN_OPTIONS -s mdb_control status
    else
	echo "manderlbot is not running"
    fi
}

case $1 in
    start) start;;

    stop)  stop;;

    status) status;;

    restart) stop && start;;

    *) usage $0
esac