#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

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

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
CFLAGS=-O0 -g
else
CFLAGS=-O2 -g
endif
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
MAKEFLAGS += -j$(NUMJOBS)
endif

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

deb_version := $(lastword $(shell dpkg-parsechangelog|grep "^Version: "))
version := $(shell echo $(deb_version)|sed -n 's,^\(.*\)-[^-]*$$,\1,p')
package := simgear$(version)

CMAKE_FLAGS = \
	-DCMAKE_INSTALL_PREFIX=/usr \
	-DJPEG_FACTORY=ON
CMAKE_SOFLAGS=-DSIMGEAR_SHARED=ON
CMAKE_LIBFLAGS=

configure: configure-stamp
configure-stamp:
	dh_testdir

	mkdir build-so
	cd build-so && \
	CFLAGS="$(CFLAGS)" CXXFLAGS="$(CFLAGS)" \
	cmake .. $(CMAKE_FLAGS) $(CMAKE_SOFLAGS)

	mkdir build-lib
	cd build-lib && \
	CFLAGS="$(CFLAGS)" CXXFLAGS="$(CFLAGS)" \
	cmake .. $(CMAKE_FLAGS) $(CMAKE_LIBFLAGS)

	touch configure-stamp

build-arch: configure-stamp build-stamp
build-stamp: configure-stamp
	dh_testdir

	cd build-so && $(MAKE)
	cd build-lib && $(MAKE)

	touch build-stamp

build-indep:
build: build-arch build-indep

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp

	rm -rf build-so
	rm -rf build-lib

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep

	# Install libraries and headers
	cd build-so && \
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp
	cd build-lib && \
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp

	# Install shared libraries
	dh_install -p$(package) --autodest debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/*.so.*
	dh_install -psimgear-dev --autodest debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/*.so
	# Install static libraries
	dh_install -psimgear-dev --autodest debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/*.a
	# Install headers
	dh_install -psimgear-dev --autodest debian/tmp/usr/include

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot

	dh_installdocs
	dh_installchangelogs ChangeLog
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs -V
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install configure
