# Check if Python 3 is installed
PYTHON3_EXISTS := $(shell which python3)

# Determine which Python executable to use
ifdef PYTHON3_EXISTS
    PYTHON := python3
else
    $(error "Error: Python 3 is not installed.")
endif

# Get the original user who invoked sudo
ORIGINAL_USER := $(shell echo $$SUDO_USER)

ifdef MAC
 INSTALL_PREFIX ?= /usr/local
else
 INSTALL_PREFIX ?= /usr
endif


# Virtual environment path
ifndef PACKAGE_BUILD
ifdef PY_SIL_VENV
	VENV_ACTIVATE := $(PY_SIL_VENV)/bin/activate
	ACTIVATE_CMD := . $(VENV_ACTIVATE)

	INSTALL_PYANG := $(shell $(ACTIVATE_CMD) && $(PYTHON) -m pip install pyang)

	# Check if pyang is installed
	PYANG_EXISTS := $(shell $(ACTIVATE_CMD) && $(PYTHON) -c "import pyang; print(pyang.__path__[0])")

	# Detect the pyang directory
	PYANG_PLUGIN_DIR := $(shell $(ACTIVATE_CMD) && $(PYTHON) -c 'import pyang; print(pyang.__path__[0])')
else
	INSTALL_PYANG := $(shell $(PYTHON) -m pip install pyang)

	# Check if pyang is installed
	PYANG_EXISTS := $(shell $(PYTHON) -c 'import pyang; print(pyang.__path__[0])')

	# Detect the pyang directory
	PYANG_PLUGIN_DIR := $(shell $(PYTHON) -c 'import pyang; print(pyang.__path__[0])')
endif
endif


.PHONY: all install uninstall clean superclean



all:


install:

    ifdef PYANG_EXISTS
		@echo "pyang is installed."
    else
		@echo "Error: pyang is not installed install it using pip install pyang."
		@exit 1
    endif

	# Copy the plugin file to the pyang plugins directory
	install -D -m 644 py-sil-gen.py $(DESTDIR)$(PYANG_PLUGIN_DIR)/plugins/py-sil-gen.py

uninstall:
	# Remove the plugin file from the pyang plugins directory
	PYANG_PLUGIN_DIR := $(shell $(PYTHON) -c 'import pyang; print(pyang.__path__[0])')
	rm -f $(DESTDIR)$(PYANG_PLUGIN_DIR)/plugins/py-sil-gen.py


clean:


superclean:
