#!/bin/sh
set -eu

export PYTHONUNBUFFERED=1
APP_ROOT="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"

export DATA_DIR="${DATA_DIR:-$APP_ROOT/data}"
export DATABASE_PATH="${DATABASE_PATH:-$DATA_DIR/putzliga.db}"
export UPLOAD_FOLDER="${UPLOAD_FOLDER:-$DATA_DIR/uploads}"
export PORT="${PORT:-8000}"

mkdir -p "$DATA_DIR" "$UPLOAD_FOLDER"

flask --app app.py init-db

exec gunicorn \
  --bind "0.0.0.0:${PORT}" \
  --workers "${GUNICORN_WORKERS:-2}" \
  --threads "${GUNICORN_THREADS:-2}" \
  --timeout "${GUNICORN_TIMEOUT:-120}" \
  "wsgi:app"
