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

export DH_VERBOSE=1
export SHELL := /bin/bash

DPKG_VARS				:= $(shell dpkg-architecture)
DEB_BUILD_MULTIARCH		?= $(call vafilt,$(DPKG_VARS),DEB_BUILD_MULTIARCH)
DEB_HOST_MULTIARCH		?= $(call vafilt,$(DPKG_VARS),DEB_HOST_MULTIARCH)

srcname		:= redis
name		:= alt-$(srcname)
_sourcedir	:= debian/source
_prefix		:= /opt/alt/$(srcname)
_bindir		:= $(_prefix)/bin
_libdir		:= $(_prefix)/lib/$(DEB_HOST_MULTIARCH)
_datadir	:= $(_prefix)/share
_includedir := $(_prefix)/include
_libexecdir	:= $(_prefix)/libexec
_mandir		:= $(_datadir)/man
_sysconfdir	:= /etc
_localstatedir	:= /var
_sharedstatedir	:= $(_localstatedir)/lib
  _unitdir	:= /lib/systemd/system
_initrddir	:= /etc/init.d

buildroot	:= $(CURDIR)/debian/tmp

redis_modules_abi	:= 1
redis_modules_dir	:= $(_libdir)/$(srcname)/modules

CFLAGS := $(shell dpkg-buildflags --get CFLAGS)
LDFLAGS := $(shell dpkg-buildflags --get LDFLAGS)
#LDFLAGS := $(LDFLAGS) -Wl,-rpath=/opt/alt/sqlite/usr/$(DEB_BUILD_MULTIARCH)

NJOBS := -j$(shell getconf _NPROCESSORS_ONLN)

%:
	dh $@

override_dh_auto_configure:
	patch -p1 < debian/patches/0001-1st-man-pageis-for-redis-cli-redis-benchmark-redis-c.patch
	mv deps/jemalloc/COPYING COPYING-jemalloc
	mv deps/lua/COPYRIGHT COPYRIGHT-lua
	mv deps/hiredis/COPYING  COPYING-hiredis

	# Configuration file changes
	sed -i -e 's|^logfile .*$$|logfile /var/log/redis/redis.log|g' redis.conf
	sed -i -e 's|^logfile .*$$|logfile /var/log/redis/sentinel.log|g' sentinel.conf
	sed -i -e 's|^dir .*$$|dir /var/lib/redis|g' redis.conf

	# Module API version safety check
api := $(shell sed -n -e 's!.define REDISMODULE_APIVER_[0-9][0-9]* !!p' src/redismodule.h)
ifneq ($(api),$(redis_modules_abi))
	echo "Error: Upstream API version is now $(api), expecting $(redis_modules_abi)."
	echo "Update the redis_modules_abi macro, the rpmmacros file, and rebuild."
	exit 1
endif

malloc_flags := MALLOC=jemalloc

  tls_flags := BUILD_TLS=yes

  sysd_flags := BUILD_WITH_SYSTEMD=yes

make_flags := DEBUG="" V="echo" LDFLAGS="$(LDFLAGS)" CFLAGS="$(CFLAGS) -fPIC" INSTALL="install -p" PREFIX=$(buildroot)$(_prefix) $(malloc_flags) $(tls_flags) $(sysd_flags)

override_dh_auto_build:
	$(MAKE) $(NJOBS) $(make_flags) all

override_dh_auto_install:
	$(MAKE) $(make_flags) install

	# Filesystem.
	install -d $(buildroot)$(_sharedstatedir)/$(srcname)
	install -d $(buildroot)$(_localstatedir)/log/$(srcname)
	install -d $(buildroot)$(_localstatedir)/run/$(srcname)
	install -d $(buildroot)$(redis_modules_dir)

	# Install logrotate file.
	install -pDm644 $(_sourcedir)/$(name).logrotate $(buildroot)$(_sysconfdir)/logrotate.d/$(srcname)

	# Install configuration files.
	install -pDm640 $(srcname).conf  $(buildroot)$(_sysconfdir)/$(srcname)/$(srcname).conf
	install -pDm640 sentinel.conf $(buildroot)$(_sysconfdir)/$(srcname)/sentinel.conf

	# Install systemd unit files.
	mkdir -p $(buildroot)$(_unitdir)
	install -pm644 $(_sourcedir)/$(name).service $(buildroot)$(_unitdir)/$(name).service
	install -pm644 $(_sourcedir)/$(name)-sentinel.service $(buildroot)$(_unitdir)/$(name)-sentinel.service

	# Install systemd limit files (requires systemd >= 204)
	install -p -D -m 644 $(_sourcedir)/$(name)-limit-systemd $(buildroot)$(systemd_conf)/$(name).service.d/limit.conf
	install -p -D -m 644 $(_sourcedir)/$(name)-limit-systemd $(buildroot)$(systemd_conf)/$(name)-sentinel.service.d/limit.conf

	# Fix non-standard-executable-perm error.
	chmod 755 $(buildroot)$(_bindir)/$(srcname)-*

	# Install redis-shutdown
	install -pDm755 $(_sourcedir)/$(name)-shutdown $(buildroot)$(_libexecdir)/$(srcname)-shutdown

	# Install redis module header
	install -pDm644 src/$(srcname)module.h $(buildroot)$(_includedir)/$(srcname)module.h


	# Install man pages
	man=$(shell dirname $(buildroot)$(_mandir)); \
	for page in man/man?/*; do \
		install -Dpm644 $$page $$man/$$page; \
	done
	ln -s redis-server.1 $(buildroot)$(_mandir)/man1/redis-sentinel.1
	ln -s redis.conf.5   $(buildroot)$(_mandir)/man5/redis-sentinel.conf.5

	# Packaging
	dh_movefiles -p$(name) \
		.$(_bindir)/$(srcname)-* \
		.$(_libexecdir)/$(srcname)-* \
		.$(_mandir)/man1/$(srcname)* \
		.$(_mandir)/man5/$(srcname)* \
		.$(_sysconfdir)/$(srcname)/$(srcname).conf \
		.$(_sysconfdir)/$(srcname)/sentinel.conf \
		.$(_sysconfdir)/logrotate.d/$(srcname)

	dh_movefiles -p$(name) \
		.$(_unitdir)/$(name)-sentinel.service \
		.$(_unitdir)/$(name).service \
		.$(systemd_conf)/$(name)-sentinel.service.d/limit.conf \
		.$(systemd_conf)/$(name).service.d/limit.conf

	dh_installdirs -p$(name) \
		.$(_libdir)/$(srcname) \
		.$(_localstatedir)/log/$(srcname) \
		.$(_localstatedir)/run/$(srcname) \
		.$(_sharedstatedir)/$(srcname) \
		.$(redis_modules_dir)

	dh_movefiles -p$(name)-devel \
		.$(_includedir)/*

override_dh_installdeb:
	for file in \
		$(_sysconfdir)/logrotate.d/$(srcname) \
		$(_sysconfdir)/$(srcname)/$(srcname).conf \
		$(_sysconfdir)/$(srcname)/sentinel.conf; \
		do \
			echo "$${file}" >> ${CURDIR}/debian/$(name).conffiles; \
	done
	for file in \
		$(systemd_conf)/$(name).service.d/limit.conf \
		$(systemd_conf)/$(name)-sentinel.service.d/limit.conf; \
		do \
			echo "$${file}" >> ${CURDIR}/debian/$(name).conffiles; \
	done
	dh_installdeb


override_dh_auto_test:
override_dh_auto_clean:

override_dh_builddeb:
	dh_builddeb
	if [ $(shell find $(buildroot) -type f | wc -l) -gt 0 ]; then \
		echo "Installed but unpackaged:"; \
		find $(buildroot) -type f -exec echo "{}" \; | sed -e 's#$(buildroot)##g'; \
		exit 1; \
	fi
