#!/usr/bin/env bash
# Copyright 2025-2026 NXP
# SPDX-License-Identifier: BSD-3-Clause

HOME="/usr/share/eiq/aaf-connector"
VENV="$HOME/venv"
PROG="connector"

# Install if missing
if [ ! -f "$VENV/bin/activate" ] || [ ! -x "$VENV/bin/$PROG" ]; then
    echo "ℹ️ Connector is not installed."
  if ! "$HOME/install.sh"; then
    echo "❌ Failed to install the Connector."
    exit 1
  fi

  echo "✅ Connector was installed successfully."

  # Stop the service as user might pass same IP
  if systemctl is-active --quiet eiq-aaf-connector.service; then
      systemctl stop eiq-aaf-connector.service
  fi
fi

source "$VENV/bin/activate"
"$PROG" "$@"
