#!/usr/bin/make -f
# Made with the aid of dh_make, by Craig Small

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

DEB_BUILD_GNU_TYPE = $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE  = $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
ifneq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
CONFARGS= --host=$(DEB_HOST_GNU_TYPE)
endif

CC=gcc
INSTALL=/usr/bin/install
CFLAGS = -g -O2 -fno-strength-reduce -D_REENTRANT -D_XOPEN_SOURCE=500
ELF_CFLAGS = $(CFLAGS) -fPIC

# The original info is from src/slang.h
SOMAJOR=1
SOMINOR=4.9

LIBSLANG_NONUTF8=libslang1
LIBSLANG_NONUTF8_PIC=libslang1-pic
LIBSLANG_NONUTF8_DEV=libslang1-dev
LIBSLANG_UTF8=libslang1-utf8
LIBSLANG_UTF8_PIC=libslang1-utf8-pic
LIBSLANG_UTF8_DEV=libslang1-utf8-dev
LIBSLANG_UTF8_UDEB=$(LIBSLANG_UTF8)-udeb

DEBVERSION=$(shell dpkg-parsechangelog | grep '^Version: ' | sed -e 's/^Version: //')
UDEBNAME=$(LIBSLANG_UTF8_UDEB)_$(DEBVERSION)_$(shell dpkg --print-architecture).udeb


# the dbs rules
TAR_DIR=slang-1.4.9
include /usr/share/dbs/dbs-build.mk

ifeq (,$(DEB_BUILD_GNU_TYPE))
 include /usr/share/dbs/dpkg-arch.mk
endif
ARCH=$(shell dpkg --print-architecture)

build: 
	true;

build-nonutf8-stamp=$(STAMP_DIR)/build-nonutf8-stamp
build-utf8-stamp=$(STAMP_DIR)/build-utf8-stamp
configure-stamp=$(STAMP_DIR)/configure-stamp

BUILD_TREE_UTF8=$(BUILD_TREE)-utf8
BUILD_TREE_NONUTF8=$(BUILD_TREE)-nonutf8

$(configure-stamp): $(patched)
	dh_testdir
	cp -a $(BUILD_TREE) $(BUILD_TREE_UTF8)
	mv  $(BUILD_TREE) $(BUILD_TREE_NONUTF8)
	cd $(BUILD_TREE_NONUTF8)  && ./configure --prefix=/usr --enable-warnings 
	cd $(BUILD_TREE_UTF8)  && ./configure --prefix=/usr --enable-warnings
	touch $@

$(build-nonutf8-stamp): $(configure-stamp)
	dh_testdir
	$(MAKE) -C $(BUILD_TREE_NONUTF8) CFLAGS="$(CFLAGS)" all demos
	$(MAKE) -C $(BUILD_TREE_NONUTF8) ELF_CFLAGS="$(ELF_CFLAGS)" UTF8MODIFIER=""  prefix=/ elf
	cp $(BUILD_TREE_NONUTF8)/src/slang.h $(BUILD_TREE_NONUTF8)/slang.h
	# Produce the -pic archive for use by the bootfloppies package.
	ar cqv $(BUILD_TREE_NONUTF8)/libslang_pic.a $(BUILD_TREE_NONUTF8)/src/elfobjs/*.o 
	touch $@

$(build-utf8-stamp): $(configure-stamp)
	dh_testdir
	$(MAKE) -C $(BUILD_TREE_UTF8) CFLAGS="$(CFLAGS) -DUTF8" all demos
	$(MAKE) -C $(BUILD_TREE_UTF8) UTF8MODIFIER="-UTF8" ELF_CFLAGS="$(ELF_CFLAGS) -DUTF8"  prefix=/ elf
	# Prepend to the start
	cat debian/slang.h.prepend $(BUILD_TREE_UTF8)/src/slang.h >  $(BUILD_TREE_UTF8)/slang.h
	# Produce the -pic archive for use by the bootfloppies package.
	ar cqv $(BUILD_TREE_UTF8)/libslang_pic.a \
		$(BUILD_TREE_UTF8)/src/elfobjs/*.o 
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -rf $(SOURCE_DIR) $(STAMP_DIR)
	dh_clean

# Build architecture-independent files here.
binary-indep:
	true

# Build the non-utf8 version
binary-nonutf8: $(build-nonutf8-stamp)
	dh_testdir -a
	dh_testroot -a
	dh_installdirs -a
	dh_install -p $(LIBSLANG_NONUTF8) --sourcedir $(BUILD_TREE_NONUTF8)
	dh_install -p $(LIBSLANG_NONUTF8_PIC) --sourcedir $(BUILD_TREE_NONUTF8)
	dh_install -p $(LIBSLANG_NONUTF8_DEV) --sourcedir $(BUILD_TREE_NONUTF8)

# Build the utf8 version
binary-utf8: $(build-utf8-stamp)
	dh_testdir -a
	dh_testroot -a
	dh_installdirs -a
	dh_install -p $(LIBSLANG_UTF8) --sourcedir $(BUILD_TREE_UTF8)
	dh_install -p $(LIBSLANG_UTF8_PIC) --sourcedir $(BUILD_TREE_UTF8)
	dh_install -p $(LIBSLANG_UTF8_DEV) --sourcedir $(BUILD_TREE_UTF8)
	dh_install -p $(LIBSLANG_UTF8_UDEB)  --sourcedir $(BUILD_TREE_UTF8)
	
binary-arch: binary-nonutf8 binary-utf8
	dh_testdir 
	dh_testroot 
	dh_link -a
	dh_installdocs -a -N$(LIBSLANG_UTF8_UDEB)
	dh_installchangelogs -a -N$(LIBSLANG_UTF8_UDEB) $(BUILD_TREE_UTF8)/changes.txt
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -p$(LIBSLANG_NONUTF8) -V "$(LIBSLANG_NONUTF8) (>> 1.4.9dbs-4)"
	dh_makeshlibs -p$(LIBSLANG_UTF8) -V "${LIBSLANG_UTF8} (>> 1.4.9dbs-4)"
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a -N$(LIBSLANG_UTF8_UDEB)
	dh_md5sums -a -N$(LIBSLANG_UTF8_UDEB)
	dh_builddeb -a -N$(LIBSLANG_UTF8_UDEB)

	# don't know how to do shlibdeps
	# dh_shlibdeps
	dh_gencontrol -a -p$(LIBSLANG_UTF8_UDEB) -- -fdebian/files~
	dpkg-distaddfile $(UDEBNAME) debian-installer extra
	dh_builddeb -p$(LIBSLANG_UTF8_UDEB) --filename=$(UDEBNAME)

source diff:                                                                  
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

binary: binary-indep binary-arch
.PHONY: clean binary binary-nonutf8 binary-utf8 binary-indep binary-arch binary
