#!/bin/bash

SOURCE_DIR=$(realpath $(dirname "${0}"))/crisiscleanup

# Add NodeJS + Yarn repositories
wget https://deb.nodesource.com/gpgkey/nodesource.gpg.key -O - | apt-key add -
wget https://dl.yarnpkg.com/debian/pubkey.gpg -O - | apt-key add -
echo 'deb https://deb.nodesource.com/node_8.x stretch main' > /etc/apt/sources.list.d/nodejs.list
echo 'deb https://dl.yarnpkg.com/debian/ stable main' > /etc/apt/sources.list.d/yarn.list
apt-get -y update

# Install CrisisCleanup dependecies
apt-get -y --no-install-recommends install build-essential curl libpq-dev nodejs ruby ruby-dev yarn zlib1g-dev
gem install bundler

# Clone CrisisCleanup git repository
git clone --depth 1 https://github.com/CrisisCleanup/crisiscleanup /srv/crisiscleanup

# Fix Ruby version dependencies
sed -i 's/2\.2\.5/2.3.3/' /srv/crisiscleanup/Gemfile
sed -i 's/rdoc (4\.2\.0)/rdoc (4.3.0)/' /srv/crisiscleanup/Gemfile.lock

# Create CrisisCleanup OS user
adduser --system --group --home /srv/crisiscleanup --shell /bin/bash crisis
chown -R crisis:crisis /srv/crisiscleanup/

# Install CrisisCleanup dependencies
sudo -u crisis -i bundle install --path /srv/crisiscleanup/vendor/bundle
sudo -u crisis -i npm install
sudo -u crisis -i yarn

# Create database
export CRISISCLEANUP_PWD=$(head -c 18 /dev/urandom | base64)
envsubst <${SOURCE_DIR}/tmp/crisiscleanup-createdb.sql >/tmp/crisiscleanup-createdb.sql
sudo -u postgres psql -f /tmp/crisiscleanup-createdb.sql
rm /tmp/crisiscleanup-createdb.sql

# Configure CrisisCleanup
export CRISISCLEANUP_SECRET=$(sudo -u crisis -i /srv/crisiscleanup/bin/rake secret)
export CRISISCLEANUP_ADMIN_USER="Admin"
export CRISISCLEANUP_ADMIN_EMAIL="admin@example.com"
export CRISISCLEANUP_ADMIN_PWD=$(head -c 12 /dev/urandom | base64)
envsubst <${SOURCE_DIR}/srv/crisiscleanup/config/database.yml >/srv/crisiscleanup/config/database.yml
envsubst <${SOURCE_DIR}/srv/crisiscleanup/config/secrets.yml >/srv/crisiscleanup/config/secrets.yml
envsubst <${SOURCE_DIR}/srv/crisiscleanup/config/initializers/devise.rb >/srv/crisiscleanup/config/initializers/devise.rb
envsubst <${SOURCE_DIR}/srv/crisiscleanup/db/seeds.rb >/srv/crisiscleanup/db/seeds.rb
cp ${SOURCE_DIR}/srv/crisiscleanup/config/environments/production.rb /srv/crisiscleanup/config/environments/production.rb
cp ${SOURCE_DIR}/srv/crisiscleanup/.env /srv/crisiscleanup/.env
rm /srv/crisiscleanup/.env.test
sudo -u crisis -i /srv/crisiscleanup/bin/rake assets:precompile RAILS_ENV=production

# Populate database
sudo -u crisis -i /srv/crisiscleanup/bin/rake db:schema:load RAILS_ENV=production
sudo -u crisis -i /srv/crisiscleanup/bin/rake db:seed RAILS_ENV=production

# Create Rails service
cp ${SOURCE_DIR}/lib/systemd/system/crisiscleanup.service /lib/systemd/system/crisiscleanup.service
systemctl daemon-reload

# Create nginx site definition
cp ${SOURCE_DIR}/etc/nginx/sites-available/crisiscleanup /etc/nginx/sites-available/crisiscleanup
ln -s /etc/nginx/sites-available/crisiscleanup /etc/nginx/sites-enabled/crisiscleanup

# Restart services
systemctl start crisiscleanup
systemctl restart nginx

# Add portal application definition
portal-app-manager crisis-cleanup "https://{host}:8005/" "${CRISISCLEANUP_ADMIN_EMAIL}" "${CRISISCLEANUP_ADMIN_PWD}"
