#!/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-2013 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.
override_dh_auto_configure:
override_dh_auto_build-arch:
override_dh_auto_build-indep:
override_dh_auto_test:
override_dh_auto_install:
override_dh_auto_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.

# Usually, "debug=yes strip=no" would be enough, but upstream uses
# "debug" to toggle many unrelated settings, for example libmozjs
# debugging and stripping of the binary.
GSMAKE += STRIP=/bin/true

# Use Debian default build options.
include /usr/share/dpkg/buildflags.mk
GSMAKE += CFLAGS="$(CFLAGS)"
GSMAKE += CPPFLAGS="$(CPPFLAGS)"
# Fail now if order of linked libraries is wrong.
GSMAKE += LDFLAGS="-Wl,-z,defs -Wl,--as-needed $(LDFLAGS)"
GSMAKE += OBJCFLAGS="$(CFLAGS)"

# 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 += ESPEAK=no
# http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=591717

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)

######################################################################
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

######################################################################
# For AdviceForNewCommanders.doc, unoconv covers parts of the text
# with embedded images, so we do not regenerate it.
PDF_FROM_ODT := \
  Doc/OoliteReadMe.pdf \
  Doc/OoliteRS.pdf

override_dh_auto_build-indep: $(PDF_FROM_ODT)
$(PDF_FROM_ODT): %.pdf: %.odt
	libreoffice --headless --convert-to pdf --outdir $(dir $@) $<
override_dh_auto_clean::
	rm -f $(PDF_FROM_ODT)

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