# vim: noexpandtab tabstop=8 shiftwidth=8 syntax=make
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
# WARNING: This may or may not work on your system. This Makefile is
# an example, rather than a ready-made universal solution.

#   ****************************************************************
#   **       IMPORTANT NOTE FOR SVN COMMITTERS:  READ THIS.       **
#   **                                                            **
#   ****************************************************************
#   | This Makefile is used by the bb-openbsd buildbot             |
#   | (http://ci.apache.org/builders/bb-openbsd). Please check     |
#   | the bot's health after making changes to this file.          |
#   |______________________________________________________________|

PERL ?= yes
ENABLE_PERL_BINDINGS = $(PERL)
THREADING ?= yes
ifeq ($(THREADING),yes)
JAVA ?= yes
else
JAVA ?= no
endif
ENABLE_JAVA_BINDINGS = $(JAVA)
USE_APR_ICONV ?= no # set to yes to use APR iconv instead of GNU iconv
PARALLEL ?= 1
CLEANUP ?= 1
EXCLUSIVE_WC_LOCKS ?= 1
USE_HTTPV1 ?= no
USE_AUTHZ_SHORT_CIRCUIT ?= no
RAMDISK ?= /ramdisk
MAKE_JOBS ?= 4
SVN_CHECK_FS_BACKENDS ?= fsfs bdb

PWD		= $(shell pwd)
UNAME		= $(shell uname)
RUBY		= $(shell which ruby 2>/dev/null)
ifeq ($(RUBY),)
RUBY		= $(shell which ruby24 2>/dev/null)
ifeq ($(RUBY),)
RUBY		= $(shell which ruby23 2>/dev/null)
ifeq ($(RUBY),)
RUBY		= $(shell which ruby22 2>/dev/null)
ifeq ($(RUBY),)
RUBY		= $(shell which ruby21 2>/dev/null)
ifeq ($(RUBY),)
RUBY		= $(shell which ruby20 2>/dev/null)
ifeq ($(RUBY),)
RUBY		= $(shell which ruby19 2>/dev/null)
ifeq ($(RUBY),)
RUBY		= $(shell which ruby18)
endif # 1.8
endif # 1.9
endif # 2.0
endif # 2.1
endif # 2.2
endif # 2.3
endif # 2.4

ifeq ($(UNAME),OpenBSD)
# Pick the correct base compiler (ie. clang rather than ancient gcc 4.2.1)
CC = cc
CXX = c++
endif

TAG		?= none
ifeq ($(TAG),none)
BRANCH		?= trunk
else
BRANCH		= $(TAG)
endif
WC		?= $(BRANCH)
BRANCH_MAJOR	= $(shell echo $(BRANCH) | \
			sed -e 's/\([0-9]\)\.\([0-9]\)\.[x0-9].*$$/\1.\2/')
SVN_REL_WC	= svn-$(WC)
SVN_WC		= $(PWD)/$(SVN_REL_WC)
PREFIX		= $(PWD)/prefix
SVN_PREFIX	= $(PREFIX)/svn-$(WC)
DISTDIR		= $(PWD)/distfiles
SRCDIR		= $(PWD)/src
OBJDIR		= $(PWD)/objdir

BDB_MAJOR_VER	= 4.7
BDB_VER		= $(BDB_MAJOR_VER).25
APR_VER		= 1.5.2
APR_ICONV_VER	= 1.2.1
GNU_ICONV_VER	= 1.15
APR_UTIL_VER	= 1.5.4
PCRE_VER	= 8.41
HTTPD_VER	= 2.4.37
NEON_VER	= 0.30.2
SERF_VER	= 1.3.9
SERF_OLD_VER	= 0.3.1
CYRUS_SASL_VER	= 2.1.25
SQLITE_VER	= 3160200
LIBMAGIC_VER	= 5.30
RUBY_VER	= 2.4.4
BZ2_VER	= 1.0.6
PYTHON_VER	= 3.7.5
PY3C_VER	= 1.1
JUNIT_VER	= 4.10
GETTEXT_VER	= 0.19.8.1
LZ4_VER		= 1.7.5

BDB_DIST	= db-$(BDB_VER).tar.gz
APR_ICONV_DIST	= apr-iconv-$(APR_ICONV_VER).tar.gz
GNU_ICONV_DIST	= libiconv-$(GNU_ICONV_VER).tar.gz
NEON_DIST	= neon-$(NEON_VER).tar.gz
SQLITE_DIST	= sqlite-autoconf-$(SQLITE_VER).tar.gz
CYRUS_SASL_DIST	= cyrus-sasl-$(CYRUS_SASL_VER).tar.gz
PCRE_DIST	= pcre-$(PCRE_VER).tar.gz
HTTPD_DIST	= httpd-$(HTTPD_VER).tar.gz
LIBMAGIC_DIST	= file-$(LIBMAGIC_VER).tar.gz
RUBY_DIST	= ruby-$(RUBY_VER).tar.gz
BZ2_DIST	= bzip2-$(BZ2_VER).tar.gz
PYTHON_DIST	= Python-$(PYTHON_VER).tgz
PY3C_DIST	= py3c-$(PY3C_VER).tar.gz
JUNIT_DIST	= junit-${JUNIT_VER}.jar
GETTEXT_DIST	= gettext-$(GETTEXT_VER).tar.gz
LZ4_DIST	= lz4-$(LZ4_VER).tar.gz

SHA256_${BDB_DIST} = f14fd96dd38915a1d63dcb94a63fbb8092334ceba6b5060760427096f631263e
SHA256_${APR_ICONV_DIST} = 19381959d50c4a5f3b9c84d594a5f9ffb3809786919b3058281f4c87e1f4b245
SHA256_${GNU_ICONV_DIST} = ccf536620a45458d26ba83887a983b96827001e92a13847b45e4925cc8913178
SHA256_${PCRE_DIST} = 244838e1f1d14f7e2fa7681b857b3a8566b74215f28133f14a8f5e59241b682c
SHA256_${HTTPD_DIST} = aa97a834a32d51974be8d8a013b561e28d327387cb1da2c3c2762acd0146aabd
SHA256_${NEON_DIST} = db0bd8cdec329b48f53a6f00199c92d5ba40b0f015b153718d1b15d3d967fbca
SHA256_${CYRUS_SASL_DIST} = 418c16e6240a4f9b637cbe3d62937b9675627bad27c622191d47de8686fe24fe
SHA256_${SQLITE_DIST} = 65cc0c3e9366f50c0679c5ccd31432cea894bc4a3e8947dabab88c8693263615
SHA256_${LIBMAGIC_DIST} = 694c2432e5240187524c9e7cf1ec6acc77b47a0e19554d34c14773e43dbbf214
SHA256_${RUBY_DIST} = 254f1c1a79e4cc814d1e7320bc5bdd995dc57e08727d30a767664619a9c8ae5a
SHA256_${BZ2_DIST} = a2848f34fcd5d6cf47def00461fcb528a0484d8edef8208d6d2e2909dc61d9cd
SHA256_${PYTHON_DIST} = 8ecc681ea0600bbfb366f2b173f727b205bb825d93d2f0b286bc4e58d37693da
SHA256_${PY3C_DIST} = c7ffc22bc92dded0ca859db53ef3a0b466f89a9f8aad29359c9fe4ff18ebdd20
SHA256_${JUNIT_DIST} = 36a747ca1e0b86f6ea88055b8723bb87030d627766da6288bf077afdeeb0f75a
SHA256_${GETTEXT_DIST} = ff942af0e438ced4a8b0ea4b0b6e0d6d657157c5e2364de57baa279c1c125c43
SHA256_${LZ4_DIST} = 0190cacd63022ccb86f44fa5041dc6c3804407ad61550ca21c382827319e7e7e

define do_check_sha256
if [ -x /bin/sha256 ]; then \
	(cd $(DISTDIR) && \
		echo "SHA256 (${1}) = ${SHA256_${1}}" | /bin/sha256 -C /dev/stdin "${1}"); \
elif [ -x /usr/bin/sha256sum ]; then \
	(cd $(DISTDIR) && \
		echo "${SHA256_${1}} ${1}" | /usr/bin/sha256sum --quiet --check); \
else \
	echo "Error: No tool found to verify checksum"; \
	false; \
fi
endef

DISTFILES	= $(DISTDIR)/$(NEON_DIST) \
		$(DISTDIR)/$(SERF_DIST) \
		$(DISTDIR)/$(SQLITE_DIST) \
		$(DISTDIR)/$(PCRE_DIST) \
		$(DISTDIR)/$(HTTPD_DIST) \
		$(DISTDIR)/$(APR_ICONV_DIST) \
		$(DISTDIR)/$(GNU_ICONV_DIST) \
		$(DISTDIR)/$(CYRUS_SASL_DIST) \
		$(DISTDIR)/$(LIBMAGIC_DIST) \
		$(DISTDIR)/$(RUBY_DIST) \
		$(DISTDIR)/$(BZ2_DIST) \
		$(DISTDIR)/$(PYTHON_DIST) \
		$(DISTDIR)/$(PY3C_DIST) \
		$(DISTDIR)/$(JUNIT_DIST) \
		$(DISTDIR)/$(GETTEXT_DIST)

FETCH_CMD	= wget -c

SUBVERSION_REPOS_URL = https://svn.apache.org/repos/asf/subversion
BDB_URL		= http://download.oracle.com/berkeley-db/$(BDB_DIST)
APR_URL		= https://svn.apache.org/repos/asf/apr/apr
APR_ICONV_URL	= https://archive.apache.org/dist/apr/$(APR_ICONV_DIST)
GNU_ICONV_URL	= https://ftp.gnu.org/pub/gnu/libiconv/$(GNU_ICONV_DIST)
APR_UTIL_URL	= https://svn.apache.org/repos/asf/apr/apr-util
PCRE_URL	= https://ftp.pcre.org/pub/pcre/$(PCRE_DIST)
HTTPD_URL	= https://archive.apache.org/dist/httpd/$(HTTPD_DIST)
#NEON_URL	= http://webdav.org/neon/$(NEON_DIST)
NEON_URL	= http://ftp.openbsd.org/pub/OpenBSD/distfiles/$(NEON_DIST)
SERF_URL	= https://svn.apache.org/repos/asf/serf/tags/$(SERF_VER)
SERF_OLD_URL	= https://svn.apache.org/repos/asf/serf/tags/$(SERF_OLD_VER)
SQLITE_URL	= https://www.sqlite.org/2017/$(SQLITE_DIST)
CYRUS_SASL_URL	= https://github.com/cyrusimap/cyrus-sasl/releases/download/cyrus-sasl-${CYRUS_SASL_VER}/$(CYRUS_SASL_DIST)
LIBMAGIC_URL	= ftp://ftp.astron.com/pub/file/$(LIBMAGIC_DIST)
RUBY_URL	= https://cache.ruby-lang.org/pub/ruby/2.4/$(RUBY_DIST)
BZ2_URL		= https://stsp.name/distfiles/$(BZ2_DIST)
PYTHON_URL	= https://python.org/ftp/python/$(PYTHON_VER)/$(PYTHON_DIST)
PY3C_URL	= https://stsp.name/distfiles/py3c-${PY3C_VER}.tar.gz
JUNIT_URL	= https://stsp.name/distfiles/$(JUNIT_DIST)
GETTEXT_URL	= https://ftp.gnu.org/pub/gnu/gettext/$(GETTEXT_DIST)
LZ4_URL		= https://github.com/lz4/lz4/archive/v$(LZ4_VER).tar.gz


BDB_SRCDIR	= $(SRCDIR)/db-$(BDB_VER)
APR_SRCDIR	= $(SRCDIR)/apr-$(APR_VER)
APR_ICONV_SRCDIR	= $(SRCDIR)/apr-iconv-$(APR_ICONV_VER)
GNU_ICONV_SRCDIR	= $(SRCDIR)/libiconv-$(GNU_ICONV_VER)
APR_UTIL_SRCDIR	= $(SRCDIR)/apr-util-$(APR_UTIL_VER)
PCRE_SRCDIR	= $(SRCDIR)/pcre-$(PCRE_VER)
HTTPD_SRCDIR	= $(SRCDIR)/httpd-$(HTTPD_VER)
NEON_SRCDIR	= $(SRCDIR)/neon-$(NEON_VER)
SERF_SRCDIR	= $(SRCDIR)/serf-$(SERF_VER)
SERF_OLD_SRCDIR	= $(SRCDIR)/serf-$(SERF_OLD_VER)
SQLITE_SRCDIR	= $(SRCDIR)/sqlite-autoconf-$(SQLITE_VER)
CYRUS_SASL_SRCDIR	= $(SRCDIR)/cyrus-sasl-$(CYRUS_SASL_VER)
LIBMAGIC_SRCDIR	= $(SRCDIR)/file-$(LIBMAGIC_VER)
RUBY_SRCDIR	= $(SRCDIR)/ruby-$(RUBY_VER)
BZ2_SRCDIR	= $(SRCDIR)/bzip2-$(BZ2_VER)
PYTHON_SRCDIR	= $(SRCDIR)/Python-$(PYTHON_VER)
PY3C_SRCDIR	= $(SRCDIR)/py3c-$(PY3C_VER)
GETTEXT_SRCDIR	= $(SRCDIR)/gettext-$(GETTEXT_VER)
LZ4_SRCDIR	= ${SRCDIR}/lz4-$(LZ4_VER)
SVN_SRCDIR	= $(SVN_WC)

BDB_OBJDIR	= $(OBJDIR)/db-$(BDB_VER)
APR_OBJDIR	= $(OBJDIR)/apr-$(APR_VER)
APR_ICONV_OBJDIR	= $(OBJDIR)/apr-iconv-$(APR_ICONV_VER)
GNU_ICONV_OBJDIR	= $(OBJDIR)/libiconv-$(GNU_ICONV_VER)
APR_UTIL_OBJDIR	= $(OBJDIR)/apr-util-$(APR_UTIL_VER)
PCRE_OBJDIR	= $(OBJDIR)/pcre-$(PCRE_VER)
HTTPD_OBJDIR	= $(OBJDIR)/httpd-$(HTTPD_VER)
NEON_OBJDIR	= $(OBJDIR)/neon-$(NEON_VER)
SERF_OBJDIR	= $(OBJDIR)/serf-$(SERF_VER)
SERF_OLD_OBJDIR	= $(OBJDIR)/serf-$(SERF_OLD_VER)
SQLITE_OBJDIR	= $(OBJDIR)/sqlite-$(SQLITE_VER)
CYRUS_SASL_OBJDIR	= $(OBJDIR)/cyrus-sasl-$(CYRUS_SASL_VER)
LIBMAGIC_OBJDIR	= $(OBJDIR)/file-$(LIBMAGIC_VER)
RUBY_OBJDIR	= $(OBJDIR)/ruby-$(RUBY_VER)
BZ2_OBJDIR	= $(OBJDIR)/bzip2-$(BZ2_VER)
PYTHON_OBJDIR	= $(OBJDIR)/python-$(PYTHON_VER)
PY3C_OBJDIR	= $(OBJDIR)/python-$(PY3C_VER)
GETTEXT_OBJDIR	= $(OBJDIR)/gettext-$(GETTEXT_VER)
LZ4_OBJDIR	= ${OBJDIR}/lz4-$(LZ4_VER)
SVN_OBJDIR	= $(OBJDIR)/$(SVN_REL_WC)

# Tweak this for out-of-tree builds. Note that running individual
# tests in the test suite won't work conveniently with out-of-tree
# builds!
svn_builddir	?=$(SVN_WC)

ifdef PROFILE
PROFILE_CFLAGS=-pg
endif

# We need this to make sure some targets below pick up the right libraries
LD_LIBRARY_PATH=$(PREFIX)/apr/lib:$(PREFIX)/gettext/lib:$(PREFIX)/iconv/lib:$(PREFIX)/bdb/lib:$(PREFIX)/neon/lib:$(PREFIX)/serf/lib:$(PREFIX)/sqlite/lib:$(PREFIX)/cyrus-sasl/lib:$(PREFIX)/iconv/lib:$(PREFIX)/libmagic/lib:$(PREFIX)/ruby/lib:$(PREFIX)/python/lib:$(PREFIX)/svn-$(WC)/lib

# We need this to make sure some targets below pick up the right pkg-config files
PKG_CONFIG_PATH=$(PREFIX)/apr/lib/pkgconfig:$(PREFIX)/neon/lib/pkgconfig:$(PREFIX)/serf/lib/pkgconfig:$(PREFIX)/sqlite/lib/pkgconfig:$(PREFIX)/ruby/lib/pkgconfig:$(PREFIX)/python/lib/pkgconfig:$(PREFIX)/lz4/lib/pkgconfig


#######################################################################
# Main targets.
#######################################################################

.PHONY: all reset clean nuke fetch

all: dirs-create bdb-install apr-install iconv-install apr-util-install \
	pcre-install httpd-install neon-install serf-install serf-old-install \
	sqlite-install cyrus-sasl-install libmagic-install \
	ruby-install bz2-install python-install py3c-retrieve gettext-install \
	lz4-install svn-install svn-bindings-install

# Use these to start a build from the beginning.
reset: dirs-reset bdb-reset apr-reset iconv-reset apr-util-reset \
	pcre-reset httpd-reset neon-reset serf-reset serf-old-reset \
	sqlite-reset cyrus-sasl-reset libmagic-reset ruby-reset python-reset \
	bz2-reset gettext-reset lz4-reset svn-reset

# Use to save disk space.
clean: bdb-clean apr-clean iconv-clean apr-util-clean pcre-clean httpd-clean \
	neon-clean serf-clean serf-old-clean sqlite-clean cyrus-sasl-clean \
	libmagic-clean ruby-clean bz2-clean python-clean gettext-clean \
	lz4-clean svn-clean

# Nukes everything (including installed binaries!)
# Use this to start ALL OVER AGAIN! Use with caution!
nuke:
	@echo
	@echo "I will now remove the following directories PERMANENTLY:"
	@echo
	@echo "  $(SRCDIR)"
	@echo "  $(OBJDIR)"
	@echo "  $(PREFIX)"
	@echo
	@echo -n 'Do you want me to continue? ([no]/yes): '
	@read ANSWER ; \
		case $$ANSWER in \
			yes)    echo "You said $$ANSWER. I will continue."; \
				echo rm -rf $(SRCDIR) $(OBJDIR) $(PREFIX); \
				rm -rf $(SRCDIR) $(OBJDIR) $(PREFIX); \
				$(MAKE) reset; \
				;; \
			"")     echo "You said no."; \
				;; \
			*)      echo "You said $$ANSWER."; \
				;; \
		esac

fetch: $(DISTFILES)

#######################################################################
# directories
#######################################################################

dirs-create: $(PWD)/.dirs-created
dirs-reset:
	rm -f $(PWD)/.dirs-created

$(PWD)/.dirs-created: 
	$(foreach d, $(PREFIX) $(DISTDIR) $(SRCDIR) $(OBJDIR), \
		[ -d $(d) ] || mkdir -p $(d);)
		touch $@

#######################################################################
# bdb
#######################################################################

bdb-retrieve:	$(BDB_OBJDIR)/.retrieved
bdb-configure:	$(BDB_OBJDIR)/.configured
bdb-compile:	$(BDB_OBJDIR)/.compiled
bdb-install:	$(BDB_OBJDIR)/.installed
bdb-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(BDB_OBJDIR)/$(f);)

bdb-clean:
	-(cd $(BDB_SRCDIR)/build_unix/ && env MAKEFLAGS= make clean)

# fetch distfile for bdb
$(DISTDIR)/$(BDB_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) $(BDB_URL)

# retrieve bdb
$(BDB_OBJDIR)/.retrieved: $(DISTDIR)/$(BDB_DIST)
	$(call do_check_sha256,$(BDB_DIST))
	[ -d $(BDB_OBJDIR) ] || mkdir -p $(BDB_OBJDIR)
	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(BDB_DIST)
	touch $@

# configure bdb
$(BDB_OBJDIR)/.configured: $(BDB_OBJDIR)/.retrieved
	cd $(BDB_SRCDIR)/build_unix \
		&& env CFLAGS="-g $(PROFILE_CFLAGS)" GREP="`which grep`" \
		CC=$(CC) CXX=$(CXX) \
		../dist/configure \
		--prefix=$(PREFIX)/bdb \
		--enable-debug
	touch $@

# compile bdb
$(BDB_OBJDIR)/.compiled: $(BDB_OBJDIR)/.configured
	(cd $(BDB_SRCDIR)/build_unix && env MAKEFLAGS= make -j${MAKE_JOBS})
	touch $@

# install bdb
$(BDB_OBJDIR)/.installed: $(BDB_OBJDIR)/.compiled
	(cd $(BDB_SRCDIR)/build_unix && env MAKEFLAGS= make install)
	touch $@

#######################################################################
# apr
#######################################################################

apr-retrieve:	$(APR_OBJDIR)/.retrieved
apr-configure:	$(APR_OBJDIR)/.configured
apr-compile:	$(APR_OBJDIR)/.compiled
apr-install:	$(APR_OBJDIR)/.installed
apr-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(APR_OBJDIR)/$(f);)

apr-clean:
	-(cd $(APR_OBJDIR) && env MAKEFLAGS= make clean)

# retrieve apr if not present yet
$(APR_OBJDIR)/.retrieved:
	[ -d $(APR_OBJDIR) ] || mkdir -p $(APR_OBJDIR)
	if [ ! -d $(APR_SRCDIR) ]; then \
		svn export $(APR_URL)/tags/$(APR_VER)/ $(APR_SRCDIR); \
	fi
	touch $@

ifeq ($(THREADING),yes)
THREADS_FLAG=--enable-threads
else
THREADS_FLAG=--disable-threads
endif

ifdef POOL_DEBUG
POOL_DEBUG_FLAG=--enable-pool-debug=all
else
# Map apr_palloc()/apr_pool_{clear,destroy}() to malloc()/free().
# This also puts poison bytes into freed memory to help detect use after free.
POOL_DEBUG_FLAG=--enable-pool-debug=yes
endif

# configure apr
$(APR_OBJDIR)/.configured: $(APR_OBJDIR)/.retrieved $(BDB_OBJDIR)/.installed
	cd $(APR_SRCDIR) && ./buildconf
	cd $(APR_OBJDIR) \
		&& env CFLAGS="-O0 -g $(PROFILE_CFLAGS)" GREP="`which grep`" \
		PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) \
		CC=$(CC) CXX=$(CXX) \
		$(APR_SRCDIR)/configure \
		--prefix=$(PREFIX)/apr \
		--enable-maintainer-mode \
		$(THREADS_FLAG) \
		$(POOL_DEBUG_FLAG)
	touch $@

# compile apr
$(APR_OBJDIR)/.compiled: $(APR_OBJDIR)/.configured
	(cd $(APR_OBJDIR) && env MAKEFLAGS= make -j${MAKE_JOBS})
	touch $@

# install apr
$(APR_OBJDIR)/.installed: $(APR_OBJDIR)/.compiled
	(cd $(APR_OBJDIR) && env MAKEFLAGS= make install)
	touch $@

#######################################################################
# apr-iconv
#######################################################################

apr-iconv-retrieve:	$(APR_ICONV_OBJDIR)/.retrieved
apr-iconv-configure:	$(APR_ICONV_OBJDIR)/.configured
apr-iconv-compile:	$(APR_ICONV_OBJDIR)/.compiled
apr-iconv-install:	$(APR_ICONV_OBJDIR)/.installed
apr-iconv-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(APR_ICONV_OBJDIR)/$(f);)

apr-iconv-clean:
	-(cd $(APR_ICONV_OBJDIR) && env MAKEFLAGS= make clean)

# fetch distfile for apr-iconv
$(DISTDIR)/$(APR_ICONV_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) $(APR_ICONV_URL)

# retrieve apr-iconv
$(APR_ICONV_OBJDIR)/.retrieved: $(DISTDIR)/$(APR_ICONV_DIST)
	$(call do_check_sha256,$(APR_ICONV_DIST))
	[ -d $(APR_ICONV_OBJDIR) ] || mkdir -p $(APR_ICONV_OBJDIR)
	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(APR_ICONV_DIST)
	touch $@

# configure apr-iconv
$(APR_ICONV_OBJDIR)/.configured: $(APR_ICONV_OBJDIR)/.retrieved \
	$(APR_OBJDIR)/.installed
	cd $(APR_ICONV_OBJDIR) \
		&& env CFLAGS="-g $(PROFILE_CFLAGS) -DAPR_POOL_DEBUG" \
		CC=$(CC) CXX=$(CXX) \
		GREP="`which grep`" \
		PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) \
		$(APR_ICONV_SRCDIR)/configure \
		--prefix=$(PREFIX)/apr \
		--with-apr=$(PREFIX)/apr
	touch $@

# compile apr-iconv
$(APR_ICONV_OBJDIR)/.compiled: $(APR_ICONV_OBJDIR)/.configured
	(cd $(APR_ICONV_OBJDIR) \
		&& env MAKEFLAGS= make CPPFLAGS="-D_OSD_POSIX" CFLAGS="-g -O0 $(PROFILE_CFLAGS)" -j${MAKE_JOBS})
	touch $@

# install apr-iconv
$(APR_ICONV_OBJDIR)/.installed: $(APR_ICONV_OBJDIR)/.compiled
	(cd $(APR_ICONV_OBJDIR) && env MAKEFLAGS= make install)
	touch $@

#######################################################################
# gnu-iconv
#######################################################################

gnu-iconv-retrieve:	$(GNU_ICONV_OBJDIR)/.retrieved
gnu-iconv-configure:	$(GNU_ICONV_OBJDIR)/.configured
gnu-iconv-compile:	$(GNU_ICONV_OBJDIR)/.compiled
gnu-iconv-install:	$(GNU_ICONV_OBJDIR)/.installed
gnu-iconv-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(GNU_ICONV_OBJDIR)/$(f);)

gnu-iconv-clean:
	-(cd $(GNU_ICONV_OBJDIR) && env MAKEFLAGS= make clean)
	rm -f $(GNU_ICONV_OBJDIR)/lib_encodings.def.diff
	rm -f $(GNU_ICONV_OBJDIR)/lib_aliases.gperf.diff

# fetch distfile for gnu-iconv
$(DISTDIR)/$(GNU_ICONV_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) $(GNU_ICONV_URL)

$(GNU_ICONV_OBJDIR)/lib_encodings.def.diff:
	mkdir -p $(dir $@)
	echo > $@.tmp '--- lib/encodings.def.orig	Wed Oct 24 23:41:41 2007'
	echo >>$@.tmp '+++ lib/encodings.def	Wed Oct 24 23:43:47 2007'
	echo >>$@.tmp '@@ -37,6 +37,7 @@'
	echo >>$@.tmp ' '
	echo >>$@.tmp ' '
	echo >>$@.tmp ' DEFENCODING(( "US-ASCII",               /* IANA */'
	echo >>$@.tmp '+              "646",'
	echo >>$@.tmp '               "ASCII",                  /* IANA, JDK 1.1 */'
	echo >>$@.tmp '               "ISO646-US",              /* IANA */'
	echo >>$@.tmp '               "ISO_646.IRV:1991",       /* IANA */'
	mv -f $@.tmp $@

$(GNU_ICONV_OBJDIR)/lib_aliases.gperf.diff:
	mkdir -p $(dir $@)
	echo > $@.tmp '--- lib/aliases.gperf.orig	Wed Oct 24 23:41:32 2007'
	echo >>$@.tmp '+++ lib/aliases.gperf	Wed Oct 24 23:47:38 2007'
	echo >>$@.tmp '@@ -10,6 +10,7 @@ struct alias { int name; unsigned int encoding_index; '
	echo >>$@.tmp ' %pic'
	echo >>$@.tmp ' %%'
	echo >>$@.tmp ' US-ASCII, ei_ascii'
	echo >>$@.tmp '+646, ei_ascii'
	echo >>$@.tmp ' ASCII, ei_ascii'
	echo >>$@.tmp ' ISO646-US, ei_ascii'
	echo >>$@.tmp ' ISO_646.IRV:1991, ei_ascii'
	mv -f $@.tmp $@

# retrieve gnu-iconv
# Add 646 as an alias for ASCII to fix prop_test 22 on OpenBSD
$(GNU_ICONV_OBJDIR)/.retrieved: $(DISTDIR)/$(GNU_ICONV_DIST) \
		$(GNU_ICONV_OBJDIR)/lib_encodings.def.diff \
		$(GNU_ICONV_OBJDIR)/lib_aliases.gperf.diff
	$(call do_check_sha256,$(GNU_ICONV_DIST))
	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(GNU_ICONV_DIST)
	cd $(SRCDIR)/libiconv-$(GNU_ICONV_VER) && \
		patch -p0 < $(GNU_ICONV_OBJDIR)/lib_encodings.def.diff && \
		patch -p0 < $(GNU_ICONV_OBJDIR)/lib_aliases.gperf.diff
	touch $@

# configure gnu-iconv
$(GNU_ICONV_OBJDIR)/.configured: $(GNU_ICONV_OBJDIR)/.retrieved
	cd $(SRCDIR)/libiconv-${GNU_ICONV_VER} && \
		${MAKE} -f Makefile.devel lib/aliases.h
	cd $(GNU_ICONV_OBJDIR) \
		&& env CFLAGS="-g $(PROFILE_CFLAGS)" GREP="`which grep`"\
		CC=$(CC) CXX=$(CXX) \
		PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) \
		$(GNU_ICONV_SRCDIR)/configure \
		--prefix=$(PREFIX)/iconv \
		--enable-extra-encodings
	touch $@

# compile gnu-iconv
$(GNU_ICONV_OBJDIR)/.compiled: $(GNU_ICONV_OBJDIR)/.configured
	(cd $(GNU_ICONV_OBJDIR) && env MAKEFLAGS= make -j${MAKE_JOBS})
	touch $@

# install gnu-iconv
$(GNU_ICONV_OBJDIR)/.installed: $(GNU_ICONV_OBJDIR)/.compiled
	(cd $(GNU_ICONV_OBJDIR) && env MAKEFLAGS= make install)
	touch $@

#######################################################################
# iconv
#######################################################################

.PHONY: iconv-install iconv-reset iconv-clean

ifeq ($(USE_APR_ICONV),yes)
iconv-install: apr-iconv-install
iconv-reset: apr-iconv-reset
iconv-clean: apr-iconv-clean
else
iconv-install: gnu-iconv-install
iconv-reset: gnu-iconv-reset
iconv-clean: gnu-iconv-clean
endif

#######################################################################
# apr-util
#######################################################################

apr-util-retrieve:	$(APR_UTIL_OBJDIR)/.retrieved
apr-util-configure:	$(APR_UTIL_OBJDIR)/.configured
apr-util-compile:	$(APR_UTIL_OBJDIR)/.compiled
apr-util-install:	$(APR_UTIL_OBJDIR)/.installed
apr-util-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(APR_UTIL_OBJDIR)/$(f);)

apr-util-clean:
	-(cd $(APR_UTIL_OBJDIR) && env MAKEFLAGS= make clean)


# retrieve apr-util if not present yet
$(APR_UTIL_OBJDIR)/.retrieved:
	[ -d $(APR_UTIL_OBJDIR) ] || mkdir -p $(APR_UTIL_OBJDIR)
	if [ ! -d $(APR_UTIL_SRCDIR) ]; then \
		svn export $(APR_UTIL_URL)/tags/$(APR_UTIL_VER)/ \
			$(APR_UTIL_SRCDIR); \
	fi
	touch $@

ifeq ($(USE_APR_ICONV),yes)
ICONV_FLAG=--with-iconv=$(PREFIX)/apr
ICONV_OBJDIR=$(APR_ICONV_OBJDIR)
else
ICONV_FLAG=--with-iconv=$(PREFIX)/iconv
ICONV_OBJDIR=$(GNU_ICONV_OBJDIR)
endif

# configure apr-util
$(APR_UTIL_OBJDIR)/.configured: $(APR_UTIL_OBJDIR)/.retrieved \
	$(APR_OBJDIR)/.installed $(ICONV_OBJDIR)/.installed
	cd $(APR_UTIL_SRCDIR) && ./buildconf --with-apr=$(APR_SRCDIR)
	cd $(APR_UTIL_OBJDIR) \
		&& env LD_LIBRARY_PATH=$(PREFIX)/bdb/lib \
			PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) \
			CFLAGS="-O0 -g $(PROFILE_CFLAGS) -DAPR_POOL_DEBUG" \
			CC=$(CC) CXX=$(CXX) \
			GREP="`which grep`" \
			$(APR_UTIL_SRCDIR)/configure \
		--prefix=$(PREFIX)/apr \
		--enable-maintainer-mode \
		--with-apr=$(PREFIX)/apr \
		--with-berkeley-db=$(PREFIX)/bdb \
		$(ICONV_FLAG)
	touch $@

# compile apr-util
$(APR_UTIL_OBJDIR)/.compiled: $(APR_UTIL_OBJDIR)/.configured
	(cd $(APR_UTIL_OBJDIR) && env MAKEFLAGS= make -j${MAKE_JOBS})
	touch $@

# install apr-util
$(APR_UTIL_OBJDIR)/.installed: $(APR_UTIL_OBJDIR)/.compiled
	(cd $(APR_UTIL_OBJDIR) && env MAKEFLAGS= make install)
	touch $@

#######################################################################
# pcre
#######################################################################

pcre-retrieve:	$(PCRE_OBJDIR)/.retrieved
pcre-configure:	$(PCRE_OBJDIR)/.configured
pcre-compile:	$(PCRE_OBJDIR)/.compiled
pcre-install:	$(PCRE_OBJDIR)/.installed
pcre-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(PCRE_OBJDIR)/$(f);)

pcre-clean:
	-(cd $(PCRE_OBJDIR) && env MAKEFLAGS= make clean)

# fetch distfile for pcre
$(DISTDIR)/$(PCRE_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) $(PCRE_URL)

# retrieve pcre
$(PCRE_OBJDIR)/.retrieved: $(DISTDIR)/$(PCRE_DIST)
	$(call do_check_sha256,$(PCRE_DIST))
	[ -d $(PCRE_OBJDIR) ] || mkdir -p $(PCRE_OBJDIR)
	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(PCRE_DIST)
	touch $@

# configure pcre
$(PCRE_OBJDIR)/.configured: $(PCRE_OBJDIR)/.retrieved
	cd $(PCRE_OBJDIR) \
		&& env CFLAGS="-g $(PROFILE_CFLAGS)" \
		CC=$(CC) CXX=$(CXX) \
		GREP="`which grep`" \
		PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) \
		$(PCRE_SRCDIR)/configure \
		--prefix=$(PREFIX)/pcre
	touch $@

# compile pcre
$(PCRE_OBJDIR)/.compiled: $(PCRE_OBJDIR)/.configured
	(cd $(PCRE_OBJDIR) && env MAKEFLAGS= make -j${MAKE_JOBS})
	touch $@

# install pcre
$(PCRE_OBJDIR)/.installed: $(PCRE_OBJDIR)/.compiled
	(cd $(PCRE_OBJDIR) && env MAKEFLAGS= make install)
	touch $@

#######################################################################
# httpd
#######################################################################

HTTPD_CONF=	$(PREFIX)/httpd/conf/httpd-$(SVN_REL_WC).conf
httpd-retrieve:	$(HTTPD_OBJDIR)/.retrieved
httpd-configure:	$(HTTPD_OBJDIR)/.configured
httpd-compile:	$(HTTPD_OBJDIR)/.compiled
httpd-install:	$(HTTPD_OBJDIR)/.installed $(HTTPD_CONF)
httpd-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(HTTPD_OBJDIR)/$(f);)

httpd-clean:
	-(cd $(HTTPD_OBJDIR) && env MAKEFLAGS= make clean)
	rm -f $(HTTPD_OBJDIR)/mod-proxy-no-threads-2.4.diff
	rm -f $(HTTPD_OBJDIR)/ssl-init-proxy-certs.diff

# fetch distfile for httpd
$(DISTDIR)/$(HTTPD_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) $(HTTPD_URL)

$(HTTPD_OBJDIR)/ssl-init-proxy-certs.diff:
	mkdir -p $(dir $@)
	echo > $@.tmp '--- modules/ssl/ssl_engine_init.c.orig	Sun Dec 16 13:34:14 2018'
	echo >> $@.tmp '+++ modules/ssl/ssl_engine_init.c	Sun Dec 16 13:34:59 2018'
	echo >> $@.tmp '@@ -1492,7 +1492,7 @@'
	echo >> $@.tmp '     X509_STORE_CTX *sctx;'
	echo >> $@.tmp '     X509_STORE *store = SSL_CTX_get_cert_store(mctx->ssl_ctx);'
	echo >> $@.tmp ' '
	echo >> $@.tmp '-#if OPENSSL_VERSION_NUMBER >= 0x1010100fL'
	echo >> $@.tmp '+#if OPENSSL_VERSION_NUMBER >= 0x1010100fL && !defined(LIBRESSL_VERSION_NUMBER)'
	echo >> $@.tmp '     /* For OpenSSL >=1.1.1, turn on client cert support which is'
	echo >> $@.tmp '      * otherwise turned off by default (by design).'
	echo >> $@.tmp '      * https://github.com/openssl/openssl/issues/6933 */'
	mv -f $@.tmp $@

$(HTTPD_OBJDIR)/mod-proxy-no-threads-2.4.diff:
	cd $(HTTPD_OBJDIR) && $(FETCH_CMD) https://stsp.name/mod-proxy-no-threads-2.4.diff

# retrieve httpd
$(HTTPD_OBJDIR)/.retrieved: $(DISTDIR)/$(HTTPD_DIST) \
	$(HTTPD_OBJDIR)/ssl-init-proxy-certs.diff \
	$(HTTPD_OBJDIR)/mod-proxy-no-threads-2.4.diff
	$(call do_check_sha256,$(HTTPD_DIST))
	[ -d $(HTTPD_OBJDIR) ] || mkdir -p $(HTTPD_OBJDIR)
	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(HTTPD_DIST)
	(cd $(HTTPD_SRCDIR) && patch -p0 < $(HTTPD_OBJDIR)/ssl-init-proxy-certs.diff)
	(cd $(HTTPD_SRCDIR) && patch -p0 < $(HTTPD_OBJDIR)/mod-proxy-no-threads-2.4.diff)
	(cd $(HTTPD_SRCDIR) && svn diff -cr1849590 https://svn.apache.org/repos/asf/httpd/httpd/trunk | patch -p0)
	(cd $(HTTPD_SRCDIR) && svn diff -cr1663375 https://svn.apache.org/repos/asf/httpd/httpd/trunk | patch -p0)
	touch $@

# configure httpd
$(HTTPD_OBJDIR)/.configured: $(HTTPD_OBJDIR)/.retrieved \
	$(APR_UTIL_OBJDIR)/.installed $(PCRE_OBJDIR)/.installed
	cd $(HTTPD_SRCDIR) && ./buildconf \
		--with-apr="$(PREFIX)/apr/bin/apr-1-config" \
		--with-apr-util="$(PREFIX)/apr/bin/apu-1-config"
	cd $(HTTPD_OBJDIR) \
		&& env CFLAGS="-g $(PROFILE_CFLAGS) -DAPR_POOL_DEBUG" \
		CC=$(CC) CXX=$(CXX) \
		GREP="`which grep`" \
		PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) \
		$(HTTPD_SRCDIR)/configure \
		--prefix=$(PREFIX)/httpd \
		--with-mpm=prefork \
		--enable-ssl \
		--enable-dav \
		--enable-proxy \
		--disable-md \
		--disable-http2 \
		--disable-brotli \
		--with-apr="$(PREFIX)/apr/bin/apr-1-config" \
		--with-apr-util="$(PREFIX)/apr/bin/apu-1-config" \
		--with-pcre="$(PREFIX)/pcre"
	touch $@

# compile httpd
$(HTTPD_OBJDIR)/.compiled: $(HTTPD_OBJDIR)/.configured
	(cd $(HTTPD_OBJDIR) && env MAKEFLAGS= make -j${MAKE_JOBS})
	touch $@

# install httpd
$(HTTPD_OBJDIR)/.installed: $(HTTPD_OBJDIR)/.compiled
	(cd $(HTTPD_OBJDIR) && env MAKEFLAGS= make install)
	touch $@

# create a httpd.conf for mod_dav_svn
$(HTTPD_CONF):
	mkdir -p $(dir $@)
	echo > $@.tmp '# httpd config for use with mod_dav_svn'
	echo >>$@.tmp 'ServerRoot "$(PREFIX)/httpd"'
	echo >>$@.tmp 'Listen localhost:8080'
	echo >>$@.tmp 'LoadModule unixd_module modules/mod_unixd.so'
	echo >>$@.tmp 'LoadModule alias_module modules/mod_alias.so'
	echo >>$@.tmp 'LoadModule access_compat_module modules/mod_access_compat.so'
	echo >>$@.tmp 'LoadModule authn_core_module modules/mod_authn_core.so'
	echo >>$@.tmp 'LoadModule authn_file_module modules/mod_authn_file.so'
	echo >>$@.tmp 'LoadModule authz_core_module modules/mod_authz_core.so'
	echo >>$@.tmp 'LoadModule authz_user_module modules/mod_authz_user.so'
	echo >>$@.tmp 'LoadModule authz_groupfile_module modules/mod_authz_groupfile.so'
	echo >>$@.tmp 'LoadModule auth_basic_module modules/mod_auth_basic.so'
	echo >>$@.tmp 'LoadModule dav_module modules/mod_dav.so'
	echo >>$@.tmp 'LoadModule dav_svn_module modules/svn-$(WC)/mod_dav_svn.so'
	echo >>$@.tmp 'LoadModule authz_svn_module modules/svn-$(WC)/mod_authz_svn.so'
	echo >>$@.tmp 'DocumentRoot "$(PREFIX)/httpd/htdocs"'
	echo >>$@.tmp '<Directory />'
	echo >>$@.tmp '    Options FollowSymLinks'
	echo >>$@.tmp '    AllowOverride None'
	echo >>$@.tmp '    Require all denied'
	echo >>$@.tmp '</Directory>'
	echo >>$@.tmp '<Location /repos>'
	echo >>$@.tmp '    DAV svn'
	echo >>$@.tmp '    SVNPath /tmp/svn-sandbox/repos'
	echo >>$@.tmp '    Require ip localhost'
	echo >>$@.tmp '</Location>'
	mv -f $@.tmp $@

#######################################################################
# neon
#######################################################################

neon-retrieve:	$(NEON_OBJDIR)/.retrieved
neon-configure:	$(NEON_OBJDIR)/.configured
neon-compile:	$(NEON_OBJDIR)/.compiled
neon-install:	$(NEON_OBJDIR)/.installed
neon-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(NEON_OBJDIR)/$(f);)

neon-clean:
	-(cd $(NEON_OBJDIR) && env MAKEFLAGS= make clean)

# fetch distfile for neon
$(DISTDIR)/$(NEON_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) $(NEON_URL)

# retrieve neon
$(NEON_OBJDIR)/.retrieved: $(DISTDIR)/$(NEON_DIST)
	$(call do_check_sha256,$(NEON_DIST))
	[ -d $(NEON_OBJDIR) ] || mkdir -p $(NEON_OBJDIR)
	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(NEON_DIST)
	touch $@

# OpenBSD does not have krb5-config in PATH, but the neon port has
# a suitable replacement.
ifeq ($(UNAME),OpenBSD)
KRB5_CONFIG_PATH=/usr/ports/net/neon/files
endif

# configure neon
$(NEON_OBJDIR)/.configured: $(NEON_OBJDIR)/.retrieved
	cd $(NEON_SRCDIR) && ./autogen.sh
	if [ -n "$(KRB5_CONFIG_PATH)" ] && [ -d "$(KRB5_CONFIG_PATH)" ]; then \
		cp $(KRB5_CONFIG_PATH)/krb5-config $(NEON_OBJDIR); \
		chmod +x $(NEON_OBJDIR)/krb5-config; \
	fi
	cd $(NEON_OBJDIR) \
		&& env CFLAGS="-g $(PROFILE_CFLAGS)" GREP="`which grep`" \
		CC=$(CC) CXX=$(CXX) \
		PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) \
		$(NEON_SRCDIR)/configure \
		PATH=$(NEON_OBJDIR):$$PATH \
		--prefix=$(PREFIX)/neon \
		--with-ssl \
		--enable-shared \
		--without-libproxy
	touch $@

# compile neon
$(NEON_OBJDIR)/.compiled: $(NEON_OBJDIR)/.configured
	(cd $(NEON_OBJDIR) && env MAKEFLAGS= make -j${MAKE_JOBS})
	touch $@

# install neon
$(NEON_OBJDIR)/.installed: $(NEON_OBJDIR)/.compiled
	(cd $(NEON_OBJDIR) && env MAKEFLAGS= make install)
	touch $@


#######################################################################
# serf
#######################################################################

serf-retrieve:	$(SERF_OBJDIR)/.retrieved
serf-configure:	$(SERF_OBJDIR)/.configured
serf-compile:	$(SERF_OBJDIR)/.compiled
serf-install:	$(SERF_OBJDIR)/.installed
serf-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(SERF_OBJDIR)/$(f);)

serf-clean:
	-(cd $(SERF_SRCDIR) && scons -c)


# fetch distfile for serf
#$(DISTDIR)/$(SERF_DIST):
#	cd $(DISTDIR) && $(FETCH_CMD) $(SERF_URL)
#
# retrieve serf
#$(SERF_OBJDIR)/.retrieved: $(DISTDIR)/$(SERF_DIST)
#	[ -d $(SERF_OBJDIR) ] || mkdir -p $(SERF_OBJDIR)
#	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(SERF_DIST)
#	touch $@

# retrieve serf if not present yet
$(SERF_OBJDIR)/.retrieved:
	[ -d $(SERF_OBJDIR) ] || mkdir -p $(SERF_OBJDIR)
	if [ ! -d $(SERF_SRCDIR) ]; then \
		svn co $(SERF_URL) $(SERF_SRCDIR); \
		svn merge ^/serf/branches/1.3.x-sslbuild@1781542 $(SERF_SRCDIR); \
	fi
	touch $@

# compile serf (serf won't compile outside its source tree)
$(SERF_OBJDIR)/.compiled: $(SERF_OBJDIR)/.retrieved \
	$(APR_UTIL_OBJDIR)/.installed
	cd $(SERF_SRCDIR) && \
		scons -j${MAKE_JOBS} DEBUG=1 \
			CFLAGS="-O0 -g $(PROFILE_CFLAGS) -DAPR_POOL_DEBUG" \
			CC=$(CC) CXX=$(CXX) \
			APR=$(PREFIX)/apr \
			APU=$(PREFIX)/apr \
			PREFIX=$(PREFIX)/serf \
			PKG_CONFIG_PATH=$(PKG_CONFIG_PATH)
	touch $@

# install serf
$(SERF_OBJDIR)/.installed: $(SERF_OBJDIR)/.compiled
	rm -rf $(PREFIX)/serf # XXX scons cannot reinstall :(
	cd $(SERF_SRCDIR) && \
		scons install
	# work around unportable scons shared lib support
	-ln -s libserf-1.so.$(shell echo $(SERF_VER) | sed -e 's/[0-9]$$/0/') \
		$(PREFIX)/serf/lib/libserf-1.so
	touch $@

#######################################################################
# serf-old (compatible with Subversion 1.5)
#######################################################################

serf-old-retrieve:	$(SERF_OLD_OBJDIR)/.retrieved
serf-old-configure:	$(SERF_OLD_OBJDIR)/.configured
serf-old-compile:	$(SERF_OLD_OBJDIR)/.compiled
serf-old-install:	$(SERF_OLD_OBJDIR)/.installed
serf-old-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(SERF_OLD_OBJDIR)/$(f);)

serf-old-clean:
	-(cd $(SERF_OLD_SRCDIR) && ./serfmake clean)

# retrieve serf if not present yet
$(SERF_OLD_OBJDIR)/.retrieved:
	[ -d $(SERF_OLD_OBJDIR) ] || mkdir -p $(SERF_OLD_OBJDIR)
	if [ ! -d $(SERF_OLD_SRCDIR) ]; then \
		svn export $(SERF_OLD_URL) $(SERF_OLD_SRCDIR); \
	fi
	touch $@

# compile serf (serf won't compile outside its source tree)
$(SERF_OLD_OBJDIR)/.compiled: $(SERF_OLD_OBJDIR)/.retrieved \
	$(APR_UTIL_OBJDIR)/.installed
	cd $(SERF_OLD_SRCDIR) && \
		env CFLAGS="-O0 -g $(PROFILE_CFLAGS) -DAPR_POOL_DEBUG" \
			CC=$(CC) CXX=$(CXX) \
			./serfmake --with-apr=$(PREFIX)/apr \
			--prefix=$(PREFIX)/serf-old \
			build
	touch $@

# install serf
$(SERF_OLD_OBJDIR)/.installed: $(SERF_OLD_OBJDIR)/.compiled
	cd $(SERF_OLD_SRCDIR) && \
		./serfmake --with-apr=$(PREFIX)/apr \
			--with-apr-util=$(PREFIX)/apr \
			--prefix=$(PREFIX)/serf-old \
			install
	touch $@


#######################################################################
# sqlite
#######################################################################

sqlite-retrieve:	$(SQLITE_OBJDIR)/.retrieved
sqlite-configure:	$(SQLITE_OBJDIR)/.configured
sqlite-compile:	$(SQLITE_OBJDIR)/.compiled
sqlite-install:	$(SQLITE_OBJDIR)/.installed
sqlite-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(SQLITE_OBJDIR)/$(f);)

sqlite-clean:
	-cd $(SQLITE_OBJDIR) && env MAKEFLAGS= make clean

# fetch distfile for sqlite
$(DISTDIR)/$(SQLITE_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) $(SQLITE_URL)

# retrieve sqlite
$(SQLITE_OBJDIR)/.retrieved: $(DISTDIR)/$(SQLITE_DIST)
	$(call do_check_sha256,$(SQLITE_DIST))
	[ -d $(SQLITE_OBJDIR) ] || mkdir -p $(SQLITE_OBJDIR)
	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(SQLITE_DIST)
	touch $@

ifeq ($(THREADING),yes)
THREADSAFE_FLAG=--enable-threadsafe
else
THREADSAFE_FLAG=--disable-threadsafe
endif

# configure sqlite
$(SQLITE_OBJDIR)/.configured: $(SQLITE_OBJDIR)/.retrieved
	cd $(SQLITE_OBJDIR) \
		&& env CFLAGS="-g $(PROFILE_CFLAGS)" GREP="`which grep`" \
		PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) \
		CC=$(CC) CXX=$(CXX) \
		$(SQLITE_SRCDIR)/configure \
		--prefix=$(PREFIX)/sqlite \
		$(THREADSAFE_FLAG)
	touch $@

# compile sqlite
$(SQLITE_OBJDIR)/.compiled: $(SQLITE_OBJDIR)/.configured
	(cd $(SQLITE_OBJDIR) && env MAKEFLAGS= make -j${MAKE_JOBS})
	touch $@

# install sqlite
$(SQLITE_OBJDIR)/.installed: $(SQLITE_OBJDIR)/.compiled
	(cd $(SQLITE_OBJDIR) && env MAKEFLAGS= make install)
	touch $@

#######################################################################
# cyrus-sasl
#######################################################################

cyrus-sasl-retrieve:	$(CYRUS_SASL_OBJDIR)/.retrieved
cyrus-sasl-configure:	$(CYRUS_SASL_OBJDIR)/.configured
cyrus-sasl-compile:	$(CYRUS_SASL_OBJDIR)/.compiled
cyrus-sasl-install:	$(CYRUS_SASL_OBJDIR)/.installed
cyrus-sasl-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(CYRUS_SASL_OBJDIR)/$(f);)

cyrus-sasl-clean:
	-(cd $(CYRUS_SASL_OBJDIR) && env MAKEFLAGS= make distclean)

# fetch distfile for cyrus-sasl
$(DISTDIR)/$(CYRUS_SASL_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) $(CYRUS_SASL_URL)

# retrieve cyrus-sasl
$(CYRUS_SASL_OBJDIR)/.retrieved: $(DISTDIR)/$(CYRUS_SASL_DIST)
	$(call do_check_sha256,$(CYRUS_SASL_DIST))
	[ -d $(CYRUS_SASL_OBJDIR) ] || mkdir -p $(CYRUS_SASL_OBJDIR)
	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(CYRUS_SASL_DIST)
	# fixes build on Debian:
	sed 's/#elif WITH_DES/#elif defined(WITH_DES)/' \
		< $(CYRUS_SASL_SRCDIR)/plugins/digestmd5.c \
		> $(CYRUS_SASL_SRCDIR)/plugins/digestmd5.c.patched
	mv $(CYRUS_SASL_SRCDIR)/plugins/digestmd5.c.patched \
		$(CYRUS_SASL_SRCDIR)/plugins/digestmd5.c
ifeq ($(UNAME),OpenBSD)
	# Fixes GSSAPI support on OpenBSD, which hasn't got libroken:
	for f in `grep -l -R -- -lroken $(CYRUS_SASL_SRCDIR)`; do \
		sed -e 's/-lroken//g' < $$f > $$f.tmp && \
		mv $$f.tmp $$f; \
	done
	chmod +x $(CYRUS_SASL_SRCDIR)/configure
endif
	# Fixes excessive auth log spam from sasl if broken .la files exist
	sed 's/SASL_LOG_WARN/SASL_LOG_DEBUG/' \
		< $(CYRUS_SASL_SRCDIR)/lib/dlopen.c \
		> $(CYRUS_SASL_SRCDIR)/lib/dlopen.c.patched
	mv $(CYRUS_SASL_SRCDIR)/lib/dlopen.c.patched \
		$(CYRUS_SASL_SRCDIR)/lib/dlopen.c
	# Fix a weird autotools error about missing cmulocal dir
	(cd $(CYRUS_SASL_SRCDIR)/saslauthd/ && ln -sf ../cmulocal)
	touch $@

# configure cyrus-sasl
$(CYRUS_SASL_OBJDIR)/.configured: $(CYRUS_SASL_OBJDIR)/.retrieved \
	$(BDB_OBJDIR)/.installed $(SQLITE_OBJDIR)/.installed
	cd $(CYRUS_SASL_OBJDIR) \
		&& env CFLAGS="-g $(PROFILE_CFLAGS)" \
		CPPFLAGS="-I/usr/include/kerberosV" \
		CC=$(CC) CXX=$(CXX) \
		GREP="`which grep`" \
		PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) \
		$(CYRUS_SASL_SRCDIR)/configure \
		--with-dbpath=$(PREFIX)/cyrus-sasl/etc/sasldb2 \
		--with-plugindir=$(PREFIX)/cyrus-sasl/lib/sasl2 \
		--with-configdir=$(PREFIX)/cyrus-sasl/lib/sasl2 \
		--with-bdb-libdir=$(PREFIX)/bdb/lib \
		--with-bdb-incdir=$(PREFIX)/bdb/include \
		--with-dblib=berkeley \
		--with-sqlite=$(PREFIX)/sqlite \
		--prefix=$(PREFIX)/cyrus-sasl
	touch $@

# compile cyrus-sasl (ignore MAKE_JOBS; multiple jobs cause random build failures)
$(CYRUS_SASL_OBJDIR)/.compiled: $(CYRUS_SASL_OBJDIR)/.configured
	(cd $(CYRUS_SASL_OBJDIR) && env MAKEFLAGS= make)
	touch $@

# install cyrus-sasl
$(CYRUS_SASL_OBJDIR)/.installed: $(CYRUS_SASL_OBJDIR)/.compiled
	(cd $(CYRUS_SASL_OBJDIR) && env MAKEFLAGS= make install)
	touch $@

#######################################################################
# libmagic
#######################################################################

libmagic-retrieve:	$(LIBMAGIC_OBJDIR)/.retrieved
libmagic-configure:	$(LIBMAGIC_OBJDIR)/.configured
libmagic-compile:	$(LIBMAGIC_OBJDIR)/.compiled
libmagic-install:	$(LIBMAGIC_OBJDIR)/.installed
libmagic-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(LIBMAGIC_OBJDIR)/$(f);)

libmagic-clean:
	-(cd $(LIBMAGIC_OBJDIR) && env MAKEFLAGS= make distclean)

# fetch distfile for libmagic
$(DISTDIR)/$(LIBMAGIC_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) $(LIBMAGIC_URL)

# retrieve libmagic
$(LIBMAGIC_OBJDIR)/.retrieved: $(DISTDIR)/$(LIBMAGIC_DIST)
	$(call do_check_sha256,$(LIBMAGIC_DIST))
	[ -d $(LIBMAGIC_OBJDIR) ] || mkdir -p $(LIBMAGIC_OBJDIR)
	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(LIBMAGIC_DIST)
	touch $@

# configure libmagic
$(LIBMAGIC_OBJDIR)/.configured: $(LIBMAGIC_OBJDIR)/.retrieved
	cd $(LIBMAGIC_OBJDIR) \
		&& env CFLAGS="-g $(PROFILE_CFLAGS)" GREP="`which grep`"\
		CC=$(CC) CXX=$(CXX) \
		PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) \
		$(LIBMAGIC_SRCDIR)/configure \
		--enable-fsect-man5 \
		--prefix=$(PREFIX)/libmagic
	touch $@

# compile libmagic
$(LIBMAGIC_OBJDIR)/.compiled: $(LIBMAGIC_OBJDIR)/.configured
	(cd $(LIBMAGIC_OBJDIR) && env MAKEFLAGS= make -j${MAKE_JOBS})
	touch $@

# install libmagic
$(LIBMAGIC_OBJDIR)/.installed: $(LIBMAGIC_OBJDIR)/.compiled
	(cd $(LIBMAGIC_OBJDIR) && env MAKEFLAGS= make install)
	touch $@

#######################################################################
# ruby
#######################################################################

ruby-retrieve:	$(RUBY_OBJDIR)/.retrieved
ruby-configure:	$(RUBY_OBJDIR)/.configured
ruby-compile:	$(RUBY_OBJDIR)/.compiled
ruby-install:	$(RUBY_OBJDIR)/.installed
ruby-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(RUBY_OBJDIR)/$(f);)

ruby-clean:
	-(cd $(RUBY_OBJDIR) && env MAKEFLAGS= make distclean)

# fetch distfile for ruby
$(DISTDIR)/$(RUBY_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) $(RUBY_URL)

$(RUBY_OBJDIR)/openssl_missing.patch:
	mkdir -p $(dir $@)
	echo > $@.tmp 'Index: ext/openssl/openssl_missing.h'
	echo >> $@.tmp '--- ext/openssl/openssl_missing.h.orig'
	echo >> $@.tmp '+++ ext/openssl/openssl_missing.h'
	echo >> $@.tmp '@@ -119,6 +119,9 @@ void ossl_HMAC_CTX_free(HMAC_CTX *);'
	echo >> $@.tmp ' #if !defined(HAVE_X509_STORE_SET_EX_DATA)'
	echo >> $@.tmp ' #  define X509_STORE_set_ex_data(x, idx, data) \'
	echo >> $@.tmp ' 	CRYPTO_set_ex_data(&(x)->ex_data, (idx), (data))'
	echo >> $@.tmp '+#endif'
	echo >> $@.tmp '+'
	echo >> $@.tmp '+#if !defined(HAVE_X509_STORE_GET_EX_NEW_INDEX)'
	echo >> $@.tmp ' #  define X509_STORE_get_ex_new_index(l, p, newf, dupf, freef) \'
	echo >> $@.tmp ' 	CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509_STORE, (l), (p), \'
	echo >> $@.tmp ' 				(newf), (dupf), (freef))'
	echo >> $@.tmp '@@ -192,6 +195,7 @@ void ossl_X509_REQ_get0_signature(const X509_REQ *, co'
	echo >> $@.tmp ' #endif'
	echo >> $@.tmp ' '
	echo >> $@.tmp ' #if !defined(HAVE_OPAQUE_OPENSSL)'
	echo >> $@.tmp '+#if defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL'
	echo >> $@.tmp ' #define IMPL_PKEY_GETTER(_type, _name) \'
	echo >> $@.tmp ' static inline _type *EVP_PKEY_get0_##_type(EVP_PKEY *pkey) { \'
	echo >> $@.tmp ' 	return pkey->pkey._name; }'
	echo >> $@.tmp '@@ -243,6 +247,7 @@ IMPL_PKEY_GETTER(EC_KEY, ec)'
	echo >> $@.tmp ' #undef IMPL_PKEY_GETTER'
	echo >> $@.tmp ' #undef IMPL_KEY_ACCESSOR2'
	echo >> $@.tmp ' #undef IMPL_KEY_ACCESSOR3'
	echo >> $@.tmp '+#endif'
	echo >> $@.tmp ' #endif /* HAVE_OPAQUE_OPENSSL */'
	echo >> $@.tmp ' '
	echo >> $@.tmp ' #if defined(HAVE_AUTHENTICATED_ENCRYPTION) && !defined(EVP_CTRL_AEAD_GET_TAG)'
	mv -f $@.tmp $@

$(RUBY_OBJDIR)/sparc64_buserror.patch:
	mkdir -p $(dir $@)
	echo > $@.tmp '--- compile.c.orig	Thu Mar 12 12:58:26 2020'
	echo >> $@.tmp '+++ compile.c	Thu Mar 12 16:36:55 2020'
	echo >> $@.tmp '@@ -751,6 +751,16 @@'
	echo >> $@.tmp '   #define STRICT_ALIGNMENT'
	echo >> $@.tmp ' #endif'
	echo >> $@.tmp ' '
	echo >> $@.tmp '+/*'
	echo >> $@.tmp '+ * Some OpenBSD platforms (including sparc64) require strict alignment.'
	echo >> $@.tmp '+ */'
	echo >> $@.tmp '+#if defined(__OpenBSD__)'
	echo >> $@.tmp '+  #include <sys/endian.h>'
	echo >> $@.tmp '+  #ifdef __STRICT_ALIGNMENT'
	echo >> $@.tmp '+    #define STRICT_ALIGNMENT'
	echo >> $@.tmp '+  #endif'
	echo >> $@.tmp '+#endif'
	echo >> $@.tmp '+'
	echo >> $@.tmp ' #ifdef STRICT_ALIGNMENT'
	echo >> $@.tmp '   #if defined(HAVE_TRUE_LONG_LONG) && SIZEOF_LONG_LONG > SIZEOF_VALUE'
	echo >> $@.tmp '     #define ALIGNMENT_SIZE SIZEOF_LONG_LONG'
	echo >> $@.tmp '@@ -2094,7 +2104,8 @@'
	echo >> $@.tmp ' 	    unref_destination(iobj, 0);'
	echo >> $@.tmp ' 	    REMOVE_ELEM(&iobj->link);'
	echo >> $@.tmp ' 	}'
	echo >> $@.tmp '-	else if (iobj != diobj && IS_INSN_ID(diobj, jump) &&'
	echo >> $@.tmp '+	else if (iobj != diobj && IS_INSN(&diobj->link) &&'
	echo >> $@.tmp '+		 IS_INSN_ID(diobj, jump) &&'
	echo >> $@.tmp ' 		 OPERAND_AT(iobj, 0) != OPERAND_AT(diobj, 0)) {'
	echo >> $@.tmp ' 	    replace_destination(iobj, diobj);'
	echo >> $@.tmp ' 	    remove_unreachable_chunk(iseq, iobj->link.next);'
	echo >> $@.tmp '@@ -2136,6 +2147,7 @@'
	echo >> $@.tmp ' 	 * L2:'
	echo >> $@.tmp ' 	 */'
	echo >> $@.tmp ' 	else if ((piobj = (INSN *)get_prev_insn(iobj)) != 0 &&'
	echo >> $@.tmp '+		 IS_INSN(&piobj->link) &&'
	echo >> $@.tmp ' 		 (IS_INSN_ID(piobj, branchif) ||'
	echo >> $@.tmp ' 		  IS_INSN_ID(piobj, branchunless))) {'
	echo >> $@.tmp ' 	    if (niobj == (INSN *)get_destination_insn(piobj)) {'
	echo >> $@.tmp '@@ -2176,7 +2188,7 @@'
	echo >> $@.tmp ' 	}'
	echo >> $@.tmp ' '
	echo >> $@.tmp ' 	for (;;) {'
	echo >> $@.tmp '-	    if (IS_INSN_ID(nobj, jump)) {'
	echo >> $@.tmp '+	    if (IS_INSN(&nobj->link) && IS_INSN_ID(nobj, jump)) {'
	echo >> $@.tmp ' 		replace_destination(iobj, nobj);'
	echo >> $@.tmp ' 	    }'
	echo >> $@.tmp ' 	    else if (prev_dup && IS_INSN_ID(nobj, dup) &&'
	mv -f $@.tmp $@

ifeq ($(UNAME),OpenBSD)
RUBY_SSL_EX_NEW_DATA_PATCH = sed -i -e '/^have_func("X509_STORE_set_ex_data")$$/ { p; s/^.*$$/\have_func("X509_STORE_get_ex_new_index")/; }'
else
RUBY_SSL_EX_NEW_DATA_PATCH = true
endif

# retrieve ruby
#
$(RUBY_OBJDIR)/.retrieved: $(DISTDIR)/$(RUBY_DIST) $(RUBY_OBJDIR)/openssl_missing.patch $(RUBY_OBJDIR)/sparc64_buserror.patch
	$(call do_check_sha256,$(RUBY_DIST))
	[ -d $(RUBY_OBJDIR) ] || mkdir -p $(RUBY_OBJDIR)
	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(RUBY_DIST)
	-which ghead && sed -i -e "s/head -c/ghead -c/" $(RUBY_SRCDIR)/configure
	$(RUBY_SSL_EX_NEW_DATA_PATCH) $(RUBY_SRCDIR)/ext/openssl/extconf.rb
	cd $(RUBY_SRCDIR) && patch -p0 < $(RUBY_OBJDIR)/openssl_missing.patch
	cd $(RUBY_SRCDIR) && patch -p0 < $(RUBY_OBJDIR)/sparc64_buserror.patch
	touch $@

ifeq ($(THREADING),yes)
THREADSAFE_FLAG=--enable-pthread
else
THREADSAFE_FLAG=--disable-pthread
endif

# configure ruby
$(RUBY_OBJDIR)/.configured: $(RUBY_OBJDIR)/.retrieved
	cd $(RUBY_OBJDIR) \
		&& env CFLAGS="-g $(PROFILE_CFLAGS)" GREP="`which grep`"\
		CC=$(CC) CXX=$(CXX) \
		PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) \
		$(RUBY_SRCDIR)/configure \
		--prefix=$(PREFIX)/ruby \
		--enable-shared \
		--with-baseruby="$(RUBY)" \
		$(THREADSAFE_FLAG) \
		--disable-install-doc \
		--without-valgrind \
		--without-gmp
	touch $@

# compile ruby (ignore MAKE_JOBS; multiple jobs cause random build failures)
$(RUBY_OBJDIR)/.compiled: $(RUBY_OBJDIR)/.configured
	(cd $(RUBY_OBJDIR) && env MAKEFLAGS= make)
	touch $@

# install ruby
$(RUBY_OBJDIR)/.installed: $(RUBY_OBJDIR)/.compiled
	(cd $(RUBY_OBJDIR) && env MAKEFLAGS= make install)
	touch $@

#######################################################################
# bz2
#######################################################################

bz2-retrieve:	$(BZ2_OBJDIR)/.retrieved
bz2-compile:	$(BZ2_OBJDIR)/.compiled
bz2-install:	$(BZ2_OBJDIR)/.installed
bz2-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(BZ2_OBJDIR)/$(f);)

bz2-clean:
	-(cd $(BZ2_SRCDIR) && env MAKEFLAGS= make distclean)

# fetch distfile for bz2
$(DISTDIR)/$(BZ2_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) $(BZ2_URL)

# retrieve bz2
$(BZ2_OBJDIR)/.retrieved: $(DISTDIR)/$(BZ2_DIST)
	$(call do_check_sha256,$(BZ2_DIST))
	[ -d $(BZ2_OBJDIR) ] || mkdir -p $(BZ2_OBJDIR)
	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(BZ2_DIST)
	touch $@

# compile bz2
$(BZ2_OBJDIR)/.compiled: $(BZ2_OBJDIR)/.retrieved
	(cd $(BZ2_SRCDIR) && env MAKEFLAGS= make CC=$(CC) CXX=$(CXX) \
		CFLAGS="-g $(PROFILE_CFLAGS) -fPIC" -j${MAKE_JOBS})
	touch $@

# install bz2
$(BZ2_OBJDIR)/.installed: $(BZ2_OBJDIR)/.compiled
	(cd $(BZ2_SRCDIR) && env MAKEFLAGS= make install PREFIX=$(PREFIX)/bz2)
	touch $@


#######################################################################
# python
#######################################################################

python-retrieve:	$(PYTHON_OBJDIR)/.retrieved
python-configure:	$(PYTHON_OBJDIR)/.configured
python-compile:	$(PYTHON_OBJDIR)/.compiled
python-install:	$(PYTHON_OBJDIR)/.installed
python-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(PYTHON_OBJDIR)/$(f);)

python-clean:
	-(cd $(PYTHON_OBJDIR) && env MAKEFLAGS= make distclean)

# fetch distfile for python
$(DISTDIR)/$(PYTHON_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) $(PYTHON_URL)

# retrieve python
#
$(PYTHON_OBJDIR)/.retrieved: $(DISTDIR)/$(PYTHON_DIST)
	$(call do_check_sha256,$(PYTHON_DIST))
	[ -d $(PYTHON_OBJDIR) ] || mkdir -p $(PYTHON_OBJDIR)
	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(PYTHON_DIST)
	# Make setup.py use our own dependencies instead of system ones
	sed -e "s#sqlite_inc_paths = \[ '/usr/include',#sqlite_inc_paths = [ '$(PREFIX)/sqlite/include',#" \
		-e "s#'/usr/include/db4'#'$(PREFIX)/bdb/include'#" \
		-e "s|\(add_dir_to_list(self.compiler.library_dirs, '/usr/local/lib')\)|pass #\1|" \
		-e "s|\(add_dir_to_list(self.compiler.include_dirs, '/usr/local/include')\)|pass #\1|" \
		-e "s#find_library_file(lib_dirs, 'bz2'#find_library_file(['$(PREFIX)/bz2/lib'] + lib_dirs, 'bz2'#" \
		< $(PYTHON_SRCDIR)/setup.py \
		> $(PYTHON_SRCDIR)/setup.py.patched
	mv $(PYTHON_SRCDIR)/setup.py.patched $(PYTHON_SRCDIR)/setup.py
	chmod +x $(PYTHON_SRCDIR)/setup.py
	touch $@

# configure python
ifdef PROFILE
PYTHON_PROFILING=--enable-profiling
endif
$(PYTHON_OBJDIR)/.configured: $(PYTHON_OBJDIR)/.retrieved \
	$(BZ2_OBJDIR)/.installed
	cd $(PYTHON_OBJDIR) \
		&& env CFLAGS="-g $(PROFILE_CFLAGS)" GREP="`which grep`" \
		CC=$(CC) CXX=$(CXX) \
		CPPFLAGS="-I$(PREFIX)/bz2/include" \
		LDFLAGS="-Wl,-rpath=$(PREFIX)/python/lib -L$(PREFIX)/bz2/lib" \
		LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):$$LD_LIBRARY_PATH" \
		PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) \
		$(PYTHON_SRCDIR)/configure \
		--prefix=$(PREFIX)/python \
		--enable-shared \
		--with-system-expat \
		--with-dbmliborder=bdb \
		--without-pymalloc \
		$(PYTHON_PROFILING)
	touch $@

# compile python
$(PYTHON_OBJDIR)/.compiled: $(PYTHON_OBJDIR)/.configured
	(cd $(PYTHON_OBJDIR) && \
		env MAKEFLAGS= \
		LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):$$LD_LIBRARY_PATH" \
		make -j${MAKE_JOBS})
	touch $@

# install python
$(PYTHON_OBJDIR)/.installed: $(PYTHON_OBJDIR)/.compiled
	(cd $(PYTHON_OBJDIR) && \
		env MAKEFLAGS= \
		LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):$$LD_LIBRARY_PATH" \
		make install)
	touch $@

#######################################################################
# py3c
#######################################################################

py3c-retrieve:	$(PY3C_OBJDIR)/.retrieved
py3c-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(PY3C_OBJDIR)/$(f);)

# fetch distfile for py3c
$(DISTDIR)/$(PY3C_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) $(PY3C_URL)

# retrieve py3c
$(PY3C_OBJDIR)/.retrieved: $(DISTDIR)/$(PY3C_DIST)
	$(call do_check_sha256,$(PY3C_DIST))
	[ -d $(PY3C_OBJDIR) ] || mkdir -p $(PY3C_OBJDIR)
	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(PY3C_DIST)
	touch $@

#######################################################################
# junit
#######################################################################

# fetch distfile for junit
$(DISTDIR)/$(JUNIT_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) $(JUNIT_URL)
	$(call do_check_sha256,$(JUNIT_DIST))


#######################################################################
# gettext
#######################################################################

gettext-retrieve:	$(GETTEXT_OBJDIR)/.retrieved
gettext-configure:	$(GETTEXT_OBJDIR)/.configured
gettext-compile:	$(GETTEXT_OBJDIR)/.compiled
gettext-install:	$(GETTEXT_OBJDIR)/.installed
gettext-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(GETTEXT_OBJDIR)/$(f);)

gettext-clean:
	-(cd $(GETTEXT_SRCDIR) && env MAKEFLAGS= make clean)

# fetch distfile for gettext
$(DISTDIR)/$(GETTEXT_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) $(GETTEXT_URL)

# retrieve gettext
$(GETTEXT_OBJDIR)/.retrieved: $(DISTDIR)/$(GETTEXT_DIST)
	$(call do_check_sha256,$(GETTEXT_DIST))
	[ -d $(GETTEXT_OBJDIR) ] || mkdir -p $(GETTEXT_OBJDIR)
	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(GETTEXT_DIST)
	touch $@

# (gettext won't compile outside its source tree)
# configure gettext
$(GETTEXT_OBJDIR)/.configured: $(GETTEXT_OBJDIR)/.retrieved
	cd $(GETTEXT_SRCDIR) \
		&& env CFLAGS="-g $(PROFILE_CFLAGS)" GREP="`which grep`"\
		CC=$(CC) CXX=$(CXX) \
		LDFLAGS="-L$(PREFIX)/iconv/lib" \
		PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) \
		$(GETTEXT_SRCDIR)/configure \
		--prefix=$(PREFIX)/gettext \
		--with-libiconv-prefix=$(PREFIX)/iconv \
		--disable-c++ \
		--disable-java \
		--disable-csharp \
		$(THREADS_FLAG)
	-which gsed && \
		sed -e 's/sed /gsed /g' < $(GETTEXT_SRCDIR)/build-aux/moopp \
		> $(GETTEXT_SRCDIR)/build-aux/moopp.fixed && \
		mv $(GETTEXT_SRCDIR)/build-aux/moopp.fixed \
		$(GETTEXT_SRCDIR)/build-aux/moopp && \
		chmod +x $(GETTEXT_SRCDIR)/build-aux/moopp
	touch $@

# compile gettext
$(GETTEXT_OBJDIR)/.compiled: $(GETTEXT_OBJDIR)/.configured
	(cd $(GETTEXT_SRCDIR) && env MAKEFLAGS= make -j${MAKE_JOBS})
	touch $@

# install gettext
$(GETTEXT_OBJDIR)/.installed: $(GETTEXT_OBJDIR)/.compiled
	(cd $(GETTEXT_SRCDIR) && env MAKEFLAGS= make install)
	touch $@

#######################################################################
# lz4
#######################################################################

lz4-retrieve:	$(LZ4_OBJDIR)/.retrieved
lz4-configure:	$(LZ4_OBJDIR)/.configured
lz4-compile:	$(LZ4_OBJDIR)/.compiled
lz4-install:	$(LZ4_OBJDIR)/.installed
lz4-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(LZ4_OBJDIR)/$(f);)

lz4-clean:
	-(cd $(LZ4_SRCDIR) && env MAKEFLAGS= $(MAKE) clean)

# fetch distfile for lz4
$(DISTDIR)/$(LZ4_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) -O $(LZ4_DIST) $(LZ4_URL)

# retrieve lz4
$(LZ4_OBJDIR)/.retrieved: $(DISTDIR)/$(LZ4_DIST)
	$(call do_check_sha256,$(LZ4_DIST))
	[ -d $(LZ4_OBJDIR) ] || mkdir -p $(LZ4_OBJDIR)
	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(LZ4_DIST)
	touch $@

# configure lz4
$(LZ4_OBJDIR)/.configured: $(LZ4_OBJDIR)/.retrieved
	touch $@

# compile lz4
$(LZ4_OBJDIR)/.compiled: $(LZ4_OBJDIR)/.configured
	(cd $(LZ4_SRCDIR)/lib && \
		env MAKEFLAGS= $(MAKE) CC=$(CC) CXX=$(CXX) \
			-j${MAKE_JOBS} PREFIX=$(PREFIX)/lz4)
	touch $@

# install lz4
$(LZ4_OBJDIR)/.installed: $(LZ4_OBJDIR)/.compiled
	mkdir -p $(PREFIX)/lz4/lib
	(cd $(LZ4_SRCDIR)/lib && \
		env MAKEFLAGS= $(MAKE) PREFIX=$(PREFIX)/lz4 install)
	touch $@

#######################################################################
# svn
#######################################################################

.PHONY: svn-configure svn-compile svn-install svn-bindings-compile \
	svn-bindings-install svn-bindings-reset svn-clean

svn-install-all: svn-install svn-bindings-install

svn-retrieve:	$(SVN_OBJDIR)/.retrieved
svn-configure:	$(SVN_OBJDIR)/.configured
svn-compile:	$(SVN_OBJDIR)/.compiled
svn-bindings-compile:	$(SVN_OBJDIR)/.bindings-compiled
svn-install:	$(SVN_OBJDIR)/.installed
svn-bindings-install:	$(SVN_OBJDIR)/.bindings-installed
svn-bindings-reset:
	$(foreach f, .bindings-compiled .bindings-installed, \
		rm -f $(SVN_OBJDIR)/$(f);)
svn-reset: svn-bindings-reset
	$(foreach f, .retrieved .configured .compiled .installed \
	        .bindings-compiled .bindings-installed, \
		rm -f $(SVN_OBJDIR)/$(f);)

svn-clean:
	-(cd $(svn_builddir) && env MAKEFLAGS= make distclean)

# retrieve svn if not present yet
$(SVN_OBJDIR)/.retrieved:
	[ -d $(SVN_OBJDIR) ] || mkdir -p $(SVN_OBJDIR)
	if [ "$(TAG)" != "none" ]; then \
		branchdir="tags/$(TAG)"; \
		co="export"; \
	elif [ $(BRANCH) != trunk ]; then \
		branchdir="branches/$(BRANCH)"; \
		co="co"; \
	else \
		branchdir="$(BRANCH)"; \
		co="co"; \
	fi; \
	if [ ! -d $(SVN_WC) ] && [ ! -h $(SVN_WC) ]; then \
		svn $${co} $(SUBVERSION_REPOS_URL)/$${branchdir} \
			$(SVN_WC); \
	fi
	touch $@

ifeq ($(BRANCH_MAJOR),1.7)
BDB_FLAG=db.h:$(PREFIX)/bdb/include:$(PREFIX)/bdb/lib:db-$(BDB_MAJOR_VER)
SERF_FLAG=--with-serf="$(PREFIX)/serf"
SERF_LDFLAG=-Wl,-rpath,$(PREFIX)/serf/lib -Wl,-rpath,$(PREFIX)/bdb/lib
MOD_DAV_SVN=modules/svn-$(WC)/mod_dav_svn.so
MOD_AUTHZ_SVN=modules/svn-$(WC)/mod_authz_svn.so
MOD_DONTDOTHAT=modules/svn-$(WC)/mod_dontdothat.so
LIBMAGIC_FLAG=--with-libmagic=$(PREFIX)/libmagic
NEON_FLAG=--with-neon="$(PREFIX)/neon"
JAVAHL_CHECK_TARGET=check-javahl
else ifeq ($(BRANCH_MAJOR),1.6)
BDB_FLAG=db.h:$(PREFIX)/bdb/include:$(PREFIX)/bdb/lib:db-$(BDB_MAJOR_VER)
SERF_FLAG=--with-serf="$(PREFIX)/serf"
SERF_LDFLAG=-Wl,-rpath,$(PREFIX)/serf/lib -Wl,-rpath,$(PREFIX)/bdb/lib
MOD_DAV_SVN=modules/svn-$(WC)/mod_dav_svn.so
MOD_AUTHZ_SVN=modules/svn-$(WC)/mod_authz_svn.so
MOD_DONTDOTHAT=modules/svn-$(WC)/mod_dontdothat.so
W_NO_SYSTEM_HEADERS=-Wno-system-headers
NEON_FLAG=--with-neon="$(PREFIX)/neon"
JAVAHL_CHECK_TARGET=check-javahl
else ifeq ($(BRANCH_MAJOR),1.5)
BDB_FLAG=$(PREFIX)/bdb
SERF_FLAG=--with-serf="$(PREFIX)/serf-old"
MOD_DAV_SVN=modules/mod_dav_svn.so
MOD_AUTHZ_SVN=modules/mod_authz_svn.so
MOD_DONTDOTHAT=modules/mod_dontdothat.so
DISABLE_NEON_VERSION_CHECK=--disable-neon-version-check
W_NO_SYSTEM_HEADERS=-Wno-system-headers
NEON_FLAG=--with-neon="$(PREFIX)/neon"
JAVAHL_CHECK_TARGET=check-javahl
else ifeq ($(BRANCH_MAJOR), $(filter $(BRANCH_MAJOR), 1.8 1.9))
BDB_FLAG=db.h:$(PREFIX)/bdb/include:$(PREFIX)/bdb/lib:db-$(BDB_MAJOR_VER)
SERF_FLAG=--with-serf="$(PREFIX)/serf"
# serf >= 1.3.0 is built with scons and no longer sets up rpath linker flags,
# so we have to do that ourselves :(
SERF_LDFLAG=-Wl,-rpath,$(PREFIX)/serf/lib -Wl,-rpath,$(PREFIX)/bdb/lib
MOD_DAV_SVN=modules/svn-$(WC)/mod_dav_svn.so
MOD_AUTHZ_SVN=modules/svn-$(WC)/mod_authz_svn.so
MOD_DONTDOTHAT=modules/svn-$(WC)/mod_dontdothat.so
LIBMAGIC_FLAG=--with-libmagic=$(PREFIX)/libmagic
JAVAHL_CHECK_TARGET=check-all-javahl
else # 1.10
BDB_FLAG=db.h:$(PREFIX)/bdb/include:$(PREFIX)/bdb/lib:db-$(BDB_MAJOR_VER)
SERF_FLAG=--with-serf="$(PREFIX)/serf"
# serf >= 1.3.0 is built with scons and no longer sets up rpath linker flags,
# so we have to do that ourselves :(
SERF_LDFLAG=-Wl,-rpath,$(PREFIX)/serf/lib -Wl,-rpath,$(PREFIX)/bdb/lib
LZ4_LDFLAG=-Wl,-rpath,$(PREFIX)/lz4/lib
MOD_DAV_SVN=modules/svn-$(WC)/mod_dav_svn.so
MOD_AUTHZ_SVN=modules/svn-$(WC)/mod_authz_svn.so
MOD_DONTDOTHAT=modules/svn-$(WC)/mod_dontdothat.so
LIBMAGIC_FLAG=--with-libmagic=$(PREFIX)/libmagic
JAVAHL_CHECK_TARGET=check-all-javahl
LZ4_FLAG=--with-lz4=$(PREFIX)/lz4
UTF8PROC_FLAG=--with-utf8proc=internal
endif

ifeq ($(ENABLE_JAVA_BINDINGS),yes)
	JAVAHL_FLAG=--enable-javahl=yes --with-jdk --with-jikes=no \
		--with-junit=$(DISTDIR)/$(JUNIT_DIST)
else
	JAVAHL_FLAG=--with-jdk=no
endif

ifdef PROFILE
SVN_STATIC_FLAG=--enable-all-static
else
SVN_STATIC_FLAG=--disable-static
SVN_WITH_HTTPD=--with-apxs="$(PREFIX)/httpd/bin/apxs" \
	--with-apache-libexecdir="$(PREFIX)/httpd/modules/svn-$(WC)"
SVN_WITH_SASL=--with-sasl="$(PREFIX)/cyrus-sasl"
endif

# On OpenBSD, MExtUtils -e ldopts outputs -L/usr/local/lib, which can
# cause us to link Perl bindings against the wrong set of SVN libraries.
# As a workaround, we patch the configure script after it has been generated.
ifeq ($(UNAME),OpenBSD)
SWIG_PL_INCLUDES_HACK= sed -i 's^\($$PERL -MExtUtils::Embed -e ccopts\)^\1 | sed -e s@-I/usr/local/include@@^' $(svn_builddir)/configure
SWIG_PL_LINK_HACK= sed -i 's^\($$PERL -MExtUtils::Embed -e ldopts\)^\1 | sed -e s@-L/usr/local/lib@@^' $(svn_builddir)/configure
else
SWIG_PL_INCLUDES_HACK=true
SWIG_PL_LINK_HACK=true
endif

$(SVN_OBJDIR)/.configured: $(SVN_OBJDIR)/.retrieved $(DISTDIR)/$(JUNIT_DIST) \
	$(APR_OBJDIR)/.installed $(APR_UTIL_OBJDIR)/.installed \
	$(BDB_OBJDIR)/.installed $(SQLITE_OBJDIR)/.installed \
	$(HTTPD_OBJDIR)/.installed $(CYRUS_SASL_OBJDIR)/.installed \
	$(LIBMAGIC_OBJDIR)/.installed $(NEON_OBJDIR)/.installed \
	$(SERF_OBJDIR)/.installed $(SERF_OLD_OBJDIR)/.installed \
	$(RUBY_OBJDIR)/.installed $(PYTHON_OBJDIR)/.installed
	cd $(SVN_SRCDIR) && ./autogen.sh
	$(SWIG_PL_INCLUDES_HACK)
	$(SWIG_PL_LINK_HACK)
	cd $(svn_builddir) && \
		env LDFLAGS="-L$(PREFIX)/neon/lib -L$(PREFIX)/apr/lib $(SERF_LDFLAG) $(LZ4_LDFLAG) -L$(PREFIX)/gettext/lib -L$(PREFIX)/iconv/lib" \
			CC=$(CC) CXX=$(CXX) \
			CFLAGS="-I$(PREFIX)/gettext/include -DAPR_POOL_DEBUG" \
			CXXFLAGS="-I$(PREFIX)/gettext/include -DAPR_POOL_DEBUG" \
			LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):$$LD_LIBRARY_PATH" \
			PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) \
			PYTHON=$(PREFIX)/python/bin/python3 \
			GREP="`which grep`" \
			PATH=$(PREFIX)/ruby/bin:$(PREFIX)/python/bin:$(PREFIX)/gettext/bin:$$PATH \
			$(SVN_SRCDIR)/configure \
			--enable-maintainer-mode \
			--prefix="$(SVN_PREFIX)" \
			--with-apr="$(PREFIX)/apr" \
			--with-apr-util="$(PREFIX)/apr" \
			$(NEON_FLAG) \
			$(SVN_WITH_HTTPD) \
			$(SVN_WITH_SASL) \
			$(SERF_FLAG) \
			--with-sqlite="$(PREFIX)/sqlite" \
			--with-zlib="/usr" \
			--without-gnome-keyring \
			--with-berkeley-db="$(BDB_FLAG)" \
			--with-ruby-sitedir="$(SVN_PREFIX)/lib/ruby/site_ruby" \
			--with-py3c="$(SRCDIR)/py3c-${PY3C_VER}" \
			--disable-mod-activation \
			$(JAVAHL_FLAG) \
			$(LIBMAGIC_FLAG) \
			$(LZ4_FLAG) \
			$(UTF8PROC_FLAG) \
			$(SVN_STATIC_FLAG) \
			$(DISABLE_NEON_VERSION_CHECK)
	touch $@

# compile svn
$(SVN_OBJDIR)/.compiled: $(SVN_OBJDIR)/.configured
	cd $(svn_builddir) \
		&& env MAKEFLAGS= make -j${MAKE_JOBS} EXTRA_CFLAGS="$(PROFILE_CFLAGS) $(W_NO_SYSTEM_HEADERS)"
	touch $@

# install svn
$(SVN_OBJDIR)/.installed: $(SVN_OBJDIR)/.compiled
	cd $(svn_builddir) \
		&& env MAKEFLAGS= make install install-tools
	touch $@

# SWIG 1.x and 2.x are not compatible. If SWIG 2.x is used to generated .swg
# files and 1.x is used to build the bindings, the Python bindings fail to
# load with errors such as "undefined symbol 'SWIG_Python_str_AsChar'".
# So clean any pre-generated .swg files to make sure everything is done
# by the same version of SWIG.
$(SVN_OBJDIR)/.pre-generated-swig-cleaned:
	-cd $(svn_builddir) \
		&& env MAKEFLAGS= make clean-swig
	touch $@


# On OpenBSD, Perl's LDDLFLAGS include -L/usr/local/lib, which can cause
# us to link Perl bindings against the wrong set of SVN libraries.
# We manually fix up the generated Makefile.PL to work around this issue.
ifeq ($(UNAME),OpenBSD)
MAKEFILE_PL_LDDLFLAGS_HACK= sed -i 's@^WriteMakefile@$$config{LDDLFLAGS} =~ s+-L/usr/local/lib++; WriteMakefile@' \
			$(SVN_SRCDIR)/subversion/bindings/swig/perl/native/Makefile.PL
else
MAKEFILE_PL_LDDLFLAGS_HACK=true
endif

$(SVN_OBJDIR)/.bindings-compiled: $(SVN_OBJDIR)/.installed $(SVN_OBJDIR)/.pre-generated-swig-cleaned
	cd $(svn_builddir) \
		&& env LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) \
			env MAKEFLAGS= make -j${MAKE_JOBS} swig-py
	cd $(svn_builddir) && \
		env PATH=$(PREFIX)/ruby/bin:$$PATH \
		LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) env MAKEFLAGS= make -j${MAKE_JOBS} swig-rb
	if [ $(ENABLE_PERL_BINDINGS) = yes ]; then \
		cd $(svn_builddir) && make $(SVN_SRCDIR)/subversion/bindings/swig/perl/native/Makefile.PL; \
		$(MAKEFILE_PL_LDDLFLAGS_HACK); \
		cd $(svn_builddir) \
			&& env LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) \
				env MAKEFLAGS= make -j${MAKE_JOBS} swig-pl; \
	fi
	if [ $(ENABLE_JAVA_BINDINGS) = yes ]; then \
		cd $(svn_builddir) \
			&& env MAKEFLAGS= make javahl; \
	fi
	touch $@

$(SVN_OBJDIR)/.bindings-installed: $(SVN_OBJDIR)/.bindings-compiled
	cd $(svn_builddir) \
	  && env LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) \
		env MAKEFLAGS= make install-swig-py
	cd $(svn_builddir) && \
		env PATH=$(PREFIX)/ruby/bin:$$PATH \
		LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) MAKEFLAGS= make install-swig-rb
	if [ $(ENABLE_PERL_BINDINGS) = yes ]; then \
		cd $(svn_builddir) \
			&& env MAKEFLAGS= LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) \
				make install-swig-pl-lib; \
		cd subversion/bindings/swig/perl/native \
			&& perl Makefile.PL PREFIX="$(SVN_PREFIX)" \
			&& env MAKEFLAGS= LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) \
				make install; \
	fi
	if [ $(ENABLE_JAVA_BINDINGS) = yes ]; then \
		cd $(svn_builddir) \
			&& env MAKEFLAGS= make install-javahl; \
	fi
	touch $@

# run svn regression tests
HTTPD_CHECK_CONF=$(PREFIX)/httpd/conf/httpd-svn-check-$(WC).conf
HTTPD_PROXY_CONF=$(PREFIX)/httpd/conf/httpd-svn-proxy-$(WC).conf
HTTPD_CHECK_USERS=$(PREFIX)/httpd/conf/httpd-svn-check-users
HTTPD_CHECK_GROUPS=$(PREFIX)/httpd/conf/httpd-svn-check-groups
HTTPD_CHECK_PORT=8081
HTTPD_PROXY_PORT=8082
MOD_DONTDOTHAT_CONF=$(PREFIX)/httpd/conf/dontdothat

$(MOD_DONTDOTHAT_CONF):
	mkdir -p $(dir $@)
	echo > $@.tmp '[recursive-actions]'
	echo >>$@.tmp '/ = deny'
	mv -f $@.tmp $@

$(HTTPD_CHECK_GROUPS):
	mkdir -p $(dir $@)
	printf "random: jrandom\nconstant: jconstant\n" > $@

$(HTTPD_CHECK_CONF): $(MOD_DONTDOTHAT_CONF) $(HTTPD_CHECK_GROUPS)
	mkdir -p $(dir $@)
	$(PREFIX)/httpd/bin/htpasswd -bc $(HTTPD_CHECK_USERS).tmp jrandom rayjandom
	$(PREFIX)/httpd/bin/htpasswd -b $(HTTPD_CHECK_USERS).tmp jconstant rayjandom
	$(PREFIX)/httpd/bin/htpasswd -b $(HTTPD_CHECK_USERS).tmp __dumpster__ __loadster__
	$(PREFIX)/httpd/bin/htpasswd -b $(HTTPD_CHECK_USERS).tmp JRANDOM rayjandom
	$(PREFIX)/httpd/bin/htpasswd -b $(HTTPD_CHECK_USERS).tmp JCONSTANT rayjandom
	mv -f $(HTTPD_CHECK_USERS).tmp $(HTTPD_CHECK_USERS)
	echo > $@.tmp '# httpd config for make check'
	echo >>$@.tmp 'ServerRoot "$(PREFIX)/httpd"'
	echo >>$@.tmp 'Listen localhost:$(HTTPD_CHECK_PORT)'
	echo >>$@.tmp 'LoadModule unixd_module modules/mod_unixd.so'
	echo >>$@.tmp 'LoadModule alias_module modules/mod_alias.so'
	echo >>$@.tmp 'LoadModule access_compat_module modules/mod_access_compat.so'
	echo >>$@.tmp 'LoadModule authn_core_module modules/mod_authn_core.so'
	echo >>$@.tmp 'LoadModule authn_file_module modules/mod_authn_file.so'
	echo >>$@.tmp 'LoadModule authz_core_module modules/mod_authz_core.so'
	echo >>$@.tmp 'LoadModule authz_user_module modules/mod_authz_user.so'
	echo >>$@.tmp 'LoadModule authz_groupfile_module modules/mod_authz_groupfile.so'
	echo >>$@.tmp 'LoadModule auth_basic_module modules/mod_auth_basic.so'
	echo >>$@.tmp 'LoadModule dav_module modules/mod_dav.so'
	echo >>$@.tmp 'LoadModule dav_svn_module $(MOD_DAV_SVN)'
	echo >>$@.tmp 'LoadModule authz_svn_module $(MOD_AUTHZ_SVN)'
	echo >>$@.tmp 'LoadModule dontdothat_module $(MOD_DONTDOTHAT)'
	echo >>$@.tmp 'DocumentRoot "$(PREFIX)/httpd/htdocs"'
	echo >>$@.tmp '# These two Locations are used for "make check"'
	echo >>$@.tmp '<Directory />'
	echo >>$@.tmp '    Options FollowSymLinks'
	echo >>$@.tmp '    AllowOverride None'
	echo >>$@.tmp '    Require all granted'
	echo >>$@.tmp '</Directory>'
	echo >>$@.tmp '<Location /svn-test-work/repositories>'
	echo >>$@.tmp '    DAV svn'
	echo >>$@.tmp '    SVNParentPath $(SVN_WC)/subversion/tests/cmdline/svn-test-work/repositories'
	echo >>$@.tmp '    AuthzSVNAccessFile $(SVN_WC)/subversion/tests/cmdline/svn-test-work/authz'
	echo >>$@.tmp '    AuthType Basic'
	echo >>$@.tmp '    AuthName "Subversion Repository"'
	echo >>$@.tmp '    AuthUserFile $(HTTPD_CHECK_USERS)'
	echo >>$@.tmp '    Require valid-user'
ifeq ($(USE_HTTPV1),yes)
	echo >>$@.tmp '    SVNAdvertiseV2Protocol off'
endif
ifeq ($(USE_AUTHZ_SHORT_CIRCUIT),yes)
	echo >>$@.tmp '    SVNPathAuthz short_circuit'
endif
	echo >>$@.tmp '</Location>'
	echo >>$@.tmp '<Location /svn-test-work/local_tmp/repos>'
	echo >>$@.tmp '    DAV svn'
	echo >>$@.tmp '    SVNPath $(SVN_WC)/subversion/tests/cmdline/svn-test-work/local_tmp/repos'
	echo >>$@.tmp '    AuthzSVNAccessFile $(SVN_WC)/subversion/tests/cmdline/svn-test-work/authz'
	echo >>$@.tmp '    AuthType Basic'
	echo >>$@.tmp '    AuthName "Subversion Repository"'
	echo >>$@.tmp '    AuthUserFile $(HTTPD_CHECK_USERS)'
	echo >>$@.tmp '    Require valid-user'
ifeq ($(USE_HTTPV1),yes)
	echo >>$@.tmp '    SVNAdvertiseV2Protocol off'
endif
ifeq ($(USE_AUTHZ_SHORT_CIRCUIT),yes)
	echo >>$@.tmp '    SVNPathAuthz short_circuit'
endif
	echo >>$@.tmp '</Location>'
	echo >>$@.tmp '<Location /svn-test-work/local_tmp/trojan>'
	echo >>$@.tmp '    DAV svn'
	echo >>$@.tmp '    SVNPath $(SVN_WC)/subversion/tests/cmdline/svn-test-work/local_tmp/trojan'
	echo >>$@.tmp '    AuthzSVNAccessFile $(SVN_WC)/subversion/tests/cmdline/svn-test-work/authz'
	echo >>$@.tmp '    AuthType Basic'
	echo >>$@.tmp '    AuthName "Subversion Repository"'
	echo >>$@.tmp '    AuthUserFile $(HTTPD_CHECK_USERS)'
	echo >>$@.tmp '    Require valid-user'
ifeq ($(USE_HTTPV1),yes)
	echo >>$@.tmp '    SVNAdvertiseV2Protocol off'
endif
ifeq ($(USE_AUTHZ_SHORT_CIRCUIT),yes)
	echo >>$@.tmp '    SVNPathAuthz short_circuit'
endif
	echo >>$@.tmp '</Location>'
	echo >>$@.tmp '# This Location lets you access repositories dropped in /tmp/'
	echo >>$@.tmp '<Location /svn>'
	echo >>$@.tmp '    DAV svn'
	echo >>$@.tmp '    SVNParentPath /tmp'
	echo >>$@.tmp '    Require all granted'
	echo >>$@.tmp '    #AuthType Basic'
	echo >>$@.tmp '    #AuthName "Subversion Repository"'
	echo >>$@.tmp '    #AuthUserFile $(HTTPD_CHECK_USERS)'
	echo >>$@.tmp '    #Require valid-user'
ifeq ($(USE_HTTPV1),yes)
	echo >> $@.tmp '   SVNAdvertiseV2Protocol off'
endif
ifeq ($(USE_AUTHZ_SHORT_CIRCUIT),yes)
	echo >> $@.tmp '   SVNPathAuthz short_circuit'
endif
	echo >>$@.tmp '</Location>'
	echo >>$@.tmp '# Location for tests using mod_dontdothat'
	echo >>$@.tmp '<Location /ddt-test-work/repositories>'
	echo >> $@.tmp 'DAV               svn'
	echo >> $@.tmp 'SVNParentPath     $(SVN_WC)/subversion/tests/cmdline/svn-test-work/repositories'
	echo >> $@.tmp 'AuthzSVNAccessFile $(SVN_WC)/subversion/tests/cmdline/svn-test-work/authz'
	echo >> $@.tmp 'AuthType          Basic'
	echo >> $@.tmp 'AuthName          "Subversion Repository"'
	echo >> $@.tmp 'AuthzSVNAccessFile $(SVN_WC)/subversion/tests/cmdline/svn-test-work/authz'
	echo >> $@.tmp 'AuthUserFile $(HTTPD_CHECK_USERS)'
	echo >> $@.tmp 'Require           valid-user'
ifeq ($(USE_HTTPV1),yes)
	echo >> $@.tmp '    SVNAdvertiseV2Protocol off'
endif
	echo >> $@.tmp 'DontDoThatConfigFile "$(MOD_DONTDOTHAT_CONF)"'
	echo >> $@.tmp '</Location>'

	echo >>$@.tmp '# Several locations for mod_authz_svn test follow'
	echo >>$@.tmp '<Location /authz-test-work/anon>'
	echo >>$@.tmp '  DAV               svn'
	echo >>$@.tmp '  SVNParentPath     $(SVN_WC)/subversion/tests/cmdline/svn-test-work/local_tmp'
	echo >>$@.tmp '  AuthzSVNAccessFile $(SVN_WC)/subversion/tests/cmdline/svn-test-work/authz'
ifeq ($(USE_HTTPV1),yes)
       echo >>$@.tmp '    SVNAdvertiseV2Protocol off'
endif
	echo >>$@.tmp '  SVNListParentPath On'
	echo >>$@.tmp '  <IfModule mod_authz_core.c>'
	echo >>$@.tmp '    Require all granted'
	echo >>$@.tmp '  </IfModule>'
	echo >>$@.tmp '  <IfModule !mod_authz_core.c>'
	echo >>$@.tmp '    Requite all granted'
	echo >>$@.tmp '  </IfModule>'
ifeq ($(USE_AUTHZ_SHORT_CIRCUIT),yes)
	echo >>$@.tmp '    SVNPathAuthz short_circuit'
endif
	echo >>$@.tmp '</Location>'
	echo >>$@.tmp '<Location /authz-test-work/mixed>'
	echo >>$@.tmp '  DAV               svn'
	echo >>$@.tmp '  SVNParentPath     $(SVN_WC)/subversion/tests/cmdline/svn-test-work/local_tmp'
	echo >>$@.tmp '  AuthzSVNAccessFile $(SVN_WC)/subversion/tests/cmdline/svn-test-work/authz'
ifeq ($(USE_HTTPV1),yes)
	echo >>$@.tmp '    SVNAdvertiseV2Protocol off'
endif
	echo >>$@.tmp '  SVNListParentPath On'
	echo >>$@.tmp '  AuthType          Basic'
	echo >>$@.tmp '  AuthName          "Subversion Repository"'
	echo >>$@.tmp '  AuthUserFile      $(HTTPD_CHECK_USERS)'
	echo >>$@.tmp '  Require           valid-user'
	echo >>$@.tmp '  Satisfy           Any'
ifeq ($(USE_AUTHZ_SHORT_CIRCUIT),yes)
	echo >>$@.tmp '    SVNPathAuthz short_circuit'
endif
	echo >>$@.tmp '</Location>'
	echo >>$@.tmp '<Location /authz-test-work/mixed-noauthwhenanon>'
	echo >>$@.tmp '  DAV               svn'
	echo >>$@.tmp '  SVNParentPath     $(SVN_WC)/subversion/tests/cmdline/svn-test-work/local_tmp'
	echo >>$@.tmp '  AuthzSVNAccessFile $(SVN_WC)/subversion/tests/cmdline/svn-test-work/authz'
ifeq ($(USE_HTTPV1),yes)
	echo >>$@.tmp '    SVNAdvertiseV2Protocol off'
endif
	echo >>$@.tmp '  SVNListParentPath On'
	echo >>$@.tmp '  AuthType          Basic'
	echo >>$@.tmp '  AuthName          "Subversion Repository"'
	echo >>$@.tmp '  AuthUserFile      $(HTTPD_CHECK_USERS)'
	echo >>$@.tmp '  Require           valid-user'
	echo >>$@.tmp '  AuthzSVNNoAuthWhenAnonymousAllowed On'
	echo >>$@.tmp '  SVNPathAuthz On'
ifeq ($(USE_AUTHZ_SHORT_CIRCUIT),yes)
	echo >>$@.tmp '    SVNPathAuthz short_circuit'
endif
	echo >>$@.tmp '</Location>'
	echo >>$@.tmp '<Location /authz-test-work/authn>'
	echo >>$@.tmp '  DAV               svn'
	echo >>$@.tmp '  SVNParentPath     $(SVN_WC)/subversion/tests/cmdline/svn-test-work/local_tmp'
	echo >>$@.tmp '  AuthzSVNAccessFile $(SVN_WC)/subversion/tests/cmdline/svn-test-work/authz'
ifeq ($(USE_HTTPV1),yes)
	echo >>$@.tmp '    SVNAdvertiseV2Protocol off'
endif
	echo >>$@.tmp '  SVNListParentPath On'
	echo >>$@.tmp '  AuthType          Basic'
	echo >>$@.tmp '  AuthName          "Subversion Repository"'
	echo >>$@.tmp '  AuthUserFile      $(HTTPD_CHECK_USERS)'
	echo >>$@.tmp '  Require           valid-user'
ifeq ($(USE_AUTHZ_SHORT_CIRCUIT),yes)
	echo >>$@.tmp '    SVNPathAuthz short_circuit'
endif
	echo >>$@.tmp '</Location>'
	echo >>$@.tmp '<Location /authz-test-work/authn-anonoff>'
	echo >>$@.tmp '  DAV               svn'
	echo >>$@.tmp '  SVNParentPath     $(SVN_WC)/subversion/tests/cmdline/svn-test-work/local_tmp'
	echo >>$@.tmp '  AuthzSVNAccessFile $(SVN_WC)/subversion/tests/cmdline/svn-test-work/authz'
ifeq ($(USE_HTTPV1),yes)
	echo >>$@.tmp '    SVNAdvertiseV2Protocol off'
endif
	echo >>$@.tmp '  SVNListParentPath On'
	echo >>$@.tmp '  AuthType          Basic'
	echo >>$@.tmp '  AuthName          "Subversion Repository"'
	echo >>$@.tmp '  AuthUserFile      $(HTTPD_CHECK_USERS)'
	echo >>$@.tmp '  Require           valid-user'
	echo >>$@.tmp '  AuthzSVNAnonymous Off'
	echo >>$@.tmp '  SVNPathAuthz On'
	echo >>$@.tmp '</Location>'
	echo >>$@.tmp '<Location /authz-test-work/authn-lcuser>'
	echo >>$@.tmp '  DAV               svn'
	echo >>$@.tmp '  SVNParentPath     $(SVN_WC)/subversion/tests/cmdline/svn-test-work/local_tmp'
	echo >>$@.tmp '  AuthzSVNAccessFile $(SVN_WC)/subversion/tests/cmdline/svn-test-work/authz'
ifeq ($(USE_HTTPV1),yes)
	echo >>$@.tmp '    SVNAdvertiseV2Protocol off'
endif
	echo >>$@.tmp '  SVNListParentPath On'
	echo >>$@.tmp '  AuthType          Basic'
	echo >>$@.tmp '  AuthName          "Subversion Repository"'
	echo >>$@.tmp '  AuthUserFile      $(HTTPD_CHECK_USERS)'
	echo >>$@.tmp '  Require           valid-user'
	echo >>$@.tmp '  AuthzForceUsernameCase Lower'
ifeq ($(USE_AUTHZ_SHORT_CIRCUIT),yes)
	echo >>$@.tmp '    SVNPathAuthz short_circuit'
endif
	echo >>$@.tmp '</Location>'
	echo >>$@.tmp '<Location /authz-test-work/authn-group>'
	echo >>$@.tmp '  DAV               svn'
	echo >>$@.tmp '  SVNParentPath     $(SVN_WC)/subversion/tests/cmdline/svn-test-work/local_tmp'
	echo >>$@.tmp '  AuthzSVNAccessFile $(SVN_WC)/subversion/tests/cmdline/svn-test-work/authz'
ifeq ($(USE_HTTPV1),yes)
	echo >>$@.tmp '    SVNAdvertiseV2Protocol off'
endif
	echo >>$@.tmp '  SVNListParentPath On'
	echo >>$@.tmp '  AuthType          Basic'
	echo >>$@.tmp '  AuthName          "Subversion Repository"'
	echo >>$@.tmp '  AuthUserFile      $(HTTPD_CHECK_USERS)'
	echo >>$@.tmp '  AuthGroupFile     $(HTTPD_CHECK_GROUPS)'
	echo >>$@.tmp '  Require           group random'
	echo >>$@.tmp '  AuthzSVNAuthoritative Off'
	echo >>$@.tmp '  SVNPathAuthz On'
	echo >>$@.tmp '</Location>'
	echo >>$@.tmp '<IfModule mod_authz_core.c>'
	echo >>$@.tmp '  <Location /authz-test-work/sallrany>'
	echo >>$@.tmp '    DAV               svn'
	echo >>$@.tmp '    SVNParentPath     $(SVN_WC)/subversion/tests/cmdline/svn-test-work/local_tmp'
	echo >>$@.tmp '    AuthzSVNAccessFile $(SVN_WC)/subversion/tests/cmdline/svn-test-work/authz'
ifeq ($(USE_HTTPV1),yes)
	echo >>$@.tmp '    SVNAdvertiseV2Protocol off'
endif
	echo >>$@.tmp '    SVNListParentPath On'
	echo >>$@.tmp '    AuthType          Basic'
	echo >>$@.tmp '    AuthName          "Subversion Repository"'
	echo >>$@.tmp '    AuthUserFile      $(HTTPD_CHECK_USERS)'
	echo >>$@.tmp '    AuthzSendForbiddenOnFailure On'
	echo >>$@.tmp '    Satisfy All'
	echo >>$@.tmp '    <RequireAny>'
	echo >>$@.tmp '      Require valid-user'
	echo >>$@.tmp '      Require expr req("ALLOW") == "1"'
	echo >>$@.tmp '    </RequireAny>'
ifeq ($(USE_AUTHZ_SHORT_CIRCUIT),yes)
	echo >>$@.tmp '    SVNPathAuthz short_circuit'
endif
	echo >>$@.tmp '  </Location>'
	echo >>$@.tmp '  <Location /authz-test-work/sallrall>'
	echo >>$@.tmp '    DAV               svn'
	echo >>$@.tmp '    SVNParentPath     $(SVN_WC)/subversion/tests/cmdline/svn-test-work/local_tmp'
	echo >>$@.tmp '    AuthzSVNAccessFile $(SVN_WC)/subversion/tests/cmdline/svn-test-work/authz'
ifeq ($(USE_HTTPV1),yes)
	echo >>$@.tmp '    SVNAdvertiseV2Protocol off'
endif
	echo >>$@.tmp '    SVNListParentPath On'
	echo >>$@.tmp '    AuthType          Basic'
	echo >>$@.tmp '    AuthName          "Subversion Repository"'
	echo >>$@.tmp '    AuthUserFile      $(HTTPD_CHECK_USERS)'
	echo >>$@.tmp '    AuthzSendForbiddenOnFailure On'
	echo >>$@.tmp '    Satisfy All'
	echo >>$@.tmp '    <RequireAll>'
	echo >>$@.tmp '      Require valid-user'
	echo >>$@.tmp '      Require expr req("ALLOW") == "1"'
	echo >>$@.tmp '    </RequireAll>'
ifeq ($(USE_AUTHZ_SHORT_CIRCUIT),yes)
	echo >>$@.tmp '    SVNPathAuthz short_circuit'
endif
	echo >>$@.tmp '  </Location>'
	echo >>$@.tmp '</IfModule>'
	echo >>$@.tmp 'RedirectMatch permanent ^/svn-test-work/repositories/REDIRECT-PERM-(.*)$$ /svn-test-work/repositories/$$1'
	echo >>$@.tmp 'RedirectMatch ^/svn-test-work/repositories/REDIRECT-TEMP-(.*)$$ /svn-test-work/repositories/$$1'
	echo >>$@.tmp 'IncludeOptional "conf/$(SVN_REL_WC)*-custom.conf"'
	echo >> $@.tmp '#SVNInMemoryCacheSize 0'
	echo >> $@.tmp '#SVNCacheTextDeltas Off'
	echo >> $@.tmp '#SVNCacheRevProps Off'
	mv -f $@.tmp $@

$(HTTPD_PROXY_CONF): $(HTTPD_CHECK_CONF)
	mkdir -p $(dir $@)
	echo > $@.tmp '# httpd config for a write-through proxy'
	echo >>$@.tmp 'ServerRoot "$(PREFIX)/httpd"'
	echo >>$@.tmp 'Listen localhost:$(HTTPD_PROXY_PORT)'
	echo >>$@.tmp 'LoadModule unixd_module modules/mod_unixd.so'
	echo >>$@.tmp 'LoadModule alias_module modules/mod_alias.so'
	echo >>$@.tmp 'LoadModule access_compat_module modules/mod_access_compat.so'
	echo >>$@.tmp 'LoadModule authn_core_module modules/mod_authn_core.so'
	echo >>$@.tmp 'LoadModule authn_file_module modules/mod_authn_file.so'
	echo >>$@.tmp 'LoadModule authz_core_module modules/mod_authz_core.so'
	echo >>$@.tmp 'LoadModule authz_user_module modules/mod_authz_user.so'
	echo >>$@.tmp 'LoadModule authz_groupfile_module modules/mod_authz_groupfile.so'
	echo >>$@.tmp 'LoadModule auth_basic_module modules/mod_auth_basic.so'
	echo >>$@.tmp 'LoadModule dav_module modules/mod_dav.so'
	echo >>$@.tmp 'LoadModule dav_svn_module $(MOD_DAV_SVN)'
	echo >>$@.tmp 'LoadModule authz_svn_module $(MOD_AUTHZ_SVN)'
	echo >>$@.tmp 'LoadModule dontdothat_module $(MOD_DONTDOTHAT)'
	echo >>$@.tmp 'DocumentRoot "$(PREFIX)/httpd/htdocs"'
	echo >>$@.tmp '# This Location lets you access repositories dropped in /tmp/svn-$(BRANCH)-proxy'
	echo >>$@.tmp '<Location /svn>'
	echo >>$@.tmp '    DAV svn'
	echo >>$@.tmp '    SVNParentPath /tmp/svn-$(BRANCH)-proxy'
	echo >>$@.tmp '    SVNMasterURI http://localhost:$(HTTPD_CHECK_PORT)/svn/'
	echo >>$@.tmp '    Requite all granted'
	echo >>$@.tmp '    #AuthType Basic'
	echo >>$@.tmp '    #AuthName "Subversion Repository"'
	echo >>$@.tmp '    #AuthUserFile $(HTTPD_CHECK_USERS)'
	echo >>$@.tmp '    #Require valid-user'
ifeq ($(USE_HTTPV1),yes)
	echo >> $@.tmp '   SVNAdvertiseV2Protocol off'
endif
ifeq ($(USE_AUTHZ_SHORT_CIRCUIT),yes)
	echo >> $@.tmp '   SVNPathAuthz short_circuit'
endif
	echo >>$@.tmp '</Location>'
	echo >>$@.tmp '# This Location allows repositories to be synced'
	echo >>$@.tmp '<Location /svn-proxy-sync>'
	echo >>$@.tmp 'DAV svn'
	echo >>$@.tmp 'SVNParentPath /tmp/svn-$(BRANCH)-proxy'
	echo >>$@.tmp 'Require all granted'
	echo >>$@.tmp '</Location>'
	mv -f $@.tmp $@

.PHONY: libpath
libpath:
	@echo export LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):$$LD_LIBRARY_PATH" \
		"PYTHONPATH=$(SVN_PREFIX)/lib/svn-python"
#
# OpenBSD requires an LD_PRELOAD hack to dlopen() libraries linked to
# libpthread (e.g. libsvn_auth_gnome_keyring.so) into executables that
# aren't linked to libpthread.
ifeq ($(UNAME),OpenBSD)
LIB_PTHREAD_HACK=LD_PRELOAD=libpthread.so
endif

.PHONY: start-svnserve stop-svnserve start-httpd stop-httpd

HTTPD_CMD = env LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) $(LIB_PTHREAD_HACK) \
		$(PREFIX)/httpd/bin/apachectl
HTTPD_LOG_ROTATE = mv $(PREFIX)/httpd/logs/error_log $(PREFIX)/httpd/logs/error_log.old
HTTPD_START_CMD = $(HTTPD_CMD) -f $(HTTPD_CHECK_CONF) -k start
HTTPD_START_CMD_PROXY = $(HTTPD_CMD) -f $(HTTPD_PROXY_CONF)
HTTPD_START_CMD_DEBUG = $(HTTPD_START_CMD) -X
HTTPD_STOP_CMD = $(HTTPD_CMD) -f $(HTTPD_CHECK_CONF) -k stop; sleep 3

SVNSERVE_START_CMD = (test -e $(PWD)/svnserve-*.pid && \
			ls $(PWD)/svnserve-*.pid | while read pidfile; do \
				kill `cat "$$pidfile"`; sleep 3; \
				rm -f $$pidfile; \
			done); \
			$(SVN_PREFIX)/bin/svnserve \
			--listen-host 127.0.0.1 \
			--pid-file $(PWD)/svnserve-$(WC).pid \
			-d -r $(svn_builddir)/subversion/tests/cmdline
SVNSERVE_STOP_CMD = kill `cat $(PWD)/svnserve-$(WC).pid`; sleep 3; \
			rm -f $(PWD)/svnserve-$(WC).pid

start-httpd: $(HTTPD_CHECK_CONF)
	-$(HTTPD_LOG_ROTATE)
	$(HTTPD_START_CMD)
	@echo "To run tests over http, run:"
	@echo "    make check BASE_URL=http://localhost:$(HTTPD_CHECK_PORT)"
	@echo "The URL http://localhost:$(HTTPD_CHECK_PORT)/svn/"
	@echo "lets you access repositories dropped into /tmp"

start-httpd-debug: $(HTTPD_CHECK_CONF)
	$(HTTPD_START_CMD_DEBUG) &
	@echo "To run tests over http, run:"
	@echo "    make check BASE_URL=http://localhost:$(HTTPD_CHECK_PORT)"
	@echo "The URL http://localhost:$(HTTPD_CHECK_PORT)/svn/"
	@echo "lets you access repositories dropped into /tmp"
	@echo "Trying to attach gdb to httpd..."
	@sleep 1
	gdb $(PREFIX)/httpd/bin/httpd `cat $(PREFIX)/httpd/logs/httpd.pid`

start-httpd-proxy: $(HTTPD_PROXY_CONF)
	$(HTTPD_START_CMD_PROXY)
	@echo "The URL http://localhost:$(HTTPD_PROXY_PORT)/svn/"
	@echo "lets you access repositories dropped into /tmp/svn-$(BRANCH)-proxy"

stop-httpd: $(HTTPD_CHECK_CONF)
	$(HTTPD_STOP_CMD)

stop-httpd-proxy: $(HTTPD_PROXY_CONF)
	pkill -f '$(PREFIX)/httpd/bin/httpd -f $(HTTPD_PROXY_CONF)'

start-svnserve: $(SVN_OBJDIR)/.compiled
	$(SVNSERVE_START_CMD)

stop-svnserve:
	$(SVNSERVE_STOP_CMD)

define do_check
-cd $(svn_builddir) && for fs in $(SVN_CHECK_FS_BACKENDS); do \
    echo "Begin test: $(subst svn-check-,,$@) x $$fs"; \
    test -d "$(RAMDISK)/tmp" && export TMPDIR="$(RAMDISK)/tmp"; \
    env LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) $(LIB_PTHREAD_HACK) \
        env MAKEFLAGS= make check PARALLEL=$(PARALLEL) CLEANUP=$(CLEANUP) \
	  EXCLUSIVE_WC_LOCKS=$(EXCLUSIVE_WC_LOCKS) \
	  THREADED=$(THREADED) \
	  SVN_BIN_DIR=$(SVN_PREFIX)/bin \
	  MEMCACHED_SERVER=$(MEMCACHED_SERVER) $1 FS_TYPE=$$fs; \
    for log in tests.log fails.log; do \
        test -f $$log && mv -f $$log $$log.$@-$$fs; \
    done; \
done
endef

TEST_WORK=$(svn_builddir)/subversion/tests/cmdline/svn-test-work
svn-check-prepare-ramdisk:
	-rm -rf "$(TEST_WORK)"; \
	if [ -d "$(RAMDISK)" ] && \
		touch "$(RAMDISK)/$(SVN_REL_WC).writetest" && \
		mkdir -p "$(RAMDISK)/$(SVN_REL_WC)"; then \
			rm -f "$(RAMDISK)/$(SVN_REL_WC).writetest"; \
			ln -s "$(RAMDISK)/$(SVN_REL_WC)" "$(TEST_WORK)"; \
			mkdir -p "$(RAMDISK)/tmp"; \
	fi

ifndef NEON_FLAG
svn-check-neon:
	@echo Neon is not supported by this build of Subversion, skipping tests
	@true
else
svn-check-neon: $(HTTPD_CHECK_CONF) $(SVN_OBJDIR)/.compiled $(SVN_OBJDIR)/.bindings-compiled svn-check-prepare-ramdisk
	$(HTTPD_START_CMD)
	$(call do_check,BASE_URL=http://localhost:$(HTTPD_CHECK_PORT) HTTP_LIBRARY=neon)
	$(HTTPD_STOP_CMD)
endif

svn-check-serf: $(HTTPD_CHECK_CONF) $(SVN_OBJDIR)/.compiled $(SVN_OBJDIR)/.bindings-compiled svn-check-prepare-ramdisk
	$(HTTPD_START_CMD)
	$(call do_check,BASE_URL=http://localhost:$(HTTPD_CHECK_PORT) HTTP_LIBRARY=serf)
	$(HTTPD_STOP_CMD)

svn-check-local: svn-check-prepare-ramdisk
	$(call do_check)

svn-check-svn: svn-check-prepare-ramdisk
	$(SVNSERVE_START_CMD)
	$(call do_check,BASE_URL=svn://127.0.0.1)
	$(SVNSERVE_STOP_CMD)

.PHONY: svn-check-swig-pl svn-check-swig-py svn-check-swig-rb svn-check-javahl
svn-check-bindings: svn-check-swig-pl svn-check-swig-py svn-check-swig-rb \
	svn-check-javahl

RUBYLIB=$(SVN_PREFIX)/lib/ruby/site_ruby$(shell grep \
	^svn_cv_ruby_sitedir_archsuffix $(svn_builddir)/config.log | \
	cut -d'=' -f2):$(SVN_PREFIX)/lib/ruby/site_ruby$(shell \
	grep ^svn_cv_ruby_sitedir_libsuffix $(svn_builddir)/config.log | \
	cut -d'=' -f2)
svn-check-swig-pl:
	-if [ $(ENABLE_PERL_BINDINGS) = yes ]; then \
		(cd $(svn_builddir) && \
			env LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) \
			$(LIB_PTHREAD_HACK) \
			env MAKEFLAGS= make check-swig-pl 2>&1) | \
				tee $(svn_builddir)/tests.log.bindings.pl; \
	fi

svn-check-swig-py:
	-(cd $(svn_builddir) && \
		env LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) \
		env MAKEFLAGS= make check-swig-py 2>&1) | \
			tee $(svn_builddir)/tests.log.bindings.py

# We add the svn prefix to PATH here because the ruby tests
# attempt to start an svnserve binary found in PATH.
svn-check-swig-rb:
	(cd $(svn_builddir) && \
		env RUBYLIB=$(RUBYLIB) \
		LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) \
		PATH=$(SVN_PREFIX)/bin:$$PATH \
		$(LIB_PTHREAD_HACK) \
			env MAKEFLAGS= make check-swig-rb 2>&1) | \
			tee $(svn_builddir)/tests.log.bindings.rb

svn-check-javahl:
	-if [ $(ENABLE_JAVA_BINDINGS) = yes ]; then \
		ln -s libsvnjavahl-1.so.0.0 $(PREFIX)/svn-$(WC)/lib/libsvnjavahl-1.so; \
		(cd $(svn_builddir) && \
			env LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) \
			MAKEFLAGS= make $(JAVAHL_CHECK_TARGET) 2>&1) | \
				tee $(svn_builddir)/tests.log.bindings.javahl; \
	fi

svn-check: svn-check-prepare-ramdisk svn-check-local svn-check-svn \
	svn-check-neon svn-check-serf svn-check-bindings

.PHONY: sign-email
ifdef NEON_FLAG
NEON_STR=ra_neon | 
NEON_VER_LINE=@echo "neon:       $(NEON_VER)"
endif
sign-email:
	@echo "Summary: +1 to release"
	@echo ""
	@echo "Tested: [bdb | fsfs] x [ra_local | ra_svn | $(NEON_STR)ra_serf]"
	@echo "        swig bindings"
ifeq ($(ENABLE_JAVA_BINDINGS),yes)
	@echo "        javahl bindings"
endif
	@echo ""
	@echo "Test results: All passed."
	@echo ""
	@echo "Platform: `uname -r -s -m`"
	@echo ""
	@echo "Dependencies:"
	@echo "bdb:        $(BDB_VER)"
ifeq ($(USE_APR_ICONV),yes)
	@echo "apr-iconv:  $(APR_ICONV_VER)"
else
	@echo "GNU-iconv:  $(GNU_ICONV_VER)"
endif
	@echo "apr:        $(APR_VER)"
	@echo "apr-util:   $(APR_UTIL_VER)"
	@echo "httpd:      $(HTTPD_VER)"
	$(NEON_VER_LINE)
	@echo "serf:       $(SERF_VER)"
	@echo "cyrus-sasl: $(CYRUS_SASL_VER)"
	@echo "sqlite:     $(SQLITE_VER)"
ifdef LZ4_FLAG
	@echo "lz4:        $(LZ4_VER)"
endif
	@echo "libssl:     `openssl version`"
	@echo "swig:       `swig -version | grep Version | cut -d' ' -f3`"
	@echo "python:     $(PYTHON_VER)"
	@echo "perl:       `eval \`perl -V:version\`; echo $$version`"
	@echo "ruby:       $(RUBY_VER)"
ifeq ($(ENABLE_JAVA_BINDINGS),yes)
	@echo "java:       `java -version 2>&1 | grep version | cut -d' ' -f3  | sed -e 's/\"//g'`"
endif
	@echo ""
	@echo "Signatures:"
	@echo
	@echo "subversion-$(TAG).tar.gz"
	@echo "`cat subversion-$(TAG).tar.gz.asc`" 
	@echo
	@echo "subversion-$(TAG).tar.bz2"
	@echo "`cat subversion-$(TAG).tar.bz2.asc`" 
