#!/usr/bin/make -f

# Ubuntu/Debian build rules file for OOlite

# Copyright (c) 2007-2009 Chris Crowther <hikari@hikari.org.uk>
# Copyright (c) 2009      Darren Salt <linux@youmustbejoking.demon.co.uk>
# Copyright (c) 2009      Michael Werle <micha@michaelwerle.com>
# Copyright (c) 2010-2016 Nicolas Boulenguez <nicolas@debian.org>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

POLICY_TARGETS := binary binary-arch binary-indep build build-arch \
  build-indep clean
.PHONY: $(POLICY_TARGETS)
$(POLICY_TARGETS):
	dh $@

# Debhelper, please, do not run make -f GNUmakefile without me.
.PHONY: $(addprefix override_dh_auto_, \
  configure build-arch build-indep test install clean)
# We will never call the install upstream target, as GNUstep stores
# arch-dep and arch-indep files together. Installing to debian/tmp and
# splitting afterwards is much more complex than moving them once.

######################################################################
# We use the magic of the MAKE variable instead of gs_make so that
# useful options like -n are transmitted to submakes.

include /usr/share/GNUstep/debian/config.mk
GSMAKE += GNUSTEP_MAKEFILES=$(GS_MAKE_DIR)

GSMAKE += messages=yes
# Display compilation flags to log checkers.

DEB_LDFLAGS_MAINT_PREPEND := -Wl,-z,defs -Wl,--as-needed
DEB_BUILD_MAINT_OPTIONS=hardening=+all
include /usr/share/dpkg/buildflags.mk
GSMAKE += $(foreach f,CFLAGS CPPFLAGS LDFLAGS OBJCFLAGS,"$(f)=$($(f))")

# Ignore GNUSTEP optimization setting, noopt is handled by CFLAGS.
GSMAKE += OPTFLAG=""

# Use all processors unless parallel is set in DEB_BUILD_OPTIONS.
# http://www.debian.org/doc/debian-policy/ch-source.html#s-debianrules-options
NUMJOBS := $(filter parallel=%,$(DEB_BUILD_OPTIONS))
ifneq (,$(NUMJOBS))
  GSMAKE += --jobs=$(subst parallel=,,$(NUMJOBS))
else
  GSMAKE += --jobs=$(shell getconf _NPROCESSORS_ONLN)
endif

######################################################################
OOLITE_APP := oolite.app

OOLITE_OPTS += OO_JAVASCRIPT_TRACE=no
# Forced by libmozjs185 settings

OOLITE_OPTS += DEPLOYMENT_RELEASE_CONFIGURATION=yes

override_dh_auto_build-arch: $(OOLITE_APP)
override_dh_auto_build-indep: $(OOLITE_APP)
$(OOLITE_APP):
	$(MAKE) $(GSMAKE) $(OOLITE_OPTS)
override_dh_auto_clean::
	$(MAKE) $(GSMAKE) $(OOLITE_OPTS) distclean clean
	rm -rf $(OOLITE_APP)

######################################################################
# See README.source.

$(OOLITE_APP): Resources/Binary
Resources/Binary: binary-resources
	ln -s ../binary-resources Resources/Binary
override_dh_auto_clean::
	rm -fr Resources/Binary

######################################################################
PLANET_DATA := src/Core/OOPlanetData
ICOSMESH_DIR := tools/icosmesh
ICOSMESH_EXE := $(ICOSMESH_DIR)/obj/icosmesh

$(OOLITE_APP): $(PLANET_DATA).c
$(PLANET_DATA).c: $(ICOSMESH_EXE)
	cd src/Core && ../../$<
override_dh_auto_clean::
	rm -f $(PLANET_DATA).[ch]

$(ICOSMESH_EXE):
	$(MAKE) $(GSMAKE) -C $(ICOSMESH_DIR)
override_dh_auto_clean::
	$(MAKE) $(GSMAKE) -C $(ICOSMESH_DIR) clean

######################################################################
# Regenerating the PDF gives very ugly results even with
# fonts-liberation, fonts-dejavu, fonts-opensymbol installed.
# Recommended format in Debian is HTML.
# However, we also install the sources which are often more pretty.
# AdviceForNewCommanders.doc produces a better result than .odt.
HTML_FROM_DOC := \
  Doc/AdviceForNewCommanders.html
HTML_FROM_ODT := \
  Doc/OoliteReadMe.html \
  Doc/OoliteRS.html

# Attempt to circumvent #730893.
TMP_LOFFICE_HOME := $(CURDIR)/debian/tmp_loffice_config
$(HTML_FROM_DOC) $(HTML_FROM_ODT): export XDG_CONFIG_HOME := $(TMP_LOFFICE_HOME)
$(HTML_FROM_DOC) $(HTML_FROM_ODT): $(TMP_LOFFICE_HOME)
$(TMP_LOFFICE_HOME):
	mkdir $@
override_dh_auto_clean::
	rm -fr $(TMP_LOFFICE_HOME)

override_dh_auto_build-indep: $(HTML_FROM_DOC) $(HTML_FROM_ODT)
$(HTML_FROM_DOC): %.html: %.doc
	libreoffice --headless --convert-to html --outdir $(dir $@) $<
$(HTML_FROM_ODT): %.html: %.odt
	libreoffice --headless --convert-to html --outdir $(dir $@) $<
override_dh_auto_clean::
	rm -f $(HTML_FROM_DOC) $(HTML_FROM_ODT)

RESOURCES := oolite.app/Resources
SOUNDS    := Music Sounds

.PHONY: override_dh_install
override_dh_install:
	dh_install --package=oolite-data-sounds \
          $(addprefix $(RESOURCES)/,$(SOUNDS)) usr/share/oolite
 # All other Resources/ go to oolite-data.
	dh_install --package=oolite-data \
          $(addprefix --exclude=$(RESOURCES)/,$(SOUNDS)) \
          $(RESOURCES)/* usr/share/oolite
	dh_install --package=oolite-doc \
	  $(HTML_FROM_DOC) $(subst .html,.doc,$(HTML_FROM_DOC)) \
	  $(HTML_FROM_ODT) $(subst .html,.odt,$(HTML_FROM_ODT)) \
	  usr/share/doc/oolite
	dh_install --remaining-packages

######################################################################
.PHONY: override_dh_installchangelogs
override_dh_installchangelogs:
	dh_installchangelogs --all Doc/CHANGELOG.TXT
