#!/usr/bin/with-contenv bash
# shellcheck shell=bash

# if user is using our env variables set the DB_URL
if [[ -n ${DB_HOST+x} ]]; then
    export CMD_DB_URL="mysql://${DB_USER}:${DB_PASS}@${DB_HOST}:${DB_PORT}/${DB_NAME}"
fi

# set env var for sqlite if db url and db_host unset
if [[ -z ${CMD_DB_URL+x} ]] && [[ -z ${CMD_DB_HOST+x} ]]; then
    export CMD_DB_URL="sqlite:///config/hedgedoc.sqlite"
fi

# set config path
if [[ -z ${CMD_CONFIG_FILE+x} ]]; then
    export CMD_CONFIG_FILE="/config/config.json"
fi

# run program
exec \
    s6-notifyoncheck -d -n 300 -w 1000 \
        cd /app/hedgedoc s6-setuidgid abc node app.js 
