
#>>>> https://stackoverflow.com/questions/53382383/makefile-cant-use-conda-activate
# Need to specify bash in order for conda activate to work.
SHELL=/bin/bash
# Note that the extra activate is needed to ensure that the activate floats env to the front of PATH
CONDA_ACTIVATE=source $$(conda info --base)/etc/profile.d/conda.sh ; conda activate ; conda activate 
# <<<<

PODMAN ?= podman
PODMAN_IMAGE ?= localhost/nih2mne:latest
CONTAINERFILE ?= container/Containerfile
SINGULARITY ?= $(shell if command -v apptainer >/dev/null 2>&1; then printf apptainer; else printf singularity; fi)
SIF_IMAGE ?= build/nih2mne.sif
DATA_DIR ?= $(CURDIR)
CONTAINER_CMD ?= bids_qa_gui.py
GUI_BACKEND ?= auto

.PHONY: podman singularity podman-gui singularity-gui

podman:
	command -v $(PODMAN) >/dev/null
	$(PODMAN) build --file $(CONTAINERFILE) --tag $(PODMAN_IMAGE) .

singularity: podman
	command -v $(SINGULARITY) >/dev/null
	mkdir -p $(dir $(SIF_IMAGE))
	@set -eu; \
	archive=$$(mktemp "$${TMPDIR:-/tmp}/nih2mne.XXXXXX"); \
	trap 'rm -f "$$archive"' EXIT HUP INT TERM; \
	$(PODMAN) save --format docker-archive --output "$$archive" $(PODMAN_IMAGE); \
	$(SINGULARITY) build --force $(SIF_IMAGE) "docker-archive:$$archive"

podman-gui: podman
	DATA_DIR="$(DATA_DIR)" GUI_BACKEND="$(GUI_BACKEND)" PODMAN="$(PODMAN)" \
		./container/launch-gui.sh podman "$(PODMAN_IMAGE)" "$(CONTAINER_CMD)"

singularity-gui: singularity
	DATA_DIR="$(DATA_DIR)" GUI_BACKEND="$(GUI_BACKEND)" SINGULARITY="$(SINGULARITY)" \
		./container/launch-gui.sh singularity "$(SIF_IMAGE)" "$(CONTAINER_CMD)"

install_test:
	mamba create --override-channels --channel=conda-forge --name=nih2mne_test "mne>1.6" "python<3.12"  pip -y  
	($(CONDA_ACTIVATE) nih2mne_test ; pip install -e .[testing]; pip install pytest pytest-reportlog )
	git submodule init
	git pull --recurse-submodules

install_headless_env:
	#conda install --channel=conda-forge --name=base mamba -y
	conda env remove -n nih2mne_test
	mamba create --override-channels --channel=conda-forge --name=nih2mne_test "mne=1.5" "python<3.12" "numba<0.60" "numpy<2.0"  pip "vtk>=9.2=*osmesa*" "mesalib=21.2.5" -y
	($(CONDA_ACTIVATE) nih2mne_test ; pip install -e .[testing]; pip install pytest pytest-reportlog )
	git submodule init
	git pull --recurse-submodules

install_system_requirements:
	dnf install Xvfb -y
	dnf install git git-annex -y

create_bids:
	cd nih2mne/test_data ; make_meg_bids.py -bids_dir ./BIDS -subjid_input ABABABAB -meg_input_dir 20010101 -bids_id S01 -mri_bsight MRI/ABABABAB_refaced_T1w.nii.gz -mri_bsight_elec MRI/ABABABAB_elec.txt -ignore_eroom

create_bids_afni:
	cd nih2mne/test_data ; make_meg_bids.py -bids_dir ./BIDS -subjid_input ABABABAB -meg_input_dir 20010101 -bids_id S01afni -mri_brik MRI/ABABABAB_refaced+orig.BRIK.gz -ignore_eroom


test:
	($(CONDA_ACTIVATE) nih2mne_test ; pytest -vv  )  

test_headless:
	($(CONDA_ACTIVATE) nih2mne_test ; cd nih_to_mne; pytest -vv ) #cd enigma_MEG; xvfb-run -a pytest -vv --report-log=/tmp/enigma_MEG_test_logfile.txt )

get_data:
	git submodule update --init --recursive
