#!/usr/bin/make -f

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

# code not written for optimizations of GCC 4.2.1 and newer
# <https://smallissimo.blogspot.com/2013/02/compiling-squeak-cog-virtual-machine-on.html>

include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/buildtools.mk
include /usr/share/dpkg/pkg-info.mk

DEB_SRCDIR = platforms/unix
DEB_BUILDDIR = build-tree
pkg = squeak-vm

# Put aside convenience code copies during build
DEB_UPSTREAM_CRUFT_MOVE = platforms/Cross/plugins/RePlugin/pcre.c \
 platforms/Cross/plugins/RePlugin/pcre.h
# all j*.[ch] except jmemd*.c and jinclude.h
jpegfiles = jcapimin.c jcapistd.c jccoefct.c jccolor.c jcdctmgr.c
jpegfiles += jchuff.c jchuff.h jcinit.c jcmainct.c jcmarker.c jcmaster.c
jpegfiles += jcomapi.c jconfig.h jcparam.c jcphuff.c jcprepct.c
jpegfiles += jcsample.c jctrans.c jdapimin.c jdapistd.c jdatadst.c
jpegfiles += jdatasrc.c jdcoefct.c jdcolor.c jdct.h jddctmgr.c jdhuff.c
jpegfiles += jdhuff.h jdinput.c jdmainct.c jdmarker.c jdmaster.c
jpegfiles += jdmerge.c jdphuff.c jdpostct.c jdsample.c jdtrans.c
jpegfiles += jerror.c jerror.h jfdctflt.c jfdctfst.c jfdctint.c
jpegfiles += jidctflt.c jidctfst.c jidctint.c jidctred.c jmemmgr.c
jpegfiles += jmemnobs.c jmemsys.h jmorecfg.h jpegint.h jpeglib.h
jpegfiles += jquant1.c jquant2.c jutils.c jversion.h
DEB_UPSTREAM_CRUFT_MOVE += \
 $(patsubst %,platforms/Cross/plugins/JPEGReadWriter2Plugin/%,$(jpegfiles))


ifneq ($(DEB_HOST_ARCH),i386)
	CONFIG_OPTS=--without-RomePlugin --without-Mpeg3Plugin
endif

# FFIPrims plugin uses assembler code unsupported on powerpcspe
ifeq ($(DEB_HOST_ARCH),powerpcspe)
	CONFIG_OPTS=--without-SqueakFFIPrims
endif

vmmversion = $(basename $(DEB_UPSTREAM_TARBALL_VERSION))
svnversion = $(subst .,,$(suffix $(DEB_UPSTREAM_TARBALL_VERSION)))
guess := $(shell /usr/share/misc/config.guess)
sub := $(shell /usr/share/misc/config.sub $(guess))
DEB_CMAKE_EXTRA_FLAGS += -DVM_HOST=$(sub)
DEB_CMAKE_EXTRA_FLAGS += -DVM_VERSION=$(vmmversion)-$(svnversion)
DEB_CMAKE_EXTRA_FLAGS += -DPLATFORM_SOURCE_VERSION=$(svnversion)
DEB_CMAKE_EXTRA_FLAGS += -DOPT--prefix=$(DEB_CMAKE_INSTALL_PREFIX)
CONFIG_OPTS += --with-npsqueak
DEB_CMAKE_EXTRA_FLAGS += $(patsubst %,-DOPT%=1,$(CONFIG_OPTS))
DEB_CMAKE_EXTRA_FLAGS += -DCMAKE_C_FLAGS="$(CFLAGS) -fno-caller-saves -fno-strict-overflow -fno-strict-aliasing -Wno-implicit-function-declaration -Wno-error=int-to-pointer-cast -Wno-error=int-conversion -Wno-error=implicit-int -Wno-error=incompatible-pointer-types -std=gnu99"

%:
	dh $@ --buildsystem=cmake --sourcedirectory=$(DEB_SRCDIR) --builddirectory=$(DEB_BUILDDIR)


override_dh_auto_configure:
	tmpdir="$(CURDIR)/tmp_upstream_cruft"; \
	mkdir -p $$tmpdir; \
	for f in $(DEB_UPSTREAM_CRUFT_MOVE); do \
		dest="$$tmpdir/$$(dirname $$f)"; \
		mkdir -p $$dest; \
		mv "$$f" "$$dest/"; \
	done
	dh_auto_configure -O--buildsystem=cmake -O--sourcedirectory=$(DEB_SRCDIR) -O--builddirectory=$(DEB_BUILDDIR) -- $(DEB_CMAKE_EXTRA_FLAGS)
	for f in $(DEB_UPSTREAM_CRUFT_MOVE); do \
		if [ -f "$$tmpdir/$$f" ]; then \
			mv "$$tmpdir/$$f" "$$(dirname $$f)/"; \
		fi; \
	done

execute_after_dh_installdocs:
	for y in debian/po/*.po; do \
	  i=`basename $$y .po`; \
	  mkdir -p debian/$(pkg)/usr/share/locale/$$i/LC_MESSAGES/; \
	  msgfmt $$y -o debian/$(pkg)/usr/share/locale/$$i/LC_MESSAGES/squeak.mo --statistics ; \
	done

execute_after_dh_install:
	rm -f debian/$(pkg)/usr/bin/squeak.sh
	mv debian/$(pkg)/usr/lib/squeak/- debian/$(pkg)/usr/lib/squeak/$(DEB_VERSION_UPSTREAM)

execute_after_dh_strip:
	$(STRIP) --remove-section=.comment debian/$(pkg)/usr/lib/squeak/*/*

override_dh_installchangelogs:
	dh_installchangelogs -p $(pkg) platforms/unix/ChangeLog
