# Copyright (c) 2022 Carlos Peña-Monferrer. All rights reserved.
# This work is licensed under the terms of the GNU LGPL v3.0 license.
# For a copy, see <https://opensource.org/licenses/LGPL-3.0>.

.PHONY: build

SHELL := /bin/bash

version=v1.0.0-rc.8
web-wasm-image := dockcross/web-wasm:20220707-ada2c8d
web-wasm := docker run --rm --user=emscripten -it -v ${PWD}:/build -w /build $(web-wasm-image)
rom-js-image := ghcr.io/simzero-oss/rom-js:$(version)
rom-js := docker run -e TARGET=${TARGET} -it --entrypoint "" -w /work -v ${PWD}:/work $(rom-js-image)
rom-js-rom := docker run -e TARGET=${TARGET} -it -w /work -v ${PWD}:/work $(rom-js-image)
openfoam-dir := thirdparty/openfoam/etc/bashrc
data-url := https://github.com/simzero-oss/cfd-xyz-data/raw/main/surrogates_$(version).tar.gz

all: install build
native-all: native-install native-thirdparty native-thirdparty-emcc native-build native-tools


# Docker
install:
	$(rom-js) npm install
build:
	$(rom-js) /bin/bash -c "./make.sh && npm run build && \
		npm install --prefix applications/steady/online"
rom:
	$(rom-js-rom) /bin/bash -c "cd /work/examples && ./Allrun ${CORES}"
rom-clean:
	$(rom-js-rom) /bin/bash -c "cd /work/examples && ./Allclean"
data:
	$(rom-js) curl -LJ0 $(data-url) -o surrogates.tar.gz
	tar -zxvf surrogates.tar.gz -C ./


# Native
native-install:
	npm install
native-thirdparty:
	cd thirdparty && ./make_non_emcc.sh
native-thirdparty-emcc:
	$(web-wasm) /bin/bash -c "cd thirdparty && ./make_emcc.sh && cd .. && ./make.sh"
native-build:
	npm run build && \
		npm install --prefix applications/steady/online
native-tools:
	cd applications && ./make.sh
native-rom:
	source $(openfoam-dir) && cd examples && ./Allrun ${CORES}
native-rom-clean:
	source $(openfoam-dir) && cd examples && ./Allclean
native-data:
	curl -LJ0 $(data-url) -o surrogates.tar.gz
	tar -zxvf surrogates.tar.gz -C ./


pack:
	tar -zcvf surrogates_$(version).tar.gz surrogates
