#!/bin/sh
#
# mode-bridge - this script starts and stops the mode-bridge client
#
# chkconfig:   2345 85 15
# description:  mode-bridge is a service for connecting data stores\
#               with modeanayltics.com
# processname: mode-bridge
# config:      /opt/mode/bridge/conf/bridge.json
# pidfile:     /opt/mode/bridge/log/mode-bridge.pid

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

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

prog="mode-bridge"
prog_runner="/opt/mode/bridge/bin/mode-bridge-wrapper"
prog_pidfile="/opt/mode/bridge/log/bridge.pid"

start() {
    [ -f $prog_config ] || exit 6
    echo -n $"Starting $prog: "
    daemon --user modeanalytics "$prog_runner" start
    retval=$?
    echo
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc -p $prog_pidfile $prog
    retval=$?
    echo
    return $retval
}

restart() {
    stop
    sleep 3
    start
}

status() {
  if [ ! -e $prog_pidfile ]; then
    echo "$prog is not running."
  fi
  pidofproc -p $prog_pidfile
}

case "$1" in
    start)
        status && exit 0
        $1
        ;;
    stop)
        status || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    status)
        status && exit 0 || exit $?
        ;;
    condrestart|try-restart)
        status || exit 7
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|reload|configtest|status|force-reload|upgrade|restart|reopen_logs}"
        exit 2
esac
