#!/bin/bash
# chkconfig: 3343 80 20
# description: Maachang Comet Server(HTTPDs)...
################################################################################
# startup(Linux) maachangComet(HTTPDs).
#
# Copyright (c) 2007 masahito suzuki, Inc. All Rights Reserved.
################################################################################

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


# target directory.
EXECUTION_USER=
JAVA_HOME=
MAACHANG_COMET=
MSTART=${MAACHANG_COMET}/sh/mserver
MEND=${MAACHANG_COMET}/sh/mdown

# add projects.
# sample.
#
# TARGETS="/etc/project1/"
# TARGETS="${TARGETS} /etc/project2/"
# TARGETS="${TARGETS} /etc/project3/"
#
TARGETS=""

mstartup() {
    TARGET_DIR=$1
    if [ -f ${MSTART} ]; then
        echo "*** start mhttpd[${TARGET_DIR}]"
        su - ${EXECUTION_USER} -c "export JAVA_HOME=${JAVA_HOME}; \
            export MAACHANG_COMET=${MAACHANG_COMET};\
            cd ${TARGET_DIR}; ${MSTART} > /dev/null &"
        RETVAL=${?}
    fi
    return $RETVAL
}

mstop() {
    TARGET_DIR=$1
    if [ -f ${MEND} ]; then
        echo "*** stop mhttpd[${TARGET_DIR}]"
        su - ${EXECUTION_USER} -c "export JAVA_HOME=${JAVA_HOME}; \
            export MAACHANG_COMET=${MAACHANG_COMET};\
            cd ${TARGET_DIR}; ${MEND}"
        RETVAL=${?}
    fi
    return $RETVAL
}

startup() {
    for targetDir in $TARGETS; do
        if [ -d ${targetDir} ]; then
          mstartup ${targetDir}
        fi
    done
}

stop() {
    for targetDir in $TARGETS; do
        if [ -d ${targetDir} ]; then
          mstop ${targetDir}
        fi
    done
}

case "$1" in
'start')
        startup
        ;;
'stop')
        stop
        ;;

'restart')
        stop
        startup
        ;;
*)
        echo "Usage: $0 {start stop restart}"
        ;;
esac
exit $RETVAL
