#!/bin/bash

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

# Install dependencies for Kanboard
apt-get -y --no-install-recommends install php7.0-gd php7.0-fpm php7.0-mbstring php7.0-pgsql php7.0-zip

# Install Kanboard
wget https://github.com/kanboard/kanboard/releases/download/v1.0.47/kanboard-1.0.47.zip -O /srv/kanboard.zip
unzip /srv/kanboard.zip -d /srv
rm -f /srv/kanboard.zip

# Install plugins
git clone --depth=1 https://github.com/BlueTeck/kanboard_plugin_overwrite_translation /srv/kanboard/plugins/Overwrite_translation
git clone --depth=1 https://github.com/BlueTeck/kanboard_plugin_coverimage /srv/kanboard/plugins/Coverimage
git clone --depth=1 https://github.com/BlueTeck/kanboard_plugin_metadata /srv/kanboard/plugins/Metadata
git clone --depth=1 https://github.com/eSkiSo/Subtaskdate /srv/kanboard/plugins/Subtaskdate
git clone --depth=1 https://github.com/kanboard/plugin-budget /srv/kanboard/plugins/Budget
git clone --depth=1 https://github.com/kanboard/plugin-calendar /srv/kanboard/plugins/Calendar
git clone --depth=1 https://github.com/kanboard/plugin-chat /srv/kanboard/plugins/Chat
git clone --depth=1 https://github.com/kanboard/plugin-gantt /srv/kanboard/plugins/Gantt
git clone --depth=1 https://github.com/kanboard/plugin-gravatar /srv/kanboard/plugins/Gravatar
git clone --depth=1 https://github.com/kanboard/plugin-registration /srv/kanboard/plugins/Registration
git clone --depth=1 https://github.com/kanboard/plugin-sms-2fa /srv/kanboard/plugins/SmsTwoFactor
git clone --depth=1 https://github.com/kanboard/plugin-task-board-date /srv/kanboard/plugins/TaskBoardDate
git clone --depth=1 https://github.com/oliviermaridat/kanboard-milestone-plugin /srv/kanboard/plugins/Milestone
git clone --depth=1 https://github.com/xavividal/kanboard-plugin-relationgraph /srv/kanboard/plugins/Relationgraph

# Populate database
export KANBOARD_PWD=$(head -c 18 /dev/urandom | base64)
envsubst <${SOURCE_DIR}/tmp/kanboard-createdb.sql >/tmp/kanboard-createdb.sql
sudo -u postgres psql -f /tmp/kanboard-createdb.sql
rm -f /tmp/kanboard-createdb.sql
export PGPASSWORD=${KANBOARD_PWD}
psql -f /srv/kanboard/app/Schema/Sql/postgres.sql kanboard kanboard
unset PGPASSWORD

# Configure Kanboard
envsubst <${SOURCE_DIR}/srv/kanboard/config.php >/srv/kanboard/config.php
export KANBOARD_ADMIN_USER=admin
export KANBOARD_ADMIN_PWD=$(head -c 12 /dev/urandom | base64)
export KANBOARD_ADMIN_HASH=$(php -r "echo password_hash('${KANBOARD_ADMIN_PWD}', PASSWORD_BCRYPT);")
envsubst <${SOURCE_DIR}/tmp/kanboard-adminpwd.sql >/tmp/kanboard-adminpwd.sql
sudo -u postgres psql -f /tmp/kanboard-adminpwd.sql kanboard

# Create OS user
adduser --system --group --home /srv/kanboard --shell /bin/false kanboard
chown -R kanboard:www-data /srv/kanboard/

# Create PHP and nginx app definition
cp ${SOURCE_DIR}/etc/php/7.0/fpm/pool.d/kanboard.conf /etc/php/7.0/fpm/pool.d/kanboard.conf
cp ${SOURCE_DIR}/etc/nginx/apps-available/kanboard /etc/nginx/apps-available/kanboard
ln -s /etc/nginx/apps-available/kanboard /etc/nginx/apps-enabled/kanboard

# Restart services
systemctl restart php7.0-fpm
systemctl restart nginx

# Install cron job
cp ${SOURCE_DIR}/etc/cron.d/kanboard /etc/cron.d/kanboard

# Add portal application definition
portal-app-manager kanboard "/kanboard/" "${KANBOARD_ADMIN_USER}" "${KANBOARD_ADMIN_PWD}"
portal-app-manager kanboard-mobile
