#!/bin/sh
#
# MongoDB (from "humongous") is a scalable, high-performance, open source NoSQL database. 
#
exec=/usr/bin/mongod
prog=$(basename $exec)
configfile=/etc/mongodb/mongodb.conf
mongouser=mongodb
mongogroup=mongodb
pidfile=/var/lib/mongodb/mongod.lock

start() {
    [ -x $exec ] || exit 5
    if [ -f $pidfile ]; then
	echo "Seems that an active process is up and running with pid $(cat $pidfile)"
	echo "If this is not true try first to remove pidfile $pidfile"
	exit 5
    fi
    echo $"Starting $prog"
    
    if [ "$(grep $mongouser /etc/passwd | cut -f 7 -d :)" == "/bin/false" ]; then
	/usr/bin/chsh -s /bin/bash $mongouser
	/bin/su $mongouser -c "$exec --fork -f $configfile"
	/usr/bin/chsh -s /bin/false $mongouser
    else
	/bin/su $mongouser -c "$exec --fork -f $configfile"
    fi
}

stop() {
    if [ -e $pidfile ]; then
        echo "Stopping $prog"
	/usr/bin/chsh -s /bin/bash mongodb
        /bin/su $mongouser -c "$exec --shutdown -f $configfile"
	/usr/bin/chsh -s /bin/false mongodb
        rm $pidfile
    fi
}

status() {
    echo -n "$prog is "
    CHECK=$(ps aux | grep $exec | grep -v grep)
    STATUS=$?
    if [ "$STATUS" == "1" ]; then
	echo "not running"
    else
	echo "running"
    fi

}

restart() {
    stop
    start
}

reload() {
    restart
}

force_reload() {
    restart
}

case "$1" in
    start)
        $1
        ;;
    stop)
        $1
        ;;
    restart)
        $1
        ;;
    status)
        $1
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart}"
        exit 2
esac
exit $?

