# Shared settings for start.sh / stop.sh / restart.sh — do not run directly.
DIR="$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" && pwd -P)"
cd "$DIR" || exit 1
PIDFILE="$DIR/bct.pid"
LOG="$DIR/logs/app.log"
mkdir -p "$DIR/logs"

# Python: the site's own venv first, then the system Python 3.11
if   [ -x "$DIR/venv/bin/python" ];        then PY="$DIR/venv/bin/python"
elif command -v python3.11 >/dev/null 2>&1; then PY="$(command -v python3.11)"
else PY="$(command -v python3)"; fi

# Port from settings.ini ([server] port = ...), default 8085
PORT="$(awk '/^\[/{s=($0 ~ /^\[server\]/)} s && /^[ \t]*port[ \t]*=/{sub(/^[^=]*=[ \t]*/,""); sub(/[ \t;#\r].*$/,""); print; exit}' settings.ini 2>/dev/null)"
PORT="${PORT:-8085}"

# All app.py processes of this user that run from this folder
site_pids() {
  local pid cwd
  for pid in $(pgrep -u "$(id -u)" -f "app\.py" 2>/dev/null); do
    case "$(cat "/proc/$pid/comm" 2>/dev/null)" in python*) ;; *) continue ;; esac
    cwd="$(readlink -f "/proc/$pid/cwd" 2>/dev/null)"
    [ "$cwd" = "$DIR" ] && echo "$pid"
  done
}
http_code() { curl -s -o /dev/null --max-time 5 -w "%{http_code}" "http://127.0.0.1:$PORT/" 2>/dev/null; }
