#!/bin/bash

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

# Install packages
apt-get -y --no-install-recommends install build-essential libpq-dev python-dev python-virtualenv virtualenv zlib1g-dev

# Clone CTS git repository
git clone --depth 1 https://github.com/theirc/CTS /srv/cts

# Install CTS python virtual environment
virtualenv --no-site-packages --python=/usr/bin/python2.7 /srv/cts
/srv/cts/bin/pip install -r /srv/cts/requirements/production.txt

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

# Configure CTS
export CTS_SECRET=$(head -c 26 /dev/urandom | base64)
envsubst <${SOURCE_DIR}/srv/cts/cts/settings/spotter.py >/srv/cts/cts/settings/spotter.py
ln -s /srv/cts/lib/python2.7/site-packages/django/contrib/admin/static/admin /srv/cts/cts/static/admin
ln -s /srv/cts/lib/python2.7/site-packages/django_extensions/static/django_extensions /srv/cts/cts/static/django_extensions
ln -s /srv/cts/lib/python2.7/site-packages/django_tables2/static/django_tables2 /srv/cts/cts/static/django_tables2
ln -s /srv/cts/lib/python2.7/site-packages/leaflet/static/leaflet /srv/cts/cts/static/leaflet
ln -s /srv/cts/lib/python2.7/site-packages/rest_framework/static/rest_framework /srv/cts/cts/static/rest_framework
ln -s /srv/cts/lib/python2.7/site-packages/rest_framework_swagger/static/rest_framework_swagger /srv/cts/cts/static/rest_framework_swagger
ln -s /srv/cts/lib/python2.7/site-packages/selectable/static/selectable /srv/cts/cts/static/selectable
ln -s /srv/cts/lib/python2.7/site-packages/session_security/static/session_security /srv/cts/cts/static/session_security

# Set "production values" (increases performance) only if the DEBUG environment variable is not set
if [ ${DEBUG:-0} -eq 0 ]; then
    sed -i 's/DEBUG = True/DEBUG = False/' /srv/cts/cts/settings/spotter.py
fi

# Populate database
DJANGO_SETTINGS_MODULE=cts.settings.spotter /srv/cts/bin/python /srv/cts/manage.py migrate

# Create admin account
export CTS_ADMIN_EMAIL=admin@example.com
export CTS_ADMIN_PWD=$(head -c 12 /dev/urandom | base64)
export CTS_ADMIN_HASH=$(cd /srv/cts && DJANGO_SETTINGS_MODULE=cts.settings.spotter /srv/cts/bin/python -c "from django.contrib.auth.hashers import make_password; print make_password('${CTS_ADMIN_PWD}')")
export CTS_ADMIN_SECRET=$(head -c 12 /dev/urandom | sha256sum | cut -c1-13)
envsubst <${SOURCE_DIR}/tmp/cts-adminpwd.sql >/tmp/cts-adminpwd.sql
sudo -u postgres psql -f /tmp/cts-adminpwd.sql cts
rm -f /tmp/cts-adminpwd.sql

# Create CTS OS user
adduser --system --group --home /srv/cts --shell /bin/false cts
chown -R cts:cts /srv/cts

# Create uwsgi and nginx app definition
cp ${SOURCE_DIR}/etc/uwsgi/apps-available/cts.ini /etc/uwsgi/apps-available/cts.ini
ln -s /etc/uwsgi/apps-available/cts.ini /etc/uwsgi/apps-enabled/cts.ini
cp ${SOURCE_DIR}/etc/nginx/sites-available/cts /etc/nginx/sites-available/cts
ln -s /etc/nginx/sites-available/cts /etc/nginx/sites-enabled/cts

# Restart services
systemctl restart uwsgi
systemctl restart nginx

# Add portal application definition
portal-app-manager cts "https://{host}:8004/" "${CTS_ADMIN_EMAIL}" "${CTS_ADMIN_PWD}"
