#  Copyright (c) 2023-2024 Felix Ingrand, CNRS/LAAS
#
# All rights reserved.
#
# Redistribution  and  use  in  source  and binary  forms,  with  or  without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of  source  code must retain the  above copyright
#    notice and this list of conditions.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice and  this list of  conditions in the  documentation and/or
#    other materials provided with the distribution.
#
# THE SOFTWARE  IS PROVIDED "AS IS"  AND THE AUTHOR  DISCLAIMS ALL WARRANTIES
# WITH  REGARD   TO  THIS  SOFTWARE  INCLUDING  ALL   IMPLIED  WARRANTIES  OF
# MERCHANTABILITY AND  FITNESS.  IN NO EVENT  SHALL THE AUTHOR  BE LIABLE FOR
# ANY  SPECIAL, DIRECT,  INDIRECT, OR  CONSEQUENTIAL DAMAGES  OR  ANY DAMAGES
# WHATSOEVER  RESULTING FROM  LOSS OF  USE, DATA  OR PROFITS,  WHETHER  IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR  OTHER TORTIOUS ACTION, ARISING OUT OF OR
# IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.



# This is the ugliest Makefile I ever wrote ;-)
# I should probably rethink the whole organisation workflow to get things simplified.


# Better set these envars in your environment in .dot-whatever-file

# HIPPO_DIST=/Users/felix/Applications/hippo-2.7.0
# TINA_DIST=/Users/felix/Applications/nd.app/Contents/MacOS

HIPPO_DIST=/home/felix/work/hippo-2.8.0
TINA_DIST=/home/felix/work/tina-3.8.0

FRAC=${HIPPO_DIST}/frac
FRAC_MAKEFILE=${HIPPO_DIST}/Makefile
TINA_BIN_DIR=${TINA_DIST}/bin
BT2FCR=$(shell which bt2fiacre)

UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
LIB_EXT = so
endif
ifeq ($(UNAME_S),Darwin)
LIB_EXT = dylib
endif


# List the models for which you want the hippo version
# DIRHS= ros2-nav ros2-nav2 ros2-nav3 drone

DIRHS= drone3 drone2 drone1 

models hippo_models: $(foreach _,${DIRHS},$_-hippo.fcr $_-hippo/$_.hippo/hippo_app_data.c)

all: $(foreach _,${DIRHS},$_-hippo.fcr $_-hippo/$_.hippo/hippo_app_data.c $_-hippo/bt2f-$_-hippo)

# drone.psk: drone7.psk		#Put here the model you want to use for runtime drone1,2,3,4,5
#	cp $< $@

%-hippo.fcr: %.btf ${BT2FCR}
	${BT2FCR} \
		  -c "set tick bt"  \
		  -c "set hippo on" \
		  -c "load bt \"$<\""  \
		  -c "save fiacre \"$*-hippo/$*.fcr\"" \
		  -c "exit" foo
	mkdir -p $*-hippo/$*.hippo
	ln -s -f $*-hippo/$*.fcr $@


%/hippo_app_data.c: %/../*.fcr
	(cd $(@D)/.. ; rm -rf `basename $(<F) .fcr`.hippo; $(FRAC) -g -hippo  $(<F)  `basename $(<F) .fcr`.hippo)
#	make -C $(@D)/.. install


# List the models you want the tina verif version

DIRS= mars_rover mars_rover2 train
DIRS += ros2-nav ros2-nav7 drone1 btf1 btf2 drone2 drone3 ros2-nav3 ros2-nav2 ros2-nav4 ros2-nav5 ros2-nav6

models tina_models: $(foreach _,${DIRS},$_.fcr $_.tts/$_.${LIB_EXT})

all: $(foreach _,${DIRS},$_.fcr $_.tts/$_.ktz $_.tts/$_.sr)

%.ktz: %.${LIB_EXT}
	$(TINA_BIN_DIR)/sift -stats $(@D) -rsd $@

%.sr: %.ktz
#	$(TINA_BIN_DIR)/selt $< $(@D)/$(*F).ltl  -b 2>&1 | tee -i $@
	export PATH=$(TINA_BIN_DIR):$(PATH) ; ./$(@D)/$(*F).rch $< 2>&1 | tee -i $@

%.${LIB_EXT}: %.c
	make -f $(FRAC_MAKEFILE) FRACLIB=${HIPPO_DIST}/lib $(*F)

%.c: ../%.fcr
	$(FRAC) -j -scan -tts $(<F) $(@D)


%.fcr: %.btf ${BT2FCR}
	${BT2FCR} \
		-c "set tick Node"  \
		-c "load bt \"$<\""  \
		-c "save fiacre \"$@\"" \
		-c "exit" foo
	mkdir -p $*.tts

.PRECIOUS: %.fcr
