#/bin/sh
trap kill_celery INT

function kill_celery() {
    if [[ -e /tmp/omplweb.pid ]]; then
        kill `cat /tmp/omplweb.pid`
    fi
}

cd `dirname $0`
if [[ ! -e omplweb.py ]]; then
    if [[ -e ../share/ompl/webapp/omplweb.py ]]; then
        # assume we are in ${prefix}/bin, cd to ${prefix}/share/ompl/webapp
        cd ../share/ompl/webapp
    else
        echo "Error: cannot find omplweb.py"
        exit -1
    fi
fi

celerystatus=`celery status -q -C -A omplweb.celery 2>&1 | cut -f2 -d " "`
if [[ ! ${celerystatus} == "OK" ]]; then
    echo "If you want to use the benchmarking facilities of ompl_webapp,"
    echo "run the \"plannerarena\" command as well."
    celery worker -A omplweb.celery -Q omplapp --loglevel=info --pidfile=/tmp/omplweb.pid --logfile=/tmp/omplweb-celery.log &
    python ./omplweb.py
    kill_celery
else
    echo "Celery is already running an omplweb job queue."
fi
