#!/bin/bash

set -e
exec 2>&1

[ -r "./conf" ] && . "./conf"

cd "/srv/social"

# First-run setup
if ! [ -d ".bundle" ]; then
  su admin -c 'bundle --path=vendor'
fi
if [ -f "/srv/secure/social.db" ]; then
  su admin -c 'bundle exec rails db:{create,migrate,seed}'
fi

# Ignore the usual lock file on every run
if [ -f "./tmp/pids/server.pid" ]; then
  rm "./tmp/pids/server.pid"
fi

exec su admin -c 'bundle exec rails s -b 0.0.0.0 -p 4000'
