#!/bin/sh
#
# Script to start OpenWorkouts
#

# Full path to where we run the script from
current=`pwd`
# Full path to the env
env_path=${current}/env

# trap control+c, which is what we use to stop pserve, ensuring ZEO is shut down
trap ctrl_c INT

ctrl_c() {
    ./bin/uwsgi_stop
    ./bin/zeo_stop
}

# Activate the virtual env
echo "Activating virtual environment"
. ${env_path}/bin/activate

# Now ensure all python and js packages are up-to-date
echo "Installing dependency updates (this may take a moment)"
yes | pip install --upgrade -e ${current}[testing] > /tmp/ow-start.stdout 2> /tmp/ow-start.stderr
./bin/js_deps > /tmp/ow-start.stdout 2> /tmp/ow-start.stderr

# Start ZEO (stop if it is running)
echo "Starting database"
./bin/zeo_stop >> var/log/zeo_stop.log 2>> var/log/zeo_stop.err &
./bin/zeo_start >> var/log/zeo_start.log 2>> var/log/zeo_start.err &

# And finally run the server
echo "Starting OpenWorkouts"
./bin/uwsgi_start development

# show the url where the app is accesible
ow_url=http://`grep http etc/uwsgi_development.ini |cut -f 3 -d " "`
echo "Open this url in your browser to access the app: ${ow_url}"
# open ${ow_url}
