#!/usr/bin/make -f

# New stable upstream release TODO:
#   s/A.B/C.D/go in debian/control.
#   Adjust debian/changelog.
#   Adjust "ver" variables below.
#   Adjust debian/guile-libs.lintian.

SHELL := /bin/bash

# Make sure the build process doesn't touch the real HOME.
export HOME := $(CURDIR)/no-trespassing

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

# This is the debhelper compatibility version to use.
export DH_COMPAT := 7

deb_src_maj_ver := 1
deb_src_min_ver := 8
deb_src_mic_ver := 8
deb_src_rev := 1

upstream_ver := $(deb_src_maj_ver).$(deb_src_min_ver).$(deb_src_mic_ver)
deb_src_ver := $(upstream_ver)+$(deb_src_rev)
deb_src_eff_ver := $(deb_src_maj_ver).$(deb_src_min_ver)
deb_pkg_basename := guile-$(deb_src_eff_ver)
deb_guile_bin_path := /usr/bin/guile-$(deb_src_eff_ver)

define deb_sub
  perl -p \
    -e "s|\@UPSTREAM_VER\@|$(upstream_ver)|go;" \
    -e "s|\@DEB_SRC_VER\@|$(deb_src_ver)|go;" \
    -e "s|\@DEB_SRC_MAJ_VER\@|$(deb_src_maj_ver)|go;" \
    -e "s|\@DEB_SRC_MIN_VER\@|$(deb_src_min_ver)|go;" \
    -e "s|\@DEB_SRC_MIC_VER\@|$(deb_src_mic_ver)|go;" \
    -e "s|\@DEB_SRC_EFF_VER\@|$(deb_src_eff_ver)|go;" \
    -e "s|\@DEB_PKG_BASENAME\@|$(deb_pkg_basename)|go;" \
      < $(1) > $(2)
endef

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

ifeq (alpha,$(shell dpkg-architecture -qDEB_HOST_ARCH))
  # The -O2 option breaks make check on alpha right now.
  deb_cflags := -Os -g
else ifeq (sh4,$(shell dpkg-architecture -qDEB_HOST_ARCH))
  # Renesas SH(sh4) needs -mieee.  Without it, test-conversion fails.
  # (Bug: 531378)
  deb_cflags := -O2 -g -mieee
else
  deb_cflags := -O2 -g
endif

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -g
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

configure_args = \
  --host=$(DEB_HOST_GNU_TYPE) \
  --build=$(DEB_BUILD_GNU_TYPE) \
  --with-threads=yes \
  --disable-error-on-warning \
  --prefix=/usr

define checkdir
  dh_testdir debian/guile.postinst
endef


build: build-stamp 
build-stamp:
	dh_testdir
	# Add here commands to compile the package.
	./configure $(configure_args)
	make -j$(shell getconf _NPROCESSORS_ONLN) all
	# --- end custom part for compiling

	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp

	# Add here commands to clean up after the build process.
#	make clean || true
	# --- end custom part for cleaning up

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Add here commands to install the package
	# The DESTDIR Has To Be Exactly  /usr/src/packages/BUILD/debian/<nameOfPackage>
	make DESTDIR=/usr/src/packages/BUILD/debian/guile-1.8 install-includeHEADERS
	make DESTDIR=/usr/src/packages/BUILD/debian/guile-1.8 install-exec
	make DESTDIR=/usr/src/packages/BUILD/debian/guile-1.8 install-data
	
	# --- end custom part for installing

# Build architecture-independent files here.
binary-indep: build install
	# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
#	dh_installdebconf
##	dh_installdocs
##	dh_installexamples
##	dh_installmenu
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installinit
##	dh_installcron
##	dh_installman
##	dh_installinfo
#	dh_undocumented
##	dh_installchangelogs
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
#	dh_makeshlibs
	dh_installdeb
#	dh_perl
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install
