#!/usr/bin/env bash
# Python-Umgebung für das Wärmepumpen-Sidecar anlegen bzw. aktualisieren.
# Aufruf beim Deploy: bash tools/heatpump/setup.sh ../../shared/heatpump
# Bricht das Deploy nie ab, meldet fehlende Voraussetzungen nur als Warnung.
set -u
TARGET="${1:?Zielordner fehlt}"
HERE="$(cd "$(dirname "$0")" && pwd)"

mkdir -p "$TARGET"
if [[ ! -x "$TARGET/venv/bin/pip" ]]; then
  rm -rf "$TARGET/venv"
  if ! python3 -m venv "$TARGET/venv" >/dev/null 2>&1; then
    rm -rf "$TARGET/venv"
    echo "WARNUNG Wärmepumpe: python3-venv fehlt (als root: apt-get install -y python3-venv). Übersprungen."
    exit 0
  fi
fi

if ! "$TARGET/venv/bin/pip" install --quiet --disable-pip-version-check -r "$HERE/requirements.txt"; then
  echo "WARNUNG Wärmepumpe: pip install fehlgeschlagen."
  exit 0
fi

if [[ ! -f "$TARGET/heatpumps.secrets.json" ]]; then
  echo "Hinweis Wärmepumpe: $TARGET/heatpumps.secrets.json fehlt (Vorlage: tools/heatpump/heatpumps.secrets.example.json)."
fi
echo "Wärmepumpe: Python-Umgebung bereit ($TARGET/venv)."
exit 0
