#! /usr/bin/make -f

#export DH_VERBOSE=1

ifndef PERL
PERL = /usr/bin/perl
endif

ifeq ($(ORACLE_HOME),)

#warning ORACLE_HOME not set

else

ifeq (true,false)
orav = $(shell readlink $(ORACLE_HOME)/lib/libclntsh.so | sed 's|.*\.so\.\([0-9.]*\)|\1|')
else
orav = 11.1
endif
oram = $(shell echo $(orav) | cut -d. -f1)
pkg = libdbd-oracle$(oram)-perl

endif

tmp = $(PWD)/debian/$(pkg)

vars:
	@echo "orav=$(orav) oram=$(oram)"

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
  CFLAGS = -g
endif

build: build-stamp
build-stamp:
	dh_testdir
	LD_LIBRARY_PATH=$(ORACLE_HOME)/lib $(PERL) Makefile.PL -l -V $(orav) $(addflags) INSTALLDIRS=vendor
	$(MAKE) \
		OPTIMIZE="-O2 -Wall $(CFLAGS)" \
		LD_RUN_PATH="/usr/oracle/lib" \
		LD_RUN_PATH="" \
		EXTRALIBS="-L$(ORACLE_HOME)/lib -lclntsh" \
		LDLOADLIBS="-L$(ORACLE_HOME)/lib -lclntsh"
	touch $@

clean:
	dh_testdir
	! [ -f Makefile ] || $(MAKE) distclean
	rm -f *-stamp
	dh_clean


install: install-stamp
install-stamp: build
	dh_testdir
	dh_testroot
	dh_installdirs
	$(MAKE) install DESTDIR=$(tmp)
	find $(tmp)/usr -name 'dbimon*' -exec rm -f {} +
	find $(tmp)/usr/lib -name Oracle.so -exec chmod +w {} +
	touch $@

binary binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs -p$(pkg) README*
	dh_installchangelogs -p$(pkg) Changes
	dh_strip -p$(pkg)
	dh_compress -p$(pkg)
	dh_fixperms -p$(pkg)
	dh_perl -p$(pkg)
	dh_installdeb -p$(pkg)
	dh_shlibdeps -p$(pkg) -l$(ORACLE_HOME)/lib -- --ignore-missing-info
	dh_gencontrol -p$(pkg)
	dh_md5sums -p$(pkg)
	dh_builddeb -p$(pkg)

binary-indep:

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