#!/bin/bash

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

# Install dependencies
apt-get -y --no-install-recommends install activemq openjdk-8-jre-headless

# Download Tomcat 8.0
wget http://mirror.dkm.cz/apache/tomcat/tomcat-8/v8.0.47/bin/apache-tomcat-8.0.47.tar.gz -O /tmp/apache-tomcat-8.tgz
tar xf /tmp/apache-tomcat-8.tgz -C /srv
mv /srv/apache-tomcat-8.0.47 /srv/motech
rm -f /tmp/apache-tomcat-8.tgz

# Download Motech
wget 'http://nexus.motechproject.org/service/local/artifact/maven/redirect?r=releases&g=org.motechproject&a=motech-platform-server&v=RELEASE&e=war' -O /tmp/motech.war
unzip /tmp/motech.war -d /srv/motech/webapps/motech
rm -f /tmp/motech.war

# Update Postgres JDBC driver
rm -f /srv/motech/webapps/motech/WEB-INF/lib/postgresql-9.1-901.jdbc4.jar
rm -f /srv/motech/webapps/motech/WEB-INF/bundles/postgresql-9.1-901.jdbc4.jar
wget https://jdbc.postgresql.org/download/postgresql-42.1.4.jar -O /srv/motech/webapps/motech/WEB-INF/lib/postgresql-42.1.4.jar
cp /srv/motech/webapps/motech/WEB-INF/lib/postgresql-42.1.4.jar /srv/motech/webapps/motech/WEB-INF/bundles/postgresql-42.1.4.jar

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

# Configure ActiveMQ
cp ${SOURCE_DIR}/etc/activemq/instances-available/main/activemq.xml /etc/activemq/instances-available/main/activemq.xml
ln -s /etc/activemq/instances-available/main /etc/activemq/instances-enabled/main
systemctl restart activemq

# Configure Motech bootstrap
mkdir -p /srv/motech/.motech/config/org.motechproject.motech-platform-email
envsubst <${SOURCE_DIR}/srv/motech/.motech/config/bootstrap.properties >/srv/motech/.motech/config/bootstrap.properties
cp ${SOURCE_DIR}/srv/motech/.motech/config-locations.properties /srv/motech/.motech/config-locations.properties
cp ${SOURCE_DIR}/srv/motech/.motech/config/motech-settings.properties /srv/motech/.motech/config/motech-settings.properties
cp ${SOURCE_DIR}/srv/motech/.motech/config/org.motechproject.motech-platform-email/motech-email.properties /srv/motech/.motech/config/org.motechproject.motech-platform-email/motech-email.properties

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

# Configure Tomcat
cp ${SOURCE_DIR}/srv/motech/conf/server.xml /srv/motech/conf/server.xml
cp ${SOURCE_DIR}/lib/systemd/system/motech.service /lib/systemd/system/motech.service
systemctl daemon-reload
systemctl enable motech
systemctl start motech

# Configure Motech admin
export MOTECH_ADMIN_USER="admin"
export MOTECH_ADMIN_EMAIL="admin@example.com"
export MOTECH_ADMIN_PWD=$(head -c 12 /dev/urandom | base64)
until $(curl -s http://127.0.0.1:9081/motech/module/server/startup/ | grep -q adminLogin); do
    sleep 1
done
curl -H "Content-Type: application/json" -X POST -d "{\"adminLogin\":\"${MOTECH_ADMIN_USER}\",\"adminEmail\":\"${MOTECH_ADMIN_EMAIL}\",\"adminPassword\":\"${MOTECH_ADMIN_PWD}\",\"adminConfirmPassword\":\"${MOTECH_ADMIN_PWD}\",\"language\":\"cs\",\"providerName\":\"\",\"providerUrl\":\"\",\"schedulerUrl\":\"\"}" http://127.0.0.1:9081/motech/module/server/startup/

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

# Restart services
systemctl restart nginx

# Add portal application definition
portal-app-manager motech "/motech/" "${MOTECH_ADMIN_USER}" "${MOTECH_ADMIN_PWD}"
