#!/bin/sh

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

# Build Docker container
docker build -t openmapkit ${SOURCE_DIR}

# Configure OpenMapKit
export OMK_ADMIN_USER="admin"
export OMK_ADMIN_PWD=$(head -c 12 /dev/urandom | base64)
mkdir -p /srv/openmapkit/data/deployments
mkdir /srv/openmapkit/data/submissions
envsubst <${SOURCE_DIR}/srv/openmapkit/settings.js >/srv/openmapkit/settings.js
chown -R 8007:8007 /srv/openmapkit/data

# Configure OpenMapKit service
cp ${SOURCE_DIR}/etc/init.d/openmapkit /etc/init.d/openmapkit
rc-update add openmapkit boot
service openmapkit start

# Create nginx app definition
cp ${SOURCE_DIR}/etc/nginx/apps/openmapkit /etc/nginx/apps/openmapkit
cp ${SOURCE_DIR}/etc/nginx/conf.d/openmapkit.conf /etc/nginx/conf.d/openmapkit.conf

# Reload nginx configuration
service nginx reload

# Add portal application definition
portal-app-manager openmapkit "https://{host}:8407/" admin "${OMK_ADMIN_PWD}"
portal-app-manager openmapkit-clients -p clienturl "http://{host}:8007"
