#!/bin/sh
# =============================================================================
# epc670-setup-venv
# Creates the epc670 Python virtual environment, sets group ownership, and
# installs required pip packages (skips gracefully if offline).
#
# Usage:
#   epc670-setup-venv
#
# venv_dir: /opt/venv/epc670
# =============================================================================
set -e

VENV_DIR="/opt/venv/epc670"
VENV_GROUP="epc670"
REQUIREMENTS="/usr/share/epc670-tofhat-utils/requirements.txt"

log() {
  echo "epc670-setup-venv: $*"
}

# -----------------------------------------------------------------------------
# Group
# -----------------------------------------------------------------------------
if ! getent group "$VENV_GROUP" >/dev/null 2>&1; then
  groupadd --system "$VENV_GROUP"
  log "Created group $VENV_GROUP"
else
  log "Group $VENV_GROUP already exists"
fi

# -----------------------------------------------------------------------------
# Venv parent directory
# -----------------------------------------------------------------------------
if [ ! -d "$VENV_DIR" ]; then
  mkdir -p "$VENV_DIR"
  log "Created directory $VENV_DIR"
fi

# -----------------------------------------------------------------------------
# Virtual environment
# -----------------------------------------------------------------------------
if [ ! -f "$VENV_DIR/bin/activate" ]; then
  log "Creating Python 3.13 virtual environment at $VENV_DIR"
  python3.13 -m venv "$VENV_DIR"
else
  log "Virtual environment already exists at $VENV_DIR, skipping creation"
fi

chown -R root:"$VENV_GROUP" "$VENV_DIR"
chmod -R g+w "$VENV_DIR"
log "Set $VENV_DIR ownership to root:$VENV_GROUP with group-write"

# -----------------------------------------------------------------------------
# Pip packages
# -----------------------------------------------------------------------------
log "Checking network connectivity for pip installations..."
if curl -s --max-time 3 --head https://pypi.org >/dev/null 2>&1; then
  log "Network available, proceeding with pip installations"
  # Wrapped in an `if` so a mid-install failure (e.g. network drops after the
  # connectivity check) does NOT trip `set -e` and abort the calling
  # maintainer script, which would leave the package half-configured.
  if "$VENV_DIR/bin/pip" install --upgrade pip \
     && "$VENV_DIR/bin/pip" install --upgrade --upgrade-strategy eager -r "$REQUIREMENTS"; then
    log "pip installations complete"
  else
    log "Warning: pip installation failed; the Python environment may be incomplete."
    log "Re-run 'sudo epc670-setup-venv' once connectivity is restored."
  fi
else
  echo ""
  echo "╔══════════════════════════════════════════════════════════════╗"
  echo "║  WARNING: No internet connection detected                    ║"
  echo "║                                                              ║"
  echo "║  pip packages could not be installed. The Python environment ║"
  echo "║  is incomplete.                                              ║"
  echo "║                                                              ║"
  echo "║  To complete the installation later, run:                    ║"
  echo "║    sudo epc670-setup-venv                                    ║"
  echo "╚══════════════════════════════════════════════════════════════╝"
  echo ""
  # Only prompt when a human is actually attached. During an apt/dpkg run there
  # is usually no controlling terminal (and DEBIAN_FRONTEND may be
  # noninteractive), so an unguarded `read` would block the install forever.
  if [ -t 0 ] && [ "${DEBIAN_FRONTEND:-}" != "noninteractive" ]; then
    printf "Press Enter to continue..."
    read -r _
  fi
  log "Skipping pip installations due to no network"
fi

log "Virtual environment setup complete."
