#!/usr/bin/make -f

DEB_AUTO_UPDATE_AUTOCONF = 2.50
include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/class/autotools.mk

ATHENA_MAJOR_VERSION = 10
ATHENA_MINOR_VERSION = 0

arch_sysnames = $(if $(filter amd64,$(DEB_BUILD_ARCH)),$(1) $(2),$(if $(filter i386,$(DEB_BUILD_ARCH)),$(2),$(error Unrecognized architecture $(DEB_BUILD_ARCH))))

DISTRO := $(shell lsb_release --short --id)
# Ubuntu releases have Debian versions, but not vice-versa
DEBIAN_VERSION := $(shell dpkg-query --showformat='$${Version}' --show base-files)
UBUNTU_VERSION := $(shell lsb_release --short --release)

compare_debian_version = $(shell dpkg --compare-versions $(DEBIAN_VERSION) '>=' $(1) && echo y)
compare_ubuntu_version = $(shell dpkg --compare-versions $(UBUNTU_VERSION) '>=' $(1) && echo y)

SYS_COMPAT :=

ifneq ($(call compare_debian_version,3.1),y)
    $(error Debian version $(DEBIAN_VERSION) is too old)
endif

# The order of the following code is important, and ensures the correct
# relative order of Debian and Ubuntu releases. (Note this is not quite
# chronological order, e.g., Debian 6.0 released before Ubuntu 11.04,
# but after its DebianImportFreeze.)

# Wheezy has had like 3 freezes, all before raring's DIF, so
# who knows if this order is right.
ifeq ($(DISTRO),Ubuntu)
    ifeq ($(call compare_ubuntu_version,13.04),y)
        SYS_COMPAT += $(call arch_sysnames,amd64_ubuntu1210,i386_ubuntu1210)
    endif
endif

ifeq ($(call compare_debian_version,7),y)
    SYS_COMPAT += $(call arch_sysnames,amd64_deb70,i386_deb70)
endif

ifeq ($(DISTRO),Ubuntu)
    ifeq ($(call compare_ubuntu_version,12.10),y)
        SYS_COMPAT += $(call arch_sysnames,amd64_ubuntu1210,i386_ubuntu1210)
    endif
    ifeq ($(call compare_ubuntu_version,12.04),y)
        SYS_COMPAT += $(call arch_sysnames,amd64_ubuntu1204,i386_ubuntu1204)
    endif
    ifeq ($(call compare_ubuntu_version,11.10),y)
        SYS_COMPAT += $(call arch_sysnames,amd64_ubuntu1110,i386_ubuntu1110)
    endif
endif

ifeq ($(call compare_debian_version,6),y)
    SYS_COMPAT += $(call arch_sysnames,amd64_deb60,i386_deb60)
endif

ifeq ($(DISTRO),Ubuntu)
    ifeq ($(call compare_ubuntu_version,11.04),y)
        SYS_COMPAT += $(call arch_sysnames,amd64_ubuntu1104,i386_ubuntu1104)
    endif

    ifeq ($(call compare_ubuntu_version,10.10),y)
        SYS_COMPAT += $(call arch_sysnames,amd64_ubuntu1010,i386_ubuntu1010)
    endif

    ifeq ($(call compare_ubuntu_version,10.04),y)
        SYS_COMPAT += $(call arch_sysnames,amd64_ubuntu1004,i386_ubuntu1004)
    endif

    ifeq ($(call compare_ubuntu_version,9.10),y)
        SYS_COMPAT += $(call arch_sysnames,amd64_ubuntu910,i386_ubuntu910)
    endif

    ifeq ($(call compare_ubuntu_version,9.04),y)
        SYS_COMPAT += $(call arch_sysnames,amd64_ubuntu904,i386_ubuntu904)
    endif
endif

ifeq ($(call compare_debian_version,4.0.4),y)
    SYS_COMPAT += $(call arch_sysnames,amd64_deb50,i386_deb50)
endif

ifeq ($(DISTRO),Ubuntu)
    ifeq ($(call compare_ubuntu_version,8.04),y)
        SYS_COMPAT += $(call arch_sysnames,amd64_ubuntu804,i386_ubuntu804)
    endif
endif

ifeq ($(call compare_debian_version,4),y)
    SYS_COMPAT += $(call arch_sysnames,amd64_deb40,i386_deb40 i386_rhel4)
endif

ifneq ($(call compare_debian_version,7),y)
    SYS_COMPAT += i386_rhel3 i386_linux24
endif

OPENAFS_VERSION := $(shell dpkg-query --showformat='$${Version}' --show openafs-client)
MAX_SYSNAMES_PLUS_ONE := $(if $(shell dpkg --compare-versions $(OPENAFS_VERSION) '>=' '1.4.12+dfsg-3~' && echo y),33,17)

ifneq ($(word $(MAX_SYSNAMES_PLUS_ONE),$(SYS_COMPAT)),)
    $(error Too many sysnames: $(SYSNAME_COMPAT))
endif

ATHENA_SYS := $(word 1, $(SYS_COMPAT))
ATHENA_SYS_COMPAT := $(patsubst %:,%,$(subst : ,:,$(patsubst %,%:,$(wordlist 2, $(words $(SYS_COMPAT)), $(SYS_COMPAT)))))

DEB_CONFIGURE_EXTRA_FLAGS += lbindir=/bin \
	ATHENA_SYS=$(ATHENA_SYS) \
	ATHENA_SYS_COMPAT=$(ATHENA_SYS_COMPAT) \
	ATHENA_MAJOR_VERSION=$(ATHENA_MAJOR_VERSION) \
	ATHENA_MINOR_VERSION=$(ATHENA_MINOR_VERSION)

clean::
	rm -f configure
