#!/bin/bash -e

BIND="${BIND:=0.0.0.0}"
PORT="${PORT:=8080}"
RAILS_ENV="${RAILS_ENV:="development"}"
MIGRATE="${MIGRATE:="false"}"

if [ "$RAILS_ENV" = "production" ] ; then
	# @TODO Add apache config to serve the assets. Until then we have Puma serve
	#       them which is slower. This is ok if an asset host is used (SaaS).
	#       But for self-hosted installations we may want to fix that.
	export OPENPROJECT_ENABLE__INTERNAL__ASSETS__SERVER=true
fi

if [ "$MIGRATE" = "true" ]; then
	echo "Migrating database..."
	bundle exec rake db:migrate
fi

# Clean up any dangling PID file
rm -f ${APP_PATH}/tmp/pids/*

# see `config/puma.rb` for configuration
exec bundle exec rails server -u puma -b $BIND -p $PORT
