#!/bin/bash

set -e

APP_DIR="/usr/share/stackfit"
VENV_DIR="$APP_DIR/venv"

if [[ ! -d "$APP_DIR" ]]; then
    echo "ERROR: StackFit installation directory does not exist:"
    echo "$APP_DIR"
    exit 1
fi

if [[ ! -x "$VENV_DIR/bin/python" ]]; then
    echo "ERROR: StackFit Python virtual environment is missing:"
    echo "$VENV_DIR"
    echo ""
    echo "Try reinstalling the package."
    exit 1
fi

cd "$APP_DIR"

export FLASK_APP="app/main.py"
export FLASK_ENV="production"
export PYTHONUNBUFFERED="1"

exec "$VENV_DIR/bin/python" "$APP_DIR/app/main.py" "$@"
