#!/usr/bin/make -f

#
# This variable holds the architecture for the `host' - system on which program
# will be run.
#
DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
#
# This variable holds the architecture for the `build' - compilation system.
#
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

#jam invokation with target processor architecture
JAM := jam -sIPLINUXARCH=$(DEB_HOST_GNU_TYPE)

#
# `clean' rule ought to clean all artifacts of package build and turn package
# directory back to state it was after source unpacking.
#
clean:
	dh_clean # cleans all debhelper temporary files
	$(JAM) clean
	rm -rf build/
	rm -rf debian/help_out/
	rm -f build-stamp
	rm -f debian/gargoyle.postinst
	rm -f debian/garglk.ini
	rm -f debian/vkbd/gargoyle.ini

#
# `install' rule is not strictly necessary, but often exist as a prerequisite of
# `binary-indep' and `binary-arch' rules. It should install built software to
# some directory.
#
# If the only one binary package is built from source package, then `install'
# rule usually install all files to iplinux/<package name>, and `binary-*' rules
# do not touch files.
#
# If more than one binary package is build from source package, then `install'
# rule is used to install files to iplinux/tmp directory, and `binary-*' rules
# are used to split one directory to several.
#
install: build debian/garglk.ini debian/gargoyle.postinst debian/vkbd/gargoyle.ini help
	dh_prep
	dh_installdirs
	
	$(JAM) install  # to build/dist/

#
# Install mime info for application/x-ifiction - fictitious mime type for all IF supported by gargoyle
#
debian/gargoyle.postinst: debian/gargoyle.postinst.in garglk/launcher.c
	debian/ifiction_file_exts $^ > $@

#
# garglk.ini for Open Inkpot - font size is twice bigger
#
debian/garglk.ini: garglk/garglk.ini debian/garglk.ini.patch
	patch -u -o $@ $^

#
# gargoyle.ini for Open Inkpot - key binding
# TODO: choose between n516 and v3 based on target arch
#
debian/vkbd/gargoyle.ini: debian/vkbd/gargoyle.n516.ini
	cp $^ $@

help:
	mkdir -p debian/help_out
	oi-help-compiler -u debian/help debian/help_out

#
# `build' rule is also not strictly necessary, but often exist as a prerequisite
# of `install' rule. It should perform building of package, but should not
# install anything to the iplinux/
#
# This way dpkg-buildpackage -aarmel will generate package for arm-linux-gnueabi,
# and not a ordinary PC package.
#
build: build-stamp
build-stamp:
	# compile the package.
	$(JAM)
	touch $@

#
# `binary-indep' rule is required. It should produce all Arch: all binary
# packages necessary.
#
# `hello' package does not have any Arch: all packages, so this rule is empty.
#
binary-indep: install

#
# `binary-arch' rule is required: It should produce all binary packages which
# are not Arch: all
#
# Note that unlike Debian, no documentation, examples, manpages or changelogs are
# to be packaged. If upstream make system installs them, they should be ignored
# during packaging.
#
binary-arch: install
	dh_install -s #copy files as specified in install file
	dh_strip -s # strips the debugging information (optionally to separate -dbg package)
	dh_fixperms -s # ajusts permissions
	dh_installdeb -s # installs metadata (triggers, maint. scripts etc)
	dh_shlibdeps -s # generates shared library dependencies
	dh_gencontrol -s # generates and installs binary package control file
	dh_builddeb -s # makes a .deb file from iplinux/<package>

#
# This target is required and should just build all binary packages.
#
binary: binary-indep binary-arch

.PHONY: binary binary-arch binary-indep clean
