#!/bin/sh
# Starts as root only to make the mounted data folders writable by the app user, then drops
# privileges for everything else (migrations and gunicorn run as "app", never as root).
set -e

if [ "$(id -u)" = "0" ]; then
    chown -R app:app /app/data /app/uploads 2>/dev/null || true
    exec gosu app "$0" "$@"
fi

export FLASK_APP=app.main

python -m app.db_wait
flask db upgrade

SSL_ARGS=""
if [ -n "$SSL_CRT_FILE" ] && [ -n "$SSL_KEY_FILE" ]; then
    SSL_ARGS="--certfile $SSL_CRT_FILE --keyfile $SSL_KEY_FILE"
fi

exec gunicorn --bind 0.0.0.0:5000 --workers 2 --timeout 300 $SSL_ARGS app.main:app
