#!/usr/bin/make -f
# -*- makefile -*-
# Simple debian/rules that uses debhelper.
# Created by Bernd Paysan, in the public domain

GFORTH_VER = @PACKAGE_VERSION@
GFORTH_ARCH = @machine@
D = $(CURDIR)/debian/gforth
DC = $(CURDIR)/debian/gforth-common
DL = $(CURDIR)/debian/gforth-lib
DLC = $(CURDIR)/debian/gforth-libcc
DLGL = $(CURDIR)/debian/gforth-libcc-gl
DLGLES = $(CURDIR)/debian/gforth-libcc-gles
DLX11 = $(CURDIR)/debian/gforth-libcc-x11
DLWL = $(CURDIR)/debian/gforth-libcc-wayland
DLM2 = $(CURDIR)/debian/gforth-libcc-minos2
DLA = $(CURDIR)/debian/gforth-libcc-audio
DB = $(CURDIR)/debian/gforth-bin
@DOHTML@DH = $(CURDIR)/debian/gforth-html
@DOPDF@DP = $(CURDIR)/debian/gforth-pdf
DV = $(CURDIR)/debian/gforth-dev
DM = $(CURDIR)/debian/gforth-minos2
DMT = $(CURDIR)/debian/gforth-minos2tools
DMA = $(CURDIR)/debian/gforth-minos2audio
DI = $(CURDIR)/debian/gforth-info
LIB = /usr/lib/$(DEB_HOST_MULTIARCH)/gforth/$(GFORTH_VER)
LIBCC = $(LIB)/$(GFORTH_ARCH)/libcc-named
SHARE = /usr/share/gforth/$(GFORTH_VER)
USHARE = $(DB)$(SHARE)
ULIB = $(DB)$(LIB)

%:
	dh $@ --builddirectory=build

override_dh_builddeb:
	dh_builddeb -- -Zxz
override_dh_auto_test:
override_dh_shlibdeps:
override_dh_auto_configure:
	dh_auto_configure -- --with-elispstartdir=/etc/emacs/site-start.d
override_dh_auto_install:
	mkdir -p $(DB)/usr/lib/gforth/site-forth
	touch $(DB)/usr/lib/gforth/site-forth/site-init.fs
	dh_auto_install -- @INSTALLHTML@ @INSTALLPDF@ DESTDIR=$(DB)
	chmod 755 $(USHARE)/filedump.fs $(USHARE)/httpd.fs $(USHARE)/sieve.fs \
	$(ULIB)/kernl*.fi $(ULIB)/gforth.fi
	mkdir -p $(DC)/usr $(DC)/usr/bin $(DL)/usr/lib $(DV)/usr/include
	mv $(DB)/usr/share $(DC)/usr
	mkdir -p $(DM)$(SHARE)/
	mv $(DC)$(SHARE)/minos2 $(DM)$(SHARE)/
	mkdir -p  $(DMT)$(SHARE)/minos2/
	mv $(DM)$(SHARE)/minos2/*-tools.fs $(DM)$(SHARE)/minos2/jpeg-exif.fs $(DMT)$(SHARE)/minos2/
	mkdir -p $(DMA)$(SHARE)/minos2/
	bash -c "mv $(DM)$(SHARE)/minos2/{opus-codec,*-audio}.fs $(DMA)$(SHARE)/minos2/"
	mv $(DB)/etc $(DC)
	mv $(DB)/usr/bin/vmgen* $(DC)/usr/bin
	mv $(DB)/usr/lib/$(DEB_HOST_MULTIARCH) $(DL)/usr/lib/$(DEB_HOST_MULTIARCH)
	mv $(DB)/usr/include/gforth.h $(DV)/usr/include
	mkdir -p $(DLC)/usr/lib/$(DEB_HOST_MULTIARCH)
	mv $(DL)/usr/lib/$(DEB_HOST_MULTIARCH)/gforth $(DLC)/usr/lib/$(DEB_HOST_MULTIARCH)
	mkdir -p $(DB)/$(LIB)
	mv $(DLC)/$(LIB)/*.f* $(DB)/$(LIB)
	mkdir -p $(DLGLES)$(LIBCC)
	-mv $(DLC)$(LIBCC)/libgfegl.* $(DLC)$(LIBCC)/libgfgles.* $(DLGLES)$(LIBCC)
	mkdir -p $(DLGLES)$(LIB)/unix
	-mv $(DLC)$(LIB)/unix/gles.fs $(DLC)$(LIB)/unix/egl.fs $(DLGLES)$(LIB)/unix
	mkdir -p $(DLGL)$(LIBCC)
	-mv $(DLC)$(LIBCC)/libgfgl*.* $(DLGL)$(LIBCC)
	mkdir -p $(DLGL)$(LIB)/unix
	-mv $(DLC)$(LIB)/unix/gl*.fs $(DLGL)$(LIB)/unix
	mkdir -p $(DLX11)$(LIBCC)
	mv $(DLC)$(LIBCC)/libgfx.* $(DLX11)$(LIBCC)
	mv $(DLC)$(LIBCC)/libgfxrandr.* $(DLX11)$(LIBCC)
	mkdir -p $(DLX11)$(LIB)/unix
	mv $(DLC)$(LIB)/unix/x.fs $(DLX11)$(LIB)/unix
	mv $(DLC)$(LIB)/unix/xrandr.fs $(DLX11)$(LIB)/unix
	mkdir -p $(DLWL)$(LIBCC)
	mv $(DLC)$(LIBCC)/libgfwayland.* $(DLWL)$(LIBCC)
	mv $(DLC)$(LIBCC)/libgfxkbcommon.* $(DLWL)$(LIBCC)
	mkdir -p $(DLWL)$(LIB)/unix
	mv $(DLC)$(LIB)/unix/wayland.fs $(DLWL)$(LIB)/unix
	mv $(DLC)$(LIB)/unix/xkbcommon.fs $(DLWL)$(LIB)/unix
	mkdir -p $(DLM2)$(SHARE)/unix
	mkdir -p $(DLM2)$(LIB)/unix
	mkdir -p $(DLM2)$(LIBCC)
	bash -c "mv $(DLC)$(LIB)/unix/{freetype_gl,harfbuzz}.fs $(DLM2)$(LIB)/unix"
	bash -c "mv $(DC)$(SHARE)/unix/{stb-image,stb-image-write}.fs $(DLM2)$(SHARE)/unix"
	bash -c "mv $(DLC)$(LIBCC)/libgf{harfbuzz,freetype_gl,stb-image,stb-image-write}.* $(DLM2)$(LIBCC)"
	mkdir -p $(DLA)$(LIB)/unix
	mkdir -p $(DLA)$(LIBCC)
	bash -c "mv $(DLC)$(LIB)/unix/{opus,pulse}.fs $(DLA)$(LIB)/unix"
	bash -c "mv $(DLC)$(LIBCC)/libgf{opus,pulse}.* $(DLA)$(LIBCC)"
	mkdir -p $(D)/usr/bin
	bash -c "mv $(DB)/usr/bin/gforth{,-fast,-itc,-ditc,mi}{,-$(GFORTH_VER)}  $(D)/usr/bin"
	mkdir -p $(DI)/usr/share
	mv $(DC)/usr/share/info $(DI)/usr/share
	rm -f $(DI)/usr/share/info/dir.gz
	@DOHTML@mkdir -p $(DH)/usr/share/doc/gforth
	@DOHTML@mkdir -p $(DH)/usr/share/doc/vmgen
	@DOHTML@mv $(DC)/usr/share/doc/gforth/html $(DH)/usr/share/doc/gforth
	@DOHTML@mv $(DC)/usr/share/doc/vmgen/html $(DH)/usr/share/doc/vmgen
	@DOPDF@mkdir -p $(DP)/usr/share/doc/gforth
	@DOPDF@mkdir -p $(DP)/usr/share/doc/vmgen
	@DOPDF@mv $(DC)/usr/share/doc/gforth/*.pdf $(DP)/usr/share/doc/gforth
	@DOPDF@mv $(DC)/usr/share/doc/vmgen/*.pdf $(DP)/usr/share/doc/vmgen

override_dh_installchangelogs:
	git log --date=format:%Y-%m-%d --pretty=format:'%cd  %cN  <%cE>%n%n%x09* %s%n' | gzip >$(DC)/usr/share/doc/gforth-common/changelog.gz
	rm -rf $(D)/usr/share/doc
	rm -rf $(DL)/usr/share/doc
	rm -rf $(DB)/usr/share/doc
	rm -rf $(DV)/usr/share/doc
	@DOPDF@rm -rf $(DP)/usr/share/doc/gforth-pdf
	@DOHTML@rm -rf $(DH)/usr/share/doc/gforth-html
