#!/bin/sh
set -ev

# Volumes
OMK_CONF="${VOLUMES_DIR}/openmapkit/omk_conf"
OMK_DATA="${VOLUMES_DIR}/openmapkit/omk_data"
OMK_LAYER="${LAYERS_DIR}/openmapkit_0.12.0-201021"

# Configure OpenMapKit
export OPENMAPKIT_ADMIN_USER="admin"
export OPENMAPKIT_ADMIN_PWD=$(head -c 12 /dev/urandom | base64 | tr -d '+/=')
install -o 108080 -g 108080 -m 750 -d ${OMK_CONF}
install -o 108080 -g 108080 -m 640 omk_conf/add-ca-cert.env ${OMK_CONF}/add-ca-cert.env
envsubst <omk_conf/settings.js | install -o 108080 -g 108080 -m 640 /dev/stdin ${OMK_CONF}/settings.js

# Copy existing files into persistent storage
cp -rp ${OMK_LAYER}/srv/openmapkit/data ${OMK_DATA}

# Register application
vmmgr register-app openmapkit omk "${OPENMAPKIT_ADMIN_USER}" "${OPENMAPKIT_ADMIN_PWD}"
