#!/usr/bin/make -f

# See debhelper(7) (uncomment to enable)
# output every command that modifies files on the build system.
#export DH_VERBOSE = 1

# Storm does not yet support control-flow hardening measures. In particular, PAC on
# ARM. The upcoming X86 one might be also be problematic when enabled down the line.

# Ubuntu uses lto flags. This does, however, break the garbage collector in some way
# (Storm crashes on startup), and produces interesting compile errors. Disable for now.
export DEB_BUILD_MAINT_OPTIONS = optimize=-lto hardening=+all

# Configuration variables:

# Default install dir for dh_install:
DESTDIR := debian/tmp

# Where to put shared Storm files.
STORM_SHARED := $(DESTDIR)/usr/lib/storm

# Where to store documentation.
STORM_DOC := $(DESTDIR)/usr/share/doc/storm-lang

# To have a local .mymake configuration file
MM_CONFIG := mymake.conf

# Mymake options.
MM_OPTS := dist nostatic mps

# Name of the version file in the compiler.
STORM_VERSION_FILE := Compiler/COMPILER.version

# Get variables with information about the package.
# (debhelper depends on the dpkg-dev package that contains this file, so no extra build-deps)
include /usr/share/dpkg/pkg-info.mk

# Date for the documentation.
STORM_DATE := $(shell date --utc --date="@$(DEB_TIMESTAMP)" "+%Y-%m-%d")

# Variables useful later on:

# Grab the number of threads that are desired.
PARALLEL := $(or $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS))), "1")

# How to call mymake. Note: we always supply PARALLEL in case it changes throughout.
MM := mymake -d INFO --global-config $(MM_CONFIG) -j $(PARALLEL)

# Directories to clear (all dirs that have .mymake file)
CLEAN_DIRS := $(basename $(wildcard */.mymake))

# Clean. We do everything here to not rely on the existence of our patches.
override_dh_auto_clean:
	rm -rf release/ Gc/release_mps/ $(addsuffix release/,$(CLEAN_DIRS))
	for i in $(filter-out Core/%,$(CLEAN_DIRS)); do \
	  if [ -f $${i}Gen/.gitignore ]; then \
	    rm -f $${i}Gen/*; \
	  else \
	    rm -rf $${i}Gen/; \
	  fi \
	done
	rm -f root/doc
	find root/ -name '*_doc' -print0 | xargs -0 rm -f
	find root/ -name '*.so' -print0 | xargs -0 rm -f
	rm -f $(MM_CONFIG)
	rm -f $(STORM_VERSION_FILE)
	[ ! -d mps/test/test/script/ntx86bin/ ] || rm -rf mps/test/test/script/ntx86bin/

# Create the mymake config:
$(MM_CONFIG):
	$(MM) --config


# Update the version.
$(STORM_VERSION_FILE):
	echo $(DEB_TIMESTAMP)
	date --utc --date=@$(DEB_TIMESTAMP) "+%Y-%m-%d"
	echo "core.info" > $(STORM_VERSION_FILE)
	echo -n "$(DEB_VERSION_UPSTREAM)" | sed -E 's/([^+\-]*).*/\1/' >> $(STORM_VERSION_FILE) # Plain upstream version (without +dfsg)
	echo "-$(DEB_VERSION_REVISION)+debian" >> $(STORM_VERSION_FILE) # Debian version and +debian to show it is from Debian.

override_dh_auto_build: $(MM_CONFIG) $(STORM_VERSION_FILE)
	$(MM) release $(MM_OPTS) Main

override_dh_auto_test: $(MM_CONFIG) $(STORM_VERSION_FILE)
	$(MM) release $(MM_OPTS) Test
	release/Test_mps --all

override_dh_auto_install:
	mkdir -p $(DESTDIR)/usr/bin
	cp release/Storm_mps $(DESTDIR)/usr/bin/storm
	chmod +x $(DESTDIR)/usr/bin/storm
	cp scripts/progvis $(DESTDIR)/usr/bin
	chmod +x $(DESTDIR)/usr/bin/progvis
	mkdir -p $(STORM_SHARED)
	cp -r root/doc $(STORM_SHARED)
	cp -r root/lang $(STORM_SHARED)
	cp -r root/layout $(STORM_SHARED)
	cp -r root/crypto $(STORM_SHARED)
	cp -r root/graphics $(STORM_SHARED)
	cp -r root/sound $(STORM_SHARED)
	cp -r root/parser $(STORM_SHARED)
	cp -r root/presentation $(STORM_SHARED)
	cp -r root/progvis $(STORM_SHARED)
	cp -r root/sql $(STORM_SHARED)
	cp -r root/test $(STORM_SHARED)
	cp -r root/ui $(STORM_SHARED)
	cp -r root/util $(STORM_SHARED)
	cp -r root/markdown $(STORM_SHARED)
	cp -r root/tutorials $(STORM_SHARED)
	cp -r root/http $(STORM_SHARED)
	cp -r root/json $(STORM_SHARED)
	cp -r root/experimental $(STORM_SHARED)
	cp root/res/flower-raw.ppm $(STORM_SHARED)/tutorials/s3d/res/
	release/Storm_mps -r root -f markdown.doc.generate -- --clear --date="$(STORM_DATE)" Storm storm doc $(STORM_DOC)/html
	find $(STORM_SHARED) -name "*.xcf" -exec rm \{\} \;
	find $(STORM_DOC) -name "*.xcf" -exec rm \{\} \;
	for file in res/*.png; do \
	  dest="$(DESTDIR)/usr/share/icons/hicolor/"$$(echo "$$file" | sed -E 's|res/([a-z]+)-([0-9x]*)\.png|\2/apps/org.storm_lang.\1.png|'); \
	  mkdir -p $$(dirname "$$dest"); \
	  cp "$$file" "$$dest"; \
	done


# We don't need the autotools config etc.
# These rules will find some autotools configuration files and configure them.
# This is not at all needed, and only makes it much more inconvenient to restore
# the repository into a state where we can build from again (e.g. changes inside
# submodules).
override_dh_update_autotools_config:

override_dh_autoreconf:

override_dh_auto_configure:

# Default rule.
%:
	dh $@

