#!/usr/bin/make -f
# Based on the initial work of Joey Hess and Craig Small.


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

PACKAGE=mmass
VERSION="4.0.0"
PYVERS=$(shell pyversions -r)
INSTALLDIR=$(CURDIR)/debian/$(PACKAGE)

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

# Using the flag below confirms the protection works fine.
# But we need not use it. Cf Debian wiki. 
# Thus setting a lintian override.
# CFLAGS += -fstack-protector-all

######################### clean #########################
.PHONY: clean
clean: 
	dh_testdir
	dh_testroot

	rm -f debian/mmass.xpm

	rm -rf $(INSTALLDIR)
	rm -rf mspy/plot/build	

	dh_clean 


######################### build #########################
build-arch-stamp: 
	dh_testdir
	dh_prep -a

	convert gui/images/gtk/icon_32.png debian/mmass.xpm

	# This will create the "calculations.so" shared object in
	# mspy/plot/build/lib.linux-i686-2.6. This shared object will
	# have to be installed as
	# /usr/lib/mmass/mspy/plot/calculations.so. Note that because
	# the build path changes according to the platform, the
	# mmass.install file has a '*' wildcard in the directory name.
	cd mspy && python setup.py build

	touch build-arch-stamp

.PHONY: build-indep
build-indep: 

.PHONY: build-arch
build-arch: build-arch-stamp

.PHONY: build
build: build-indep build-arch


######################### binary #########################
.PHONY: binary-arch
binary-arch: build-arch
	dh_testdir -a
	dh_testroot -a
	dh_installdirs -a
	dh_lintian -a
	dh_installchangelogs -a
	dh_installdocs -a
	dh_install -a
	dh_installmenu -a
	dh_python2 -a
	dh_installman -a
	dh_compress  -a
	dh_shlibdeps -a
	dh_fixperms -a
	dh_strip -a
	dh_installdeb -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

.PHONY: binary-indep
binary-indep: 
	dh_testdir -i
	dh_testroot -i
	dh_prep  -i
	dh_installdirs -i
	dh_lintian -i
	cp debian/start-script $(INSTALLDIR)/usr/bin/mmass -i
	dh_installchangelogs  -i
	dh_installdocs -i
	dh_install -i
	dh_installman -i
	dh_compress  -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

.PHONY: binary
binary: binary-indep binary-arch

get-orig-source:
	wget http://mmass.org/download/files/mmass_v$(VERSION)_source.zip
	unzip mmass_v$(VERSION)_source.zip
	rm -r mmass_v$(VERSION)_source.zip
	mv mMass mmass-upstream-$(VERSION).orig
	find mmass-upstream-$(VERSION).orig -name "mmass.tmproj" -delete
	find mmass-upstream-$(VERSION).orig -name "*.pyc" -delete
	find mmass-upstream-$(VERSION).orig -name "*.pyd" -delete
	find mmass-upstream-$(VERSION).orig -name "*.so" -delete
	rm "mmass-upstream-$(VERSION).orig/User Guide.pdf"
	rm -rf mmass-upstream-$(VERSION).orig/mspy/plot/build
	find mmass-upstream-$(VERSION).orig -type f -executable -exec chmod a-x '{}' \;
	rm -rf mmass-upstream-$(VERSION).orig/gui/images/mac/
	rm -rf mmass-upstream-$(VERSION).orig/gui/images/msw/
	tar cf - mmass-upstream-$(VERSION).orig | gzip --best > mmass_$(VERSION).orig.tar.gz
	rm -rf mmass-upstream-$(VERSION).orig
