#!/usr/bin/env bash set -euo pipefail APP_DIR="${APP_DIR:-/opt/hw}" ENV_FILE="${ENV_FILE:-/etc/hw/hw.env}" SERVICE_NAME="${SERVICE_NAME:-hw}" RUN_SEED_ALL="${RUN_SEED_ALL:-0}" require_root() { if [[ "${EUID}" -ne 0 ]]; then printf 'Please run this script as root.\n' >&2 exit 1 fi } require_file() { local path="$1" if [[ ! -f "$path" ]]; then printf 'Required file not found: %s\n' "$path" >&2 exit 1 fi } main() { require_root require_file "$ENV_FILE" require_file "$APP_DIR/requirements.txt" require_file "$APP_DIR/run.py" set -a # shellcheck disable=SC1090 source "$ENV_FILE" set +a "$APP_DIR/.venv/bin/python" -m pip install --upgrade pip "$APP_DIR/.venv/bin/python" -m pip install -r "$APP_DIR/requirements.txt" export FLASK_APP=run.py export HW_CONFIG="${HW_CONFIG:-production}" (cd "$APP_DIR" && "$APP_DIR/.venv/bin/python" -m flask db upgrade) if [[ "$RUN_SEED_ALL" == "1" ]]; then (cd "$APP_DIR" && "$APP_DIR/.venv/bin/python" -m flask seed-all) fi systemctl daemon-reload systemctl restart "$SERVICE_NAME" systemctl enable "$SERVICE_NAME" printf 'Deployment completed.\n' printf 'Use: systemctl status %s --no-pager\n' "$SERVICE_NAME" } main "$@"