################################################################################
## Main targets:
##    tx-clean              - Remove all `.tx` files and supporting compile information
##    tx                    - Main compile target
##    tx-all                - Compile all `.tl` files in one tethysl invocation
##    tx1                   - Compile only any modified `.tl` files
##    tl-lrauv-info         - Capture info from LRAUV sources to support `.tl` compilation.
##    tl-publish-lrauv-info - Capture and publish the LRAUV definitions.
##    tl-prettify           - Prettify all `.tl` files
##    tl-check-pretty       - Check format of all `.tl` files
##    tl-lint               - Lint all `.tl` files
################################################################################

TETHYSL := tethysl

## Add to the PATH in case we need to install TethysL:
export PATH := $(PATH):tethysl_downloads/installed/bin

## Supporting target to install TethysL if needed:
tl-tethysl:
	@if ! which tethysl &>/dev/null; then \
	  if [ ! -x tethysl_downloads/installed/bin/tethysl 2> /dev/null ]; then \
	    ./tethysl-install.sh; \
	    fi \
	fi

## Unconditional removal of all `.tx` files and supporting compile information
tx-clean:
	-$(RM) -vf $(shell find ../Missions -name \*.tx)
	-$(RM) -rf tl-lrauv-info
	-$(RM) -rf tethysl_downloads

TL_FILES := $(shell find ../Missions -name '*.tl')
TX_FILES := $(patsubst ../Missions/%.tl, ../Missions/%.tx, $(TL_FILES))

## TethysL uses key LRAUV information from theses sources:
NEEDED_LRAUV_FILES := $(shell find ../Source -name '*IF.h' -or -name '*Module.cpp' -or -name 'Units.cpp' -or -name 'UniversalURI.cpp')

## Auxiliary target that allows to capture the LRAUV information such that
## subsequent compilation of mission files is performed more efficiently:
tl-lrauv-info: tl-tethysl $(NEEDED_LRAUV_FILES)
	@echo "Capturing information from LRAUV sources for TethysL"
	$(TETHYSL) parse-lrauv-info --lrauv=.. --dest=tl-lrauv-info
	@echo 'Finished capturing information from LRAUV sources for TethysL.'
	@echo ' '

## To perform an efficient dispatch the main `tx` target is defined depending
## on whether or not the LRAUV information has already been captured:
ifeq ($(wildcard tl-lrauv-info/.),)
tx: tx-all  # no captured LRAUV info, so do everything
else
tx: tx1     # LRAUV info available, do regular one-by-one
endif

## The `tx-all` target is for unconditional compilation of all TethysL mission files.
## This is the fastest option when compiling all or a good part of the mission file set.
tx-all: tl-tethysl tl-lrauv-info
	@echo "Compiling: $(TL_FILES)"
	$(TETHYSL) compile --lrauv-info=tl-lrauv-info --mission-dir=../Missions $(TL_FILES)
	@echo 'Finished compiling all `.tl` files.'
	@echo ' '

## The `tx1` target is for a more typical one-by-one dispatch in makefiles.
## So, this is useful when one wants to recompile only any modified `.tl` sources.
tx1: tl-lrauv-info $(TX_FILES)
../Missions/%.tx: ../Missions/%.tl
	@echo 'Compiling: $<'
	$(TETHYSL) compile --lrauv-info=tl-lrauv-info --mission-dir=../Missions "$<"
	@echo 'Finished compiling: $<'
	@echo ' '

tl-prettify: tl-lrauv-info
	@echo 'Prettifying all `.tl` files ...'
	$(TETHYSL) prettify --lrauv-info=tl-lrauv-info --mission-dir=../Missions --autogenerated --update --all
	@echo 'Finished prettifying `.tl` files.'
	@echo ' '

tl-check-pretty: tl-lrauv-info
	@echo 'Checking format of all `.tl` files ...'
	$(TETHYSL) prettify --lrauv-info=tl-lrauv-info --mission-dir=../Missions --autogenerated --all
	@echo 'Finished checking format of `.tl` files.'
	@echo ' '

tl-lint: tl-lrauv-info
	@echo 'Linting all `.tl` files ...'
	$(TETHYSL) lint --lrauv-info=tl-lrauv-info --mission-dir=../Missions --all
	@echo 'Finished linting `.tl` files.'
	@echo ' '

## tl-publish-lrauv-info generates and publishes LRAUV definitions for external TethysL users.
## Such definitions get generated under `/var/www/html/tethys/defs` in a subdirectory named after the current
## commit hash, that is, under `/var/www/html/tethys/defs/<hash>/`.
## A soft link from `/var/www/html/tethys/defs/<branch-name>` to that directory is also created.
## This target is to support external TethysL users not having access to the lrauv-application codebase
## but needing updates in the information (new settings, behaviors, etc.)
tl-publish-lrauv-info: tl-tethysl
	@echo "Capturing/publishing LRAUV definitions for external TethysL users"
	$(TETHYSL) parse-lrauv-info --lrauv=.. --dest=/var/www/html/tethys/defs/$$(git rev-parse HEAD)
	@branch=$$(git branch --show-current); \
	if [ "$$branch" != "" ]; then \
		noSlashes=$$(echo $$branch | tr / _); \
		ln -Tsf /var/www/html/tethys/defs/$$(git rev-parse HEAD) /var/www/html/tethys/defs/$$noSlashes; \
		echo "Link updated: /var/www/html/tethys/defs/$$noSlashes -> /var/www/html/tethys/defs/$$(git rev-parse HEAD)"; \
	fi
	@echo 'Finished capturing/publishing LRAUV definitions for external TethysL users.'
	@echo ' '
