#!/bin/sh
# if [ "x$(id -un)" != "xgreenlight" ] ; then
#   echo "Please run this script as greenlight user" 1>&2
#   exit 1
# fi
set -e
export RAILS_ENV="${RAILS_ENV:=production}"
export LC_ALL="en_US.UTF-8"
export LANG="en_US.UTF-8"
echo "Hoping into the basedir within env $RAILS_ENV"
cd /srv/www/vhosts/greenlight/
if [ ! -e tmp/rebuild.txt ] ; then
  echo "Nothing to do for greenlight-update. Exiting"
  exit 0
fi
echo "Running rake"
/usr/bin/bundle.ruby2.7 exec /usr/bin/rake.ruby2.7 db:migrate
echo "Fixing permissions"
chown -R greenlight:greenlight tmp/
find public/backups -type d -print0 | xargs -r0 chmod u=rwx,go=x
echo "Trigger tmp/restart.txt"
touch tmp/restart.txt
echo "Finalize rebuild"
rm -f tmp/rebuild.txt
