post_install() {
  echo "==> Set your radio connection (serial, TCP, or BLE) in"
  echo "==>   /etc/remoteterm-meshcore/remoteterm.env"
  echo "==> Start the service with: systemctl enable --now remoteterm-meshcore"
  echo "==> The web UI will be at http://localhost:8000"
}

post_upgrade() {
  # Clean orphaned __pycache__ dirs left by the previous Python version
  find /opt/remoteterm-meshcore -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null || true

  # Skip systemd operations in chroots/containers where the binary may exist
  # but PID 1 is not systemd.
  if [ -d /run/systemd/system ] && command -v systemctl &>/dev/null; then
    systemctl daemon-reload || true
    if systemctl is-active --quiet remoteterm-meshcore; then
      systemctl restart remoteterm-meshcore || true
    fi
  fi
}

pre_remove() {
  if [ -d /run/systemd/system ] && command -v systemctl &>/dev/null; then
    systemctl disable --now remoteterm-meshcore 2>/dev/null || true
  fi
}

post_remove() {
  if [ -d /run/systemd/system ] && command -v systemctl &>/dev/null; then
    systemctl daemon-reload
  fi

  echo "==> Database and config remain in /var/lib/remoteterm-meshcore/, remoteterm user retained."
  echo "==> To fully clean up: sudo rm -rf /var/lib/remoteterm-meshcore"
}
