#!/usr/bin/make -f

pam_types = account auth session password
pam_krb5_auth_opts = use_authtok
pam_krb5_passwd_opts = use_authtok

DEB_AUTO_UPDATE_DEBIAN_CONTROL = 1
DEB_DIVERT_EXTENSION = .debathena

ifneq ($(wildcard /etc/pam.d/sshd),)
    PAM_SSHD = /etc/pam.d/sshd.debathena
    DEB_TRANSFORM_SCRIPT_$(PAM_SSHD) = debian/transform_ssh.debathena
else
    PAM_SSHD = /etc/pam.d/ssh.debathena
endif

ifneq ($(wildcard /usr/share/pam/common-auth),)
    DEB_CHECK_FILES_SOURCE_/etc/pam.d/common-auth.debathena = /usr/share/pam/common-auth
    DEB_CHECK_FILES_SOURCE_/etc/pam.d/common-session.debathena = /usr/share/pam/common-session
    DEB_CHECK_FILES_SOURCE_/etc/pam.d/common-account.debathena = /usr/share/pam/common-account
    DEB_CHECK_FILES_SOURCE_/etc/pam.d/common-password.debathena = /usr/share/pam/common-password
endif

DEB_TRANSFORM_FILES_debathena-pam-config += \
	$(patsubst %,/etc/pam.d/common-%.debathena,$(pam_types)) \
	/etc/pam.d/gdm.debathena \
	$(PAM_SSHD) \
	/etc/pam.d/login.debathena

DEB_DIVERT_FILES_debathena-pam-config += \
	/etc/security/access.conf.debathena

include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/rules/config-package.mk

ifneq ($(wildcard /etc/pam.d/sshd),)
debian-divert/debathena-pam-config::
	(echo "if [ \"\$$1\" = configure ] && [ -n \"\$$2\" ]; then"; \
	echo "    undivert_unlink /etc/pam.d/ssh"; \
	echo "fi") >> $(CURDIR)/debian/$(cdbs_curpkg).postinst.debhelper
endif

common-build-indep:: debian/access.conf.debathena

LSB_ID = $(shell lsb_release --short --id)
debian/access.conf.debathena: $(call debian_check_files,/etc/security/access.conf)
ifeq ($(LSB_ID), Debian)
	(cat $<; sed 's/root admin /root /' debian/access.conf.append) > $@
else
    ifeq ($(LSB_ID), Ubuntu)
	cat $< debian/access.conf.append > $@
    else
	$(error Unrecognized distribution ID $(LSB_ID).)
    endif
endif

clean::
	rm -f debian/access.conf.debathena
