#!/usr/bin/make -f

CFLAGS = -Wall -g
VERSION = $(shell cat VERSION)
SOVERSION = $(VERSION)

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

CONTROL_FILES = \
  debian/control.in \
  debian/parrot.install.in \
  debian/parrot-minimal.install.in \
  debian/parrot-doc.install.in \
  debian/parrot-devel.install.in \
  debian/libparrot.install.in \
  debian/libparrot-dev.install.in

debian-control-stamp: $(CONTROL_FILES) VERSION
	for infile in $(CONTROL_FILES); do \
	    outfile=`echo $$infile | sed "s/\.in$$//"`; \
	    sed -e "s/@VERSION@/$(VERSION)/g" \
	        -e "s/@SOVERSION@/$(SOVERSION)/g" \
	            <$$infile >$$outfile; \
	done
	touch $@

configure: configure-stamp
configure-stamp: debian-control-stamp $(QUILT_STAMPFN)
	dh_testdir
	perl Configure.pl --prefix=/usr --mandir=/usr/share/man --disable-rpath --without-libffi --without-gmp --without-opengl --without-pcre --without-zlib
	touch configure-stamp

build: build-arch build-indep

build-arch: build-arch-stamp
build-arch-stamp: configure-stamp 
	$(MAKE) installable LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+$LD_LIBRARY_PATH:}`pwd`/blib/lib
	touch build-arch-stamp

build-indep: build-indep-stamp
build-indep-stamp: configure-stamp 
	pod2man --section=1 --release="Debian Project" --center="Debian GNU/Linux manual" docs/running.pod debian/parrot.1
	pod2man --section=1 --release="Debian Project" --center="Debian GNU/Linux manual" tools/dev/parrot-config.pir debian/parrot_config.1
	pod2man --section=1 --release="Debian Project" --center="Debian GNU/Linux manual" frontend/pbc_dump/main.c debian/pbc_dump.1
	pod2man --section=1 --release="Debian Project" --center="Debian GNU/Linux manual" frontend/pbc_disassemble/main.c debian/pbc_disassemble.1
	pod2man --section=1 --release="Debian Project" --center="Debian GNU/Linux manual" frontend/pbc_merge/main.c debian/pbc_merge.1
	pod2man --section=1 --release="Debian Project" --center="Debian GNU/Linux manual" tools/dev/pbc_to_exe.pir debian/pbc_to_exe.1
	pod2man --section=1 --release="Debian Project" --center="Debian GNU/Linux manual" ext/nqp-rx/parrot-nqp.pod debian/parrot-nqp.1
	pod2man --section=1 --release="Debian Project" --center="Debian GNU/Linux manual" compilers/opsc/README.pod debian/ops2c.1
	touch build-indep-stamp

clean: debian-control-stamp
	dh_testdir
	dh_testroot
	rm -f build-stamp build-arch-stamp build-indep-stamp configure-stamp debian-control-stamp debian/parrot.1 debian/parrot_config.1 debian/pbc_dump.1 debian/pbc_disassemble.1 debian/pbc_merge.1 debian/pbc_to_exe.1 debian/parrot-nqp.1 debian/ops2c.1 debian/libparrot$(SOVERSION).install src/main.o installable_pbc_to_exe ext/Parrot-Embed/Makefile.PL test.c test.ldo
	[ ! -f Makefile ] || $(MAKE) distclean
	dh_clean 

debian/libparrot$(SOVERSION).install: debian/libparrot.install VERSION
	cp $< $@

install: build debian/libparrot$(SOVERSION).install
	dh_testdir
	dh_testroot
	dh_clean -k 
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+$LD_LIBRARY_PATH:}`pwd`/blib/lib
	$(MAKE) install-doc DESTDIR=$(CURDIR)/debian/tmp
	dh_installdirs
	find $(CURDIR)/debian/tmp -type f
	dh_install --sourcedir=$(CURDIR)/debian/tmp --list-missing


# Build architecture-independent files here.
binary-indep: build install
	dh_testdir -i 
	dh_testroot -i
	dh_installchangelogs -i ChangeLog
	dh_installdocs -i
	dh_link -plibparrot-dev usr/lib/libparrot.so.$(SOVERSION) usr/lib/libparrot.so
	dh_compress -pparrot-doc -X.pod
	dh_compress -plibparrot-dev
	dh_fixperms -i
	chmod +x debian/parrot-devel/usr/lib/parrot/$(VERSION)/tools/dev/mk_language_shell.pl
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	dh_installchangelogs -a ChangeLog
	dh_installman -pparrot-minimal debian/parrot.1
	dh_installman -pparrot-devel debian/pbc_disassemble.1 debian/pbc_dump.1 debian/pbc_merge.1 debian/parrot_config.1 debian/pbc_to_exe.1 debian/parrot-nqp.1 debian/ops2c.1
	dh_installdocs -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -plibparrot$(SOVERSION)
	dh_shlibdeps -a -Llibparrot$(SOVERSION) -l$(CURDIR)/debian/libparrot$(SOVERSION)/usr/lib
	dh_installdeb -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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