#!/bin/bash

ACTIVEMQ_START_DELAY=10

CHIPSTER_HOME="$(dirname "$(readlink -f ${BASH_SOURCE[0]})")"
# platform, should be linux-x86-32 or linux-x86-64, autodetected if not set
PLATFORM=""


## detect if platform not set
if [ ! $PLATFORM ]; then
    ARCH=`arch`
    if [ "$ARCH" = "i686" ]; then
	PLATFORM="linux-x86-32"
    elif [ "$ARCH" = "x86_64" ]; then
	PLATFORM="linux-x86-64"
    else
	echo "Could not detect hardware architecture, please set platform manually."
	exit -1;
    fi
fi


start() {
	$CHIPSTER_HOME/activemq/bin/$PLATFORM/activemq start
    echo Waiting for activemq to start...
    sleep $ACTIVEMQ_START_DELAY

    $CHIPSTER_HOME/auth/bin/$PLATFORM/chipster-auth start
    $CHIPSTER_HOME/comp/bin/$PLATFORM/chipster-comp start
    $CHIPSTER_HOME/fileserver/bin/$PLATFORM/chipster-fileserver start
}

stopit() {
    $CHIPSTER_HOME/auth/bin/$PLATFORM/chipster-auth stop

    $CHIPSTER_HOME/activemq/bin/$PLATFORM/activemq stop
    $CHIPSTER_HOME/comp/bin/$PLATFORM/chipster-comp stop
    $CHIPSTER_HOME/fileserver/bin/$PLATFORM/chipster-fileserver stop
}

status() {
    $CHIPSTER_HOME/activemq/bin/$PLATFORM/activemq status

    $CHIPSTER_HOME/auth/bin/$PLATFORM/chipster-auth status
    $CHIPSTER_HOME/comp/bin/$PLATFORM/chipster-comp status
    $CHIPSTER_HOME/fileserver/bin/$PLATFORM/chipster-fileserver status
}

case "$1" in

    'start')
        start
        ;;
    'stop')
        stopit
        ;;
    'restart')
        stopit
        start
        ;;
    'status')
        status
        ;;
    *)
        echo "Usage: chipster { start | stop | restart | status }"
        exit 1
        ;;
esac


exit 0
