#!/bin/bash
### BEGIN INIT INFO
# Provides:		canvas_init
# Required-Start: 	$local_fs $remote_fs $network $syslog
# Required-Stop:	$local_fs $remote_fs $network $syslog
# Default-Start:	2 3 4 5
# Default-Stop:		0 1 6
# X-Interactive:	true
# Short-Description:	Start/stop Canvas background jobs
### END INIT INFO

set -e

# drop privs if necessary
if [ "$(id -u)" == "0" ]; then
  exec su $(stat -c %U $(dirname $(readlink -f $0))/../config/environment.rb) -c "/bin/bash $0 $@"
  exit -1;
fi

# switch to app root
cd $(dirname $(readlink -f $0))/..

# set up config
if [ -e "config/GEM_HOME" ]; then
  export GEM_HOME=$(cat config/GEM_HOME)
fi
#export GEM_HOME=/path/to/gem/home
export RAILS_ENV=production

# run delayed jobs
exec script/delayed_job $@
