#!/usr/bin/make -f
# -*- makefile -*-

include /usr/share/ocaml/ocamlvars.mk

DEB_HOST_ARCH_OS ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)

PKGNAME = libocamlnet-ocaml-dev
DESTDIR = $(CURDIR)/debian/tmp
export OCAMLFIND_DESTDIR = $(DESTDIR)$(OCAML_STDLIB_DIR)

BUILD_TARGET = all
ifeq ($(OCAML_HAVE_OCAMLOPT),yes)
BUILD_TARGET += opt
endif

CFGFLAGS =
CFGFLAGS += -enable-gtk2 -enable-ssl
CFGFLAGS += -with-nethttpd -prefer-netcgi2 -with-rpc-auth-dh
CFGFLAGS += -bindir /usr/bin -datadir /usr/share/ocamlnet
CFGFLAGS += -enable-apache -apache /usr/sbin/apache2 -apxs /usr/bin/apxs2
CFGFLAGS += -enable-pcre -enable-zip -enable-crypto

%:
	dh $@ --with ocaml,apache2

.PHONY: override_dh_auto_configure
override_dh_auto_configure:
	./configure $(CFGFLAGS)
ifeq ($(DEB_HOST_ARCH_OS),linux)
# In some build environments (especially cowbuilder and Ubuntu
# autobuilders), many netsys autotests fail even though the feature is
# available (LP: #257524)
	@echo
	@echo "The following netsys features will be forced:"
	@if grep '#undef ' src/netsys/config.h; then \
	  sed -i 's/#undef /#define /' src/netsys/config.h; \
	else \
	  echo "none"; \
	fi
	@echo
endif
ifeq ($(DEB_HOST_ARCH_OS),kfreebsd)
# No named semaphores on kfreebsd
	sed -i '/netcamlbox/d' debian/libocamlnet-ocaml*.install
	sed -i '/netmulticore/d' debian/libocamlnet-ocaml*.install
endif

.PHONY: override_dh_auto_build
override_dh_auto_build:
	$(MAKE) $(BUILD_TARGET)

.PHONY: override_dh_auto_install
override_dh_auto_install:
	$(MAKE) install DESTDIR=$(DESTDIR)

.PHONY: override_dh_install
override_dh_install:
	dh_install --fail-missing -X.so.owner -Xmod_netcgi_apache.so

.PHONY: override_dh_compress
override_dh_compress:
	dh_compress -X.ml

.PHONY: override_dh_installchangelogs
override_dh_installchangelogs:
	dh_installchangelogs ChangeLog
