#! /bin/sh

RAILS_ENV=production
RAILS_PORT=3001

SELF=`readlink -m $0`
pushd `dirname $SELF` >/dev/null
cd ..
export RAILS_ENV
ACTION=$1
if [ "$1" == "" ]; then
	ACTION=status
fi

case $ACTION in
	status )
		if [ -f tmp/pids/server.pid ]; then
			echo "Rails server is running"
		else
			echo "Rails server is NOT running"
		fi
		;;
	start )
		if [ -f tmp/pids/server.pid ]; then
			echo "Rails server is running already"
		else
			echo "Rails server start"
			bin/bundle install
			bin/rake db:migrate
			bin/rake tmp:clear
			bin/rake assets:precompile
			bin/rails s --port=$RAILS_PORT --daemon
		fi
		;;
	stop )
		if [ -f tmp/pids/server.pid ]; then
			echo "Rails server shutdown"
			kill `cat tmp/pids/server.pid`
		else
			echo "Rails server is NOT running"
		fi
		;;
	restart )
		"$SELF" stop
		"$SELF" start
		;;
esac

popd >/dev/null
