#
# use gmake instead BSD make
#

CONFIGURE_DIR=..
M4_DIR=../m4
ACLS_DIR=../acls
FEATURES_DIR=../features
SITE_DIR=../site
SYSTEMFILTERS_DIR=../system_filters
DELIVERIES_DIR=../deliveries
VIRT_DOMAINS_DIR=../virt_domains
PERL_DIR=../perl
BIN_DIR=../bin
PERIODIC_DIR=../periodic
PERIODIC_M4_DIR=../m4/periodic
PERIODIC_SITE_DIR=../site/periodic
SAMPLES_DIR=../samples
ENTERPRISE_DIR=../enterprise
CONTRIB_DIR=../contrib
DLFUNC_DIR=../dlfunc

OS := $(shell uname)

XARGS_L    := xargs -L 1 
EXIM_USER  := mailnull
EXIM_GROUP := mail
ECHO       := /bin/echo

ECHO       := $(shell if test "${OS}" = "SunOS"     ; then /usr/bin/echo -n '/usr/bin/echo'	; else ${ECHO} -n "${ECHO}"       ; fi)
XARGS_L    := $(shell if test "${OS}" = "SunOS"     ; then /usr/bin/echo -n 'xargs -L 1 '	; else ${ECHO} -n "${XARGS_L}"    ; fi)

OS2 := $(shell if test -f /etc/redhat-release       ; then echo 'RedHat'			; else ${ECHO} -n "${OS2}"        ; fi)
OS2 := $(shell if test -f /etc/gentoo-release       ; then echo 'Gentoo'			; else ${ECHO} -n "${OS2}"        ; fi)
OS2 := $(shell if test -f /etc/SuSE-release         ; then echo 'SuSE'				; else ${ECHO} -n "${OS2}"        ; fi)
OS2 := $(shell if test -f /etc/debian_version       ; then echo 'Debian'			; else ${ECHO} -n "${OS2}"        ; fi)

PKGSRC     := $(shell if test -d /usr/pkg           ; then ${ECHO} -n "YES"			; else ${ECHO} -n "NO"            ; fi)
DLFUNC_FNAME := $(shell if test "${OS}" = "Darwin"  ; then ${ECHO} -n 'exim-dlfunc.dylib'	; else ${ECHO} -n 'exim-dlfunc.so' ; fi)

EXIM_USER  := $(shell if test "${OS}" = "SunOS"     ; then if test -d /usr/pkg ; then /usr/bin/echo -n 'mail' ; else /usr/bin/echo -n 'exim' ; fi ; else echo -n "${EXIM_USER}"  ; fi)
EXIM_GROUP := $(shell if test "${OS}" = "SunOS"     ; then if test -d /usr/pkg ; then /usr/bin/echo -n 'mail' ; else /usr/bin/echo -n 'exim' ; fi ; else echo -n "${EXIM_GROUP}" ; fi)

EXIM_USER  := $(shell if test "${OS}" = "Darwin"    ; then if test -d /usr/pkg ; then ${ECHO} -n 'mail' ; else ${ECHO} -n 'exim' ; fi ; else ${ECHO} -n "${EXIM_USER}"  ; fi)
EXIM_GROUP := $(shell if test "${OS}" = "Darwin"    ; then if test -d /usr/pkg ; then ${ECHO} -n 'mail' ; else ${ECHO} -n 'mail' ; fi ; else ${ECHO} -n "${EXIM_GROUP}" ; fi)

XARGS_L    := $(shell if test "${OS}" = "NetBSD"    ; then echo -n 'xargs -n 1 '		; else ${ECHO} -n "${XARGS_L}"    ; fi)
EXIM_USER  := $(shell if test "${OS}" = "NetBSD"    ; then echo -n 'mail'			; else ${ECHO} -n "${EXIM_USER}"  ; fi)
EXIM_GROUP := $(shell if test "${OS}" = "NetBSD"    ; then echo -n 'mail'			; else ${ECHO} -n "${EXIM_GROUP}" ; fi)

XARGS_L    := $(shell if test "${OS}" = "DragonFly" ; then echo -n 'xargs -n 1 '		; else ${ECHO} -n "${XARGS_L}"    ; fi)
EXIM_USER  := $(shell if test "${OS}" = "DragonFly" ; then if test -d /usr/pkg ; then echo -n 'mail' ; else echo -n 'mailnull' ; fi ; else echo -n "${EXIM_USER}"  ; fi)
EXIM_GROUP := $(shell if test "${OS}" = "DragonFly" ; then echo -n 'mail'			; else ${ECHO} -n "${EXIM_GROUP}" ; fi)

XARGS_L    := $(shell if test "${OS}" = "QNX"       ; then echo -n 'xargs -n 1 '		; else ${ECHO} -n "${XARGS_L}"    ; fi)
EXIM_USER  := $(shell if test "${OS}" = "QNX"       ; then echo -n 'mail'			; else ${ECHO} -n "${EXIM_USER}"  ; fi)
EXIM_GROUP := $(shell if test "${OS}" = "QNX"       ; then echo -n 'mail'			; else ${ECHO} -n "${EXIM_GROUP}" ; fi)

XARGS_L    := $(shell if test "${OS}" = "OpenBSD"   ; then echo -n 'xargs -L 1 '		; else ${ECHO} -n "${XARGS_L}"    ; fi)
EXIM_USER  := $(shell if test "${OS}" = "OpenBSD"   ; then echo -n '_exim'			; else ${ECHO} -n "${EXIM_USER}"  ; fi)
EXIM_GROUP := $(shell if test "${OS}" = "OpenBSD"   ; then echo -n '_exim'			; else ${ECHO} -n "${EXIM_GROUP}" ; fi)

XARGS_L    := $(shell if test "${OS}" = "FreeBSD"   ; then echo -n 'xargs -L 1 '		; else ${ECHO} -n "${XARGS_L}"    ; fi)
EXIM_USER  := $(shell if test "${OS}" = "FreeBSD"   ; then echo -n 'mailnull'			; else ${ECHO} -n "${EXIM_USER}"  ; fi)
EXIM_GROUP := $(shell if test "${OS}" = "FreeBSD"   ; then echo -n 'mail'			; else ${ECHO} -n "${EXIM_GROUP}" ; fi)

XARGS_L    := $(shell if test "${OS}" = "Linux"     ; then echo -n 'xargs -l1 '			; else ${ECHO} -n "${XARGS_L}"    ; fi)
EXIM_USER  := $(shell if test "${OS}" = "Linux"     ; then echo -n 'exim'			; else ${ECHO} -n "${EXIM_USER}"  ; fi)
EXIM_GROUP := $(shell if test "${OS}" = "Linux"     ; then echo -n 'mail'			; else ${ECHO} -n "${EXIM_GROUP}" ; fi)

EXIM_USER  := $(shell if test "${OS2}" = "RedHat"   ; then echo -n 'exim'			; else ${ECHO} -n "${EXIM_USER}"  ; fi)
EXIM_GROUP := $(shell if test "${OS2}" = "RedHat"   ; then echo -n 'mail'			; else ${ECHO} -n "${EXIM_GROUP}" ; fi)

EXIM_USER  := $(shell if test "${OS2}" = "Gentoo"   ; then echo -n 'mail'			; else ${ECHO} -n "${EXIM_USER}"  ; fi)
EXIM_GROUP := $(shell if test "${OS2}" = "Gentoo"   ; then echo -n 'mail'			; else ${ECHO} -n "${EXIM_GROUP}" ; fi)

EXIM_USER  := $(shell if test "${OS2}" = "SuSE"     ; then echo -n 'mail'			; else ${ECHO} -n "${EXIM_USER}"  ; fi)
EXIM_GROUP := $(shell if test "${OS2}" = "SuSE"     ; then echo -n 'mail'			; else ${ECHO} -n "${EXIM_GROUP}" ; fi)

EXIM_USER  := $(shell if test "${OS2}" = "Debian"   ; then echo -n 'Debian-exim'		; else ${ECHO} -n "${EXIM_USER}"  ; fi)
EXIM_GROUP := $(shell if test "${OS2}" = "Debian"   ; then echo -n 'Debian-exim'		; else ${ECHO} -n "${EXIM_GROUP}" ; fi)

GM4_1 := $(shell test -f /usr/local/bin/gm4	&& echo /usr/local/bin/gm4)
GM4_2 := $(shell which gm4 2>/dev/null | grep -v "no gm4 in")
M4_1  := $(shell test -f /usr/bin/m4		&& echo /usr/bin/m4)
M4_2  := $(shell which m4 2>/dev/null)
M4    := $(shell if test -z "${M4}" ; then ${ECHO} -n "${GM4_1}" ; else ${ECHO} -n "${M4}" ; fi)
M4    := $(shell if test -z "${M4}" ; then ${ECHO} -n "${GM4_2}" ; else ${ECHO} -n "${M4}" ; fi)
M4    := $(shell if test -z "${M4}" ; then ${ECHO} -n "${M4_1}"  ; else ${ECHO} -n "${M4}" ; fi)
M4    := $(shell if test -z "${M4}" ; then ${ECHO} -n "${M4_2}"  ; else ${ECHO} -n "${M4}" ; fi)
M4    := $(shell if test -z "${M4}" ; then ${ECHO} -n "m4"       ; else ${ECHO} -n "${M4}" ; fi)

MAP_OBJ += $(shell test -f ../mailertable	&& echo ../mailertable.db)
MAP_OBJ += $(shell test -f ../aliases		&& echo ../aliases.db)
MAP_OBJ += $(shell test -f ../virt_aliases	&& echo ../virt_aliases.db)
MAP_OBJ += $(shell test -f ../virtusertable	&& echo ../virtusertable.db)
MAP_OBJ += $(shell ls ${VIRT_DOMAINS_DIR}/*.aliases       2>/dev/null | perl -p -e 's/\.aliases\b/.aliases.db/')
MAP_OBJ += $(shell ls ${VIRT_DOMAINS_DIR}/*.virtusertable 2>/dev/null | perl -p -e 's/\.virtusertable\b/.virtusertable.db/')

ACLS_FILES       += $(shell ls -1d ${ACLS_DIR}/*       2>/dev/null)
FEATURES_FILES   += $(shell ls -1d ${FEATURES_DIR}/*   2>/dev/null)
DELIVERIES_FILES += $(shell ls -1d ${DELIVERIES_DIR}/* 2>/dev/null)
SITE_FILES       += $(shell ls -1d ${SITE_DIR}/*       2>/dev/null)
SITE_PERL_FILES  += $(shell ls -1d ${SITE_DIR}/*.pl    2>/dev/null)

INIT_SCRIPT := $(shell if test -f /etc/rc.exim							; then echo /etc/rc.exim						; else ${ECHO} -n "${INIT_SCRIPT}" ; fi)
INIT_SCRIPT := $(shell if test -f /etc/rc.d/exim.sh						; then echo /etc/rc.d/exim.sh						; else ${ECHO} -n "${INIT_SCRIPT}" ; fi)
INIT_SCRIPT := $(shell if test -f /etc/rc.d/exim						; then echo /etc/rc.d/exim						; else ${ECHO} -n "${INIT_SCRIPT}" ; fi)
INIT_SCRIPT := $(shell if test -f /etc/init.d/exim						; then echo /etc/init.d/exim						; else ${ECHO} -n "${INIT_SCRIPT}" ; fi)
INIT_SCRIPT := $(shell if test -f /etc/init.d/exim4						; then echo /etc/init.d/exim4						; else ${ECHO} -n "${INIT_SCRIPT}" ; fi)
INIT_SCRIPT := $(shell if test -f /usr/local/etc/rc.d/exim.sh					; then echo /usr/local/etc/rc.d/exim.sh					; else ${ECHO} -n "${INIT_SCRIPT}" ; fi)
INIT_SCRIPT := $(shell if test -f /usr/local/etc/rc.d/exim					; then echo /usr/local/etc/rc.d/exim					; else ${ECHO} -n "${INIT_SCRIPT}" ; fi)
INIT_SCRIPT := $(shell if test -f /etc/rc.d/init.d/exim						; then echo /etc/rc.d/init.d/exim					; else ${ECHO} -n "${INIT_SCRIPT}" ; fi)
INIT_SCRIPT := $(shell if test -f /sbin/service							; then echo /sbin/service exim						; else ${ECHO} -n "${INIT_SCRIPT}" ; fi)
INIT_SCRIPT := $(shell if test -f /opt/csw/lib/svc/method/svc-exim				; then echo /opt/csw/lib/svc/method/svc-exim				; else ${ECHO} -n "${INIT_SCRIPT}" ; fi)
INIT_SCRIPT := $(shell if test -f /usr/pkg/etc/rc.d/exim					; then echo /usr/pkg/etc/rc.d/exim					; else ${ECHO} -n "${INIT_SCRIPT}" ; fi)
INIT_SCRIPT := $(shell if test -f /opt/local/etc/rc.d/exim					; then echo /opt/local/etc/rc.d/exim					; else ${ECHO} -n "${INIT_SCRIPT}" ; fi)


build : check pre-install configure system_filter exim.pl exim-dlfunc periodic

build-all : check pre-install configure system_filter exim.pl exim-dlfunc ${MAP_OBJ}
db : ${MAP_OBJ}
all : build-all

reload :
	[ -z "${INIT_SCRIPT}" ] || ${INIT_SCRIPT} reload

restart :
	[ -z "${INIT_SCRIPT}" ] || ${INIT_SCRIPT} restart

start :
	[ -z "${INIT_SCRIPT}" ] || ${INIT_SCRIPT} start

stop :
	[ -z "${INIT_SCRIPT}" ] || ${INIT_SCRIPT} stop

##### configure #####

configure : check ${CONFIGURE_DIR}/configure

${CONFIGURE_DIR}/configure : ${M4_DIR}/configure.m4 ${SITE_DIR}/conf ${M4_DIR}/conf.default ${ACLS_FILES} ${FEATURES_FILES} ${DELIVERIES_FILES} ${SITE_FILES}
	if [ -f ${CONFIGURE_DIR}/configure ]; then cat ${CONFIGURE_DIR}/configure > ${CONFIGURE_DIR}/configure.bak; chmod 640 ${CONFIGURE_DIR}/configure.bak; fi
	${M4} \
	    -DconfOS="${OS}" \
	    -DconfOS2="${OS2}" -DconfPKGSRC="${PKGSRC}" -DconfDLFUNC_FNAME="${DLFUNC_FNAME}" \
	    -DconfEXIM_USER="${EXIM_USER}" \
	    -DconfEXIM_GROUP="${EXIM_GROUP}" \
	    -DconfACLS_DIR="${ACLS_DIR}" \
	    -DconfFEATURES_DIR="${FEATURES_DIR}" \
	    -DconfM4_DIR="${M4_DIR}" \
	    -DconfSITE_DIR="${SITE_DIR}" \
	    -DconfSYSTEMFILTERS_DIR="${SYSTEMFILTERS_DIR}" \
	    -DconfDELIVERIES_DIR="${DELIVERIES_DIR}" \
	    ${M4_DIR}/configure.m4 \
	| perl -n -e 's/\s+([\r\n])/$$1/; if(/^[\r\n]*$$/){$$empty=1;}else{if($$empty){print("\n");$$empty=0}print $$_}' \
	| perl -n -e 's/^ +\t/\t/; print $$_ unless (/^\s*\\\s*$$/)' \
	| perl -p -e 's/^ +\t/\t/' \
	    > $@.tmp
	cat $@.tmp > $@
	rm -rf $@.tmp
	chmod 640 $@
	echo " \
	include(confFEATURES_DIR\`/cfhead.m4')\
	include(confM4_DIR\`/conf.default')\
	include(confSITE_DIR/\`conf')\
	chown ifdef(\`confCONFIGURE_OWNER',confCONFIGURE_OWNER,${EXIM_USER}):ifdef(\`confCONFIGURE_GROUP',confCONFIGURE_GROUP,${EXIM_GROUP}) $@ ; \
	" | ${M4} \
	    -DconfOS="${OS}" \
	    -DconfOS2="${OS2}" -DconfPKGSRC="${PKGSRC}" -DconfDLFUNC_FNAME="${DLFUNC_FNAME}" \
	    -DconfEXIM_USER="${EXIM_USER}" \
	    -DconfEXIM_GROUP="${EXIM_GROUP}" \
	    -DconfM4_DIR="${M4_DIR}" \
	    -DconfFEATURES_DIR="${FEATURES_DIR}" \
	    -DconfSITE_DIR="${SITE_DIR}" \
	| perl -n -e 's/^ +\t/\t/; print $$_ unless (/^\s*\\\s*$$/)' \
	| perl -p -e 's/^ +\t/\t/' \
	| perl -n -e 's/\s+([\r\n])/$$1/; if(/^[\r\n]*$$/){$$empty=1;}else{if($$empty){print("\n");$$empty=0}print $$_}' \
	    > ${BIN_DIR}/chown.sh
	/bin/sh ${BIN_DIR}/chown.sh

##### upgrade #####

upgrade :
	cat ${M4_DIR}/upgrade.sh.m4 \
	| m4 \
	    -DconfCONFIGURE_DIR="${CONFIGURE_DIR}" \
	    -DconfACLS_DIR="${ACLS_DIR}" \
	    -DconfFEATURES_DIR="${FEATURES_DIR}" \
	    -DconfM4_DIR="${M4_DIR}" \
	    -DconfSITE_DIR="${SITE_DIR}" \
	    -DconfSYSTEMFILTERS_DIR="${SYSTEMFILTERS_DIR}" \
	    -DconfDELIVERIES_DIR="${DELIVERIES_DIR}" \
	| perl -n -e 's/^ +\t/\t/; print $$_ unless (/^\s*\\\s*$$/)' \
	| perl -p -e 's/^ +\t/\t/' \
	| perl -n -e 's/\s+([\r\n])/$$1/; if(/^[\r\n]*$$/){$$empty=1;}else{if($$empty){print("\n");$$empty=0}print $$_}' \
	    > ${BIN_DIR}/upgrade.sh
	/bin/sh ${BIN_DIR}/upgrade.sh

fix :
	cat ${M4_DIR}/fix.sh.m4 \
	| ${M4} \
	    -DconfOS="${OS}" \
	    -DconfOS2="${OS2}" -DconfPKGSRC="${PKGSRC}" -DconfDLFUNC_FNAME="${DLFUNC_FNAME}" \
	    -DconfEXIM_USER="${EXIM_USER}" \
	    -DconfEXIM_GROUP="${EXIM_GROUP}" \
	    -DconfCONFIGURE_DIR="${CONFIGURE_DIR}" \
	    -DconfACLS_DIR="${ACLS_DIR}" \
	    -DconfFEATURES_DIR="${FEATURES_DIR}" \
	    -DconfM4_DIR="${M4_DIR}" \
	    -DconfSITE_DIR="${SITE_DIR}" \
	    -DconfSYSTEMFILTERS_DIR="${SYSTEMFILTERS_DIR}" \
	    -DconfDELIVERIES_DIR="${DELIVERIES_DIR}" \
	    -DconfENTERPRISE_DIR="${ENTERPRISE_DIR}" \
	| perl -n -e 's/^ +\t/\t/; print $$_ unless (/^\s*\\\s*$$/)' \
	| perl -p -e 's/^ +\t/\t/' \
	| perl -n -e 's/\s+([\r\n])/$$1/; if(/^[\r\n]*$$/){$$empty=1;}else{if($$empty){print("\n");$$empty=0}print $$_}' \
	    > ${BIN_DIR}/fix.sh
	/bin/sh ${BIN_DIR}/fix.sh

check :
	cat ${M4_DIR}/check.sh.m4 \
	| ${M4} \
	    -DconfOS="${OS}" \
	    -DconfOS2="${OS2}" -DconfPKGSRC="${PKGSRC}" -DconfDLFUNC_FNAME="${DLFUNC_FNAME}" \
	    -DconfEXIM_USER="${EXIM_USER}" \
	    -DconfEXIM_GROUP="${EXIM_GROUP}" \
	    -DconfCONFIGURE_DIR="${CONFIGURE_DIR}" \
	    -DconfACLS_DIR="${ACLS_DIR}" \
	    -DconfFEATURES_DIR="${FEATURES_DIR}" \
	    -DconfM4_DIR="${M4_DIR}" \
	    -DconfSAMPLES_DIR="${SAMPLES_DIR}" \
	    -DconfSITE_DIR="${SITE_DIR}" \
	    -DconfSYSTEMFILTERS_DIR="${SYSTEMFILTERS_DIR}" \
	    -DconfDELIVERIES_DIR="${DELIVERIES_DIR}" \
	    -DconfENTERPRISE_DIR="${ENTERPRISE_DIR}" \
	| perl -n -e 's/^ +\t/\t/; print $$_ unless (/^\s*\\\s*$$/)' \
	| perl -p -e 's/^ +\t/\t/' \
	| perl -n -e 's/\s+([\r\n])/$$1/; if(/^[\r\n]*$$/){$$empty=1;}else{if($$empty){print("\n");$$empty=0}print $$_}' \
	    > ${BIN_DIR}/check.sh
	/bin/sh ${BIN_DIR}/check.sh

##### system filters #####

system_filter : ${CONFIGURE_DIR}/system_filter

SYSTEMFILTERS += $(shell ls ${SYSTEMFILTERS_DIR}/* 2>/dev/null | ${XARGS_L} echo)
SYSTEMFILTERS_SITE += $(shell ls ${SITE_DIR}/system_filter* 2>/dev/null | ${XARGS_L} echo)

${CONFIGURE_DIR}/system_filter : ${SYSTEMFILTERS_DIR}/system_filter.m4 ${SYSTEMFILTERS} ${SYSTEMFILTERS_SITE} ${SITE_DIR}/conf ${M4_DIR}/conf.default ${SITE_DIR}/system_filter_quarantine_warned ${SITE_DIR}/system_filter_quarantine_quarantined ${FEATURES_DIR}/spamassassin.m4
	${M4} \
	    -DconfOS="${OS}" \
	    -DconfOS2="${OS2}" -DconfPKGSRC="${PKGSRC}" -DconfDLFUNC_FNAME="${DLFUNC_FNAME}" \
	    -DconfEXIM_USER="${EXIM_USER}" \
	    -DconfEXIM_GROUP="${EXIM_GROUP}" \
	    -DconfACLS_DIR="${ACLS_DIR}" \
	    -DconfFEATURES_DIR="${FEATURES_DIR}" \
	    -DconfM4_DIR=${M4_DIR} \
	    -DconfSITE_DIR="${SITE_DIR}" \
	    -DconfSYSTEMFILTERS_DIR="${SYSTEMFILTERS_DIR}" \
	    ${SYSTEMFILTERS_DIR}/system_filter.m4 \
	    | perl -n -e 's/\s+([\r\n])/$$1/; if(/^[\r\n]*$$/){$$empty=1;}else{if($$empty){print("\n");$$empty=0}print $$_}' \
	    | perl -n -e 's/^ +\t/\t/; print $$_ unless (/^\s*\\\s*$$/)' \
	    | perl -p -e 's/^ +\t/\t/' \
	    > ${CONFIGURE_DIR}/system_filter

system_filter_quarantine_warned : ${SITE_DIR}/system_filter_quarantine_warned
${SITE_DIR}/system_filter_quarantine_warned : ${SYSTEMFILTERS_DIR}/quarantine_warned.list ${SYSTEMFILTERS_DIR}/quarantine_warned.m4
	#${ECHO} -n > ${SITE_DIR}/system_filter_quarantine_warned
	(echo "dnl" ; echo "dnl Do not edit this file" ; echo "dnl" ; echo "") > ${SITE_DIR}/system_filter_quarantine_warned
	perl -ne 'chomp; s/\|/\#\#\#/g; @f=split(/\s*\#\#\#\s*/); if (open(M4, "|${M4} " . "-DWARN_FIELD=$$f[$$[] " . "-DWARN_DIR=$$f[$$[+1] " . "-DWARN_MESSAGE=\"$$f[$$[+2]\" " . "${SYSTEMFILTERS_DIR}/quarantine_warned.m4")) { while (<M4>) {print $$_;} close(M4); }' ${SYSTEMFILTERS_DIR}/quarantine_warned.list \
		| perl -n -e 's/^ +\t/\t/; print $$_ unless (/^\s*\\\s*$$/)' \
		| perl -p -e 's/^ +\t/\t/' \
		| perl -n -e 's/\s+([\r\n])/$$1/; if(/^[\r\n]*$$/){$$empty=1;}else{if($$empty){print("\n");$$empty=0}print $$_}' \
		>> ${SITE_DIR}/system_filter_quarantine_warned

system_filter_quarantine_quarantined : ${SITE_DIR}/system_filter_quarantine_quarantined
${SITE_DIR}/system_filter_quarantine_quarantined : ${SYSTEMFILTERS_DIR}/quarantine_quarantined.list ${SYSTEMFILTERS_DIR}/quarantine_quarantined.m4
	#${ECHO} -n > ${SITE_DIR}/system_filter_quarantine_quarantined
	(echo "dnl" ; echo "dnl Do not edit this file" ; echo "dnl" ; echo "") > ${SITE_DIR}/system_filter_quarantine_quarantined
	perl -ne 'chomp; s/\|/\#\#\#/g; @f=split(/\s*\#\#\#\s*/); if (open(M4, "|${M4} " . "-DWARN_FIELD=$$f[$$[] " . "-DWARN_DIR=$$f[$$[+1] " . "-DWARN_MESSAGE=\"$$f[$$[+2]\" " . "-DconfACLS_DIR=\"${ACLS_DIR}\" " . "-DconfFEATURES_DIR=\"${FEATURES_DIR}\" " . "-DconfM4_DIR=\"${M4_DIR}\" " . "-DconfSITE_DIR=\"${SITE_DIR}\" " . "-DconfSYSTEMFILTERS_DIR=\"${SYSTEMFILTERS_DIR}\" " . "${SYSTEMFILTERS_DIR}/quarantine_quarantined.m4")) { while (<M4>) {print $$_;} close(M4); }' ${SYSTEMFILTERS_DIR}/quarantine_quarantined.list \
		| perl -n -e 's/^ +\t/\t/; print $$_ unless (/^\s*\\\s*$$/)' \
		| perl -p -e 's/^ +\t/\t/' \
		| perl -n -e 's/\s+([\r\n])/$$1/; if(/^[\r\n]*$$/){$$empty=1;}else{if($$empty){print("\n");$$empty=0}print $$_}' \
		>> ${SITE_DIR}/system_filter_quarantine_quarantined

##### exim.pl #####

exim.pl : ${CONFIGURE_DIR}/exim.pl

${CONFIGURE_DIR}/exim.pl : ${CONFIGURE_DIR}/configure ${SITE_PERL_FILES}
	${ECHO} -n > $@
	chmod 750 $@
	echo " \
	include(confFEATURES_DIR\`/cfhead.m4')\
	include(confM4_DIR\`/conf.default')\
	include(confSITE_DIR/\`conf')\
	chown ifdef(\`confEXIM_USER',confEXIM_USER,${EXIM_USER}) $@ ; \
	chgrp ifdef(\`confEXIM_GROUP',confEXIM_GROUP,${EXIM_GROUP}) $@ ; \
	" | ${M4} \
	    -DconfOS="${OS}" \
	    -DconfOS2="${OS2}" -DconfPKGSRC="${PKGSRC}" -DconfDLFUNC_FNAME="${DLFUNC_FNAME}" \
	    -DconfEXIM_USER="${EXIM_USER}" \
	    -DconfEXIM_GROUP="${EXIM_GROUP}" \
	    -DconfM4_DIR="${M4_DIR}" \
	    -DconfFEATURES_DIR="${FEATURES_DIR}" \
	    -DconfSITE_DIR="${SITE_DIR}" \
	    | perl -n -e 's/\s+([\r\n])/$$1/; if(/^[\r\n]*$$/){$$empty=1;}else{if($$empty){print("\n");$$empty=0}print $$_}' \
	    > ${BIN_DIR}/chown.sh
	/bin/sh ${BIN_DIR}/chown.sh
	if [ ! -z "${SITE_PERL_FILES}" ]; then cat ${SITE_PERL_FILES} > $@; fi
	for f in `perl -n -e 'print "$$1\n" if (/\\$$\{perl\{([^\}\r\n]+)\}/)' < ${CONFIGURE_DIR}/configure | sort | uniq` ; do \
		if [ -f ${PERL_DIR}/$${f}.pl ] ; then \
		    cat ${PERL_DIR}/$${f}.pl >> $@ ; \
		else \
		    echo "Warning: ${PERL_DIR}/$${f}.pl not found" ; \
		fi ; \
	done
#	[ -z "`cat $@`" ] && rm $@
#	rm ${BIN_DIR}/chown.sh

##### exim-dlfunc.so #####

dlfunc : ${DLFUNC_FNAME}

exim-dlfunc : ${DLFUNC_FNAME}

${DLFUNC_FNAME} : ${DLFUNC_DIR}/tmp/${DLFUNC_FNAME}

DLFUNC_SRC += $(shell ls ${DLFUNC_DIR}/*/*.c ${DLFUNC_DIR}/*/*.h 2>/dev/null)

${DLFUNC_DIR}/tmp/${DLFUNC_FNAME} : ${CONFIGURE_DIR}/configure ${DLFUNC_SRC}
	[ -d ${DLFUNC_DIR}/tmp ] || mkdir -p ${DLFUNC_DIR}/tmp
	echo " \
	include(confFEATURES_DIR\`/cfhead.m4')\
	include(confM4_DIR\`/conf.default')\
	include(confSITE_DIR/\`conf')\
	ifdef(\`confDLFUNC_PATH',echo "confDLFUNC_PATH" > confDLFUNC_DIR/tmp/BINDIR) \
	" | ${M4} \
	    -DconfOS="${OS}" \
	    -DconfOS2="${OS2}" -DconfPKGSRC="${PKGSRC}" -DconfDLFUNC_FNAME="${DLFUNC_FNAME}" \
	    -DconfEXIM_USER="${EXIM_USER}" \
	    -DconfEXIM_GROUP="${EXIM_GROUP}" \
	    -DconfM4_DIR="${M4_DIR}" \
	    -DconfFEATURES_DIR="${FEATURES_DIR}" \
	    -DconfSITE_DIR="${SITE_DIR}" \
	    -DconfDLFUNC_DIR="${DLFUNC_DIR}" \
	    | perl -n -e 's/\s+([\r\n])/$$1/; if(/^[\r\n]*$$/){$$empty=1;}else{if($$empty){print("\n");$$empty=0}print $$_}' \
	    > ${BIN_DIR}/chbindir.sh
	/bin/sh ${BIN_DIR}/chbindir.sh
	for f in INCLUDE STATCLIBS CLIBS OBJS ; do \
		cat ${DLFUNC_DIR}/$${f}.default > ${DLFUNC_DIR}/tmp/$${f} ; \
	done
	if [ -f ${DLFUNC_DIR}/tmp/Makefile ] || [ -h ${DLFUNC_DIR}/tmp/Makefile ] ; then \
	    rm ${DLFUNC_DIR}/tmp/Makefile ; \
	fi
	if [ -f ${DLFUNC_DIR}/Makefile.${OS} ] ; then \
		ln -s ../Makefile.${OS} ${DLFUNC_DIR}/tmp/Makefile ; \
	else \
		ln -s ../Makefile ${DLFUNC_DIR}/tmp/Makefile ; \
	fi
	#/bin/sh -c 'cd ${DLFUNC_DIR}/tmp && ${MAKE} cleanall'
	for d in `perl -n -e 'print "$$1\n" if (/\\$$\{dlfunc\{.+?\}\{([^\}\r\n]+)\}/)' < ${CONFIGURE_DIR}/configure | sort | uniq` ; do \
		echo "prepare to build dynamically loaded function $${d}" ; \
		for f in INCLUDE STATCLIBS CLIBS OBJS ; do \
			if [ -f ${DLFUNC_DIR}/$${d}/$${f} ] ; then \
				cat ${DLFUNC_DIR}/$${d}/$${f} >> ${DLFUNC_DIR}/tmp/$${f} ; \
			fi ; \
		done ; \
		[ -z "`ls -1 ${DLFUNC_DIR}/$${d}/*.c 2>/dev/null`" ] ||  rsync -a ${DLFUNC_DIR}/$${d}/*.c ${DLFUNC_DIR}/tmp/ ; \
		[ -z "`ls -1 ${DLFUNC_DIR}/$${d}/*.h 2>/dev/null`" ] ||  rsync -a ${DLFUNC_DIR}/$${d}/*.h ${DLFUNC_DIR}/tmp/ ; \
	done
	[ -z "`ls -1 ${DLFUNC_DIR}/tmp/*.c 2>/dev/null`" ] || /bin/sh -c 'cd ${DLFUNC_DIR}/tmp && ${MAKE} all install'
	touch $@
#	rm ${BIN_DIR}/chbindir.sh

##### STARTTLS key/certificate #####

exim.pem : ../exim.pem

../exim.pem :
	openssl req -newkey rsa:2048 -x509 -sha256 -nodes -days 365 -out $@ -keyout $@
	chmod 600 $@
	echo " \
	include(confFEATURES_DIR\`/cfhead.m4')\
	include(confM4_DIR\`/conf.default')\
	include(confSITE_DIR/\`conf')\
	chown ifdef(\`confEXIM_USER',confEXIM_USER,${EXIM_USER}) $@ ; \
	chgrp ifdef(\`confEXIM_GROUP',confEXIM_GROUP,${EXIM_GROUP}) $@ ; \
	" | ${M4} \
	    -DconfOS="${OS}" \
	    -DconfOS2="${OS2}" -DconfPKGSRC="${PKGSRC}" -DconfDLFUNC_FNAME="${DLFUNC_FNAME}" \
	    -DconfEXIM_USER="${EXIM_USER}" \
	    -DconfEXIM_GROUP="${EXIM_GROUP}" \
	    -DconfM4_DIR="${M4_DIR}" \
	    -DconfFEATURES_DIR="${FEATURES_DIR}" \
	    -DconfSITE_DIR="${SITE_DIR}" \
	    | perl -n -e 's/\s+([\r\n])/$$1/; if(/^[\r\n]*$$/){$$empty=1;}else{if($$empty){print("\n");$$empty=0}print $$_}' \
	    > ${BIN_DIR}/chown.sh
	/bin/sh ${BIN_DIR}/chown.sh
#	rm ${BIN_DIR}/chown.sh

##### DBM files #####

%.db : %
	exim_dbmbuild $< $@
../%.db : ../%
	exim_dbmbuild $< $@

##### text files #####

%-UTF8 : %
	iconv -f koi8-r -t utf-8 < $< > $@

texts-utf8 : ${M4_DIR}/CHANGES-UTF8 ${M4_DIR}/DEPENDENCIES-UTF8 ${M4_DIR}/README-UTF8 ${M4_DIR}/conf.default-UTF8

##### synchronization #####

SYNC_SRC_HOST=rsync.mta.org.ua
SYNC_SRC_DIR=exim-4.83-conf

SYNC_ENTERPRISE_SRC_HOST=rsync2.mta.org.ua
SYNC_ENTERPRISE_SRC_DIR=/usr/local/parovoz/exim-4.83-conf

sync : sync_m4 sync_acls sync_deliveries sync_features sync_perl sync_system_filters sync_samples sync_bin sync_contrib sync_patches sync_packages sync_dlfunc sync_enterprise

sync_enterprise :
	if [ -f ${SITE_DIR}/conf ] ; then \
	    echo "synchronize enterprise" ; \
	    /bin/sh -c ' \
		ENTERPRISE_USER=`(grep ENTERPRISE_USER ${SITE_DIR}/conf ; echo confENTERPRISE_USER ) | ${M4} | perl -p -e "s/^\s+//"` ; \
		if [ ! -z "$${ENTERPRISE_USER}" ] && [ -z "`echo $${ENTERPRISE_USER} | grep confENTERPRISE_USER`" ] ; then \
		    if [ ! -d ${CONFIGURE_DIR}/enterprise ] ; then \
			mkdir -p ${CONFIGURE_DIR}/enterprise ; \
		    fi ; \
		    if [ "$${ENTERPRISE_USER}" = "anonymous" ] ; then \
			rsync -avz --delete --exclude "*/README.html" --exclude "README.html" rsync://${SYNC_ENTERPRISE_SRC_HOST}/${SYNC_SRC_DIR}/enterprise/	${CONFIGURE_DIR}/enterprise/ ; \
		    else \
			rsync -avz --delete --exclude "*/README.html" --exclude "README.html" --rsh="ssh -l $${ENTERPRISE_USER}" \
		        ${SYNC_ENTERPRISE_SRC_HOST}:${SYNC_ENTERPRISE_SRC_DIR}/enterprise/	${CONFIGURE_DIR}/enterprise/ ; \
		    fi ; \
		fi' ; \
	fi
sync_data :
	if [ -f ${SITE_DIR}/conf ] ; then \
	    echo "synchronize enterprise" ; \
	    /bin/sh -c ' \
		ENTERPRISE_USER=`(grep ENTERPRISE_USER ${SITE_DIR}/conf ; echo confENTERPRISE_USER ) | ${M4} | perl -p -e "s/^\s+//"` ; \
		if [ ! -z "$${ENTERPRISE_USER}" ] && [ -z "`echo $${ENTERPRISE_USER} | grep confENTERPRISE_USER`" ] ; then \
		    for f in access-spf2 skip_dnsbl_check skip_greylist_relays ; do \
			rsync -av ${CONFIGURE_DIR}/enterprise/data/$$f ${CONFIGURE_DIR}/ ; \
		    done ; \
		    for f in skip_verify_sender_mail ; do \
			perl ${ENTERPRISE_DIR}/automation/join_common.pl \
			    ${ENTERPRISE_DIR}/data/$$f \
			    < ${CONFIGURE_DIR}/$$f \
			    > ${CONFIGURE_DIR}/$$f.tmp ; \
			if [ ! -z "`cat ${CONFIGURE_DIR}/$$f.tmp`" ] ; then \
			    cat ${CONFIGURE_DIR}/$$f.tmp > ${CONFIGURE_DIR}/$$f ; \
			    rm -rf ${CONFIGURE_DIR}/$$f.tmp ; \
			fi ; \
		    done ; \
		fi' ; \
	fi
sync_m4 :
	if [ -f ${CONFIGURE_DIR}/m4/CHANGES ]; then \
		mv ${CONFIGURE_DIR}/m4/CHANGES /tmp/exim-4.83-conf-CHANGES; \
	fi; \
	rsync -avz --delete --exclude "*/README.html" --exclude "README.html" rsync://${SYNC_SRC_HOST}/${SYNC_SRC_DIR}/m4/		${CONFIGURE_DIR}/m4/	--exclude '*m4/CHANGES.bak'
	if [ -f /tmp/exim-4.83-conf-CHANGES ]; then \
		mv /tmp/exim-4.83-conf-CHANGES ${CONFIGURE_DIR}/m4/CHANGES.bak ; \
	fi;
	touch ${CONFIGURE_DIR}/m4/VERSION
sync_acls :
	rsync -avz --delete --exclude "*/README.html" --exclude "README.html" rsync://${SYNC_SRC_HOST}/${SYNC_SRC_DIR}/acls/		${CONFIGURE_DIR}/acls/
	touch ${CONFIGURE_DIR}/m4/VERSION
sync_deliveries :
	rsync -avz --delete --exclude "*/README.html" --exclude "README.html" rsync://${SYNC_SRC_HOST}/${SYNC_SRC_DIR}/deliveries/	${CONFIGURE_DIR}/deliveries/
sync_features :
	rsync -avz --delete --exclude "*/README.html" --exclude "README.html" rsync://${SYNC_SRC_HOST}/${SYNC_SRC_DIR}/features/		${CONFIGURE_DIR}/features/
	touch ${CONFIGURE_DIR}/m4/VERSION
sync_perl :
	rsync -avz --delete --exclude "*/README.html" --exclude "README.html" rsync://${SYNC_SRC_HOST}/${SYNC_SRC_DIR}/perl/		${CONFIGURE_DIR}/perl/
	touch ${CONFIGURE_DIR}/m4/VERSION
sync_system_filters :
	rsync -avz --delete --exclude "*/README.html" --exclude "README.html" rsync://${SYNC_SRC_HOST}/${SYNC_SRC_DIR}/system_filters/	${CONFIGURE_DIR}/system_filters/
	touch ${CONFIGURE_DIR}/m4/VERSION
sync_samples :
	rsync -avz --delete --exclude "*/README.html" --exclude "README.html" rsync://${SYNC_SRC_HOST}/${SYNC_SRC_DIR}/samples/		${CONFIGURE_DIR}/samples/
	touch ${CONFIGURE_DIR}/m4/VERSION
sync_bin :
	rsync -avz --delete --exclude "*/README.html" --exclude "README.html" rsync://${SYNC_SRC_HOST}/${SYNC_SRC_DIR}/bin/		${CONFIGURE_DIR}/bin/
	touch ${CONFIGURE_DIR}/m4/VERSION
sync_contrib :
	rsync -avz --delete --exclude "*/README.html" --exclude "README.html" rsync://${SYNC_SRC_HOST}/${SYNC_SRC_DIR}/contrib/		${CONFIGURE_DIR}/contrib/
	touch ${CONFIGURE_DIR}/m4/VERSION
sync_patches :
	rsync -avz --delete --exclude "*/README.html" --exclude "README.html" rsync://${SYNC_SRC_HOST}/${SYNC_SRC_DIR}/patches/		${CONFIGURE_DIR}/patches/
	touch ${CONFIGURE_DIR}/m4/VERSION
sync_packages :
	if [ ! -d ${CONFIGURE_DIR}/packages ] ; then mkdir -p ${CONFIGURE_DIR}/packages ; fi
	if [ -h ${CONFIGURE_DIR}/ports ] ; then rm ${CONFIGURE_DIR}/ports ; fi
	for d in pkgsrc-dragonfly pkgsrc-netbsd pkgsrc-solaris ports-freebsd ports-openbsd ; do \
	    if [ -d ${CONFIGURE_DIR}/$$d ] ; then \
		if [ ! -d ${CONFIGURE_DIR}/packages/$$d ] ; then \
		    mv ${CONFIGURE_DIR}/$$d ${CONFIGURE_DIR}/packages/ ; \
		else \
		    rm -rf ${CONFIGURE_DIR}/$$d ; \
		fi ; \
	    fi ; \
	done ; \
	for d in redhat suse ; do \
	    if [ -d ${CONFIGURE_DIR}/$$d ] ; then \
		if [ ! -d ${CONFIGURE_DIR}/packages/rpms-$$d ] ; then \
		    mv ${CONFIGURE_DIR}/$$d ${CONFIGURE_DIR}/packages/rpms-$$d ; \
		else \
		    rm -rf ${CONFIGURE_DIR}/$$d ; \
		fi ; \
	    fi ; \
	done ; \
	for d in gentoo ; do \
	    if [ -d ${CONFIGURE_DIR}/$$d ] ; then \
		if [ ! -d ${CONFIGURE_DIR}/packages/portage-$$d ] ; then \
			mv ${CONFIGURE_DIR}/$$d ${CONFIGURE_DIR}/packages/portage-$$d ; \
		else \
		    rm -rf ${CONFIGURE_DIR}/$$d ; \
		fi ; \
	    fi ; \
	done ; \
	[ -d ${CONFIGURE_DIR}/packages/pkgsrc-solaris ] && mv ${CONFIGURE_DIR}/packages/pkgsrc-solaris ${CONFIGURE_DIR}/packages/pkgsrc-sunos ; \
	[ -d ${CONFIGURE_DIR}/packages/pkgsrc-macosx ] && mv ${CONFIGURE_DIR}/packages/pkgsrc-macosx ${CONFIGURE_DIR}/packages/pkgsrc-darwin ; \
	[ -d ${CONFIGURE_DIR}/packages/ports-macosx ] && mv ${CONFIGURE_DIR}/packages/ports-macosx ${CONFIGURE_DIR}/packages/macports-darwin ; \
	rsync -avz --delete --exclude "*/README.html" --exclude "README.html" rsync://${SYNC_SRC_HOST}/${SYNC_SRC_DIR}/packages/		${CONFIGURE_DIR}/packages/ \
		--exclude '*/Makefile.local' --exclude '*/make.conf' ; \
	if [ ! -f ${CONFIGURE_DIR}/packages/ports-freebsd/make.conf ]; then \
		touch ${CONFIGURE_DIR}/packages/ports-freebsd/make.conf ; \
	fi ; \
	if [ ! -f ${CONFIGURE_DIR}/packages/rpms-redhat/make.conf ]; then \
		touch ${CONFIGURE_DIR}/packages/rpms-redhat/make.conf ; \
	fi ; \
	if [ ! -f ${CONFIGURE_DIR}/packages/rpms-suse/make.conf ]; then \
		touch ${CONFIGURE_DIR}/packages/rpms-suse/make.conf ; \
	fi ; \
	touch ${CONFIGURE_DIR}/m4/VERSION
sync_dlfunc :
	[ -d ${CONFIGURE_DIR}/dlfunc/spamoborona2 ] && mv ${CONFIGURE_DIR}/dlfunc/spamoborona2 ${CONFIGURE_DIR}/dlfunc/spamooborona2 ; \
	rsync -avz --delete --exclude "*/README.html" --exclude "README.html" rsync://${SYNC_SRC_HOST}/${SYNC_SRC_DIR}/dlfunc/		${CONFIGURE_DIR}/dlfunc/ \
		--exclude 'tmp/'
	touch ${CONFIGURE_DIR}/m4/VERSION

##### install #####

install : pre-install post-install
preinstall : pre-install
postinstall : post-install

pre-install :
	# УПЪДБОЙЕ ЛБФБМПЗПЧ
	if [ ! -d ${SITE_DIR} ]; then			mkdir ${SITE_DIR}; fi
	if [ ! -d ${PERIODIC_DIR} ]; then		mkdir ${PERIODIC_DIR}; fi
	if [ ! -d ${PERIODIC_SITE_DIR} ]; then		mkdir ${PERIODIC_SITE_DIR}; fi
	# РЕТЕЙНЕОПЧБОЙЕ/РЕТЕОПУ/ХДБМЕОЙЕ ЧУРПНБЗБФЕМШОЩИ ЛПОЖЙЗПЧ
	if [ -f ${CONFIGURE_DIR}/hosts-reject ]; then		mv ${CONFIGURE_DIR}/hosts-reject ${CONFIGURE_DIR}/hosts-dropfrom; fi
	if [ -f ${CONFIGURE_DIR}/hosts-rejected ]; then		mv ${CONFIGURE_DIR}/hosts-rejected ${CONFIGURE_DIR}/hosts-dropfrom; fi
	if [ -f ${CONFIGURE_DIR}/recipients_abuse ]; then	mv ${CONFIGURE_DIR}/recipients_abuse ${CONFIGURE_DIR}/recipients_spam_friends; fi
	if [ -f ${CONFIGURE_DIR}/domains-sharedpop ]; then	rm -rf ${CONFIGURE_DIR}/domains-sharedpop; fi
	# ЛПОЧЕТФБГЙС ЧУРПНБЗБФЕМШОЩИ ЛПОЖЙЗПЧ
	if [ -f ${CONFIGURE_DIR}/skip_verify_sender_addr ]; then \
		( \
		cat ${SAMPLES_DIR}/skip_verify_sender_mail; \
		perl -e 'while (<>) { \
			if ((!/^\s*[#\n\r]/) and (/(.+)\@(.+)/)) { \
				$$addr{$$2}.=$$1."\n"; \
			} \
		} \
		foreach $$domain (sort(keys(%addr))) { \
			print($$domain."\t"); \
			foreach (split(/\n/, $$addr{$$domain})) { \
				print(" : ".$$_); \
			} \
			print("\n"); \
		}') < ${CONFIGURE_DIR}/skip_verify_sender_addr > ${CONFIGURE_DIR}/skip_verify_sender_mail; \
		rm ${CONFIGURE_DIR}/skip_verify_sender_addr; \
	fi
	# УПЪДБОЙЕ НЙОЙНБМШОПЗП ОБВПТБ ЧУРПНБЗБФЕМШОЩИ ЛПОЖЙЗПЧ
	if [ ! -f ${SITE_DIR}/Makefile ]; then					CUR_DIR=`pwd` ; cd ${SITE_DIR}; ln -s ${M4_DIR}/Makefile; cd $${CUR_DIR}; fi
	if [ ! -f ${SITE_DIR}/conf.default ]; then				CUR_DIR=`pwd` ; cd ${SITE_DIR}; ln -s ${M4_DIR}/conf.default; cd $${CUR_DIR}; fi
	if [ ! -f ${SITE_DIR}/conf ]; then					cp ${SAMPLES_DIR}/site/conf			${SITE_DIR}/conf; fi
	if [ ! -f ${SITE_DIR}/configure.retry_rules ]; then			cp ${SAMPLES_DIR}/site/configure.retry_rules	${SITE_DIR}/configure.retry_rules; fi
	if [ ! -f ${SITE_DIR}/configure.rewrite_rules ]; then			cp ${SAMPLES_DIR}/site/configure.rewrite_rules	${SITE_DIR}/configure.rewrite_rules; fi
	if [ -f ${SITE_DIR}/system_filter ] && [ ! -f ${SITE_DIR}/system_filter_bottom ] ; then mv ${SITE_DIR}/system_filter ${SITE_DIR}/system_filter_bottom; fi
	if [ ! -f ${SITE_DIR}/system_filter_top ]; then				cp ${SAMPLES_DIR}/site/system_filter_top	${SITE_DIR}/system_filter_top; fi
	if [ ! -f ${SITE_DIR}/system_filter_bottom ]; then			cp ${SAMPLES_DIR}/site/system_filter_bottom	${SITE_DIR}/system_filter_bottom; fi
	cat ${M4_DIR}/pre-install.sh.m4 \
	| ${M4} \
	    -DconfOS="${OS}" \
	    -DconfOS2="${OS2}" -DconfPKGSRC="${PKGSRC}" -DconfDLFUNC_FNAME="${DLFUNC_FNAME}" \
	    -DconfEXIM_USER="${EXIM_USER}" \
	    -DconfEXIM_GROUP="${EXIM_GROUP}" \
	    -DconfFEATURES_DIR="${FEATURES_DIR}" \
	    -DconfM4_DIR="${M4_DIR}" \
	    -DconfSITE_DIR="${SITE_DIR}" \
	    -DconfSYSTEMFILTERS_DIR="${SYSTEMFILTERS_DIR}" \
	    -DconfSAMPLES_DIR="${SAMPLES_DIR}" \
	| perl -n -e 's/^ +\t/\t/; print $$_ unless (/^\s*\\\s*$$/)' \
	| perl -p -e 's/^ +\t/\t/' \
	| perl -n -e 's/\s+([\r\n])/$$1/; if(/^[\r\n]*$$/){$$empty=1;}else{if($$empty){print("\n");$$empty=0}print $$_}' \
	    > ${BIN_DIR}/pre-install.sh
	/bin/sh ${BIN_DIR}/pre-install.sh
#	rm ${BIN_DIR}/pre-install.sh

post-install :
	for f in `perl -n -e 'print($$1."\n") if (/^[^#]*?CONFDIR\/([\w\d_\-\.]+)/)' < ${CONFIGURE_DIR}/configure | perl -p -e 's/\.db$$//' | sort | uniq | grep -v .pem\$$ | grep -v .pl\$$` ; do \
	    if [ ! -f ${CONFIGURE_DIR}/$${f} ]; then \
		if [ -f ${SAMPLES_DIR}/$${f} ]; then \
		    echo "> cp ${SAMPLES_DIR}/$${f} ${CONFIGURE_DIR}/$${f}"; \
		    cp ${SAMPLES_DIR}/$${f} ${CONFIGURE_DIR}/$${f}; \
		else \
		    echo "file ${SAMPLES_DIR}/$${f} not found"; \
		fi; \
	    fi; \
	done
	# УПЪДБОЙЕ ЛБФБМПЗПЧ, УНЕОБ ЧМБДЕМШГЕЧ, РТБЧ ДПУФХРБ, УПЪДБОЙЕ ЛЕЫЕК
	cat ${M4_DIR}/install.sh.m4 \
	| ${M4} \
	    -DconfOS="${OS}" \
	    -DconfOS2="${OS2}" -DconfPKGSRC="${PKGSRC}" -DconfDLFUNC_FNAME="${DLFUNC_FNAME}" \
	    -DconfEXIM_USER="${EXIM_USER}" \
	    -DconfEXIM_GROUP="${EXIM_GROUP}" \
	    -DconfACLS_DIR="${ACLS_DIR}" \
	    -DconfFEATURES_DIR="${FEATURES_DIR}" \
	    -DconfM4_DIR="${M4_DIR}" \
	    -DconfSITE_DIR="${SITE_DIR}" \
	    -DconfSYSTEMFILTERS_DIR="${SYSTEMFILTERS_DIR}" \
	    -DconfDELIVERIES_DIR="${DELIVERIES_DIR}" \
	    -DconfENTERPRISE_DIR="${ENTERPRISE_DIR}" \
	    -DconfCONTRIB_DIR="${CONTRIB_DIR}" \
	| perl -n -e 's/^ +\t/\t/; print $$_ unless (/^\s*\\\s*$$/)' \
	| perl -p -e 's/^ +\t/\t/' \
	| perl -n -e 's/\s+([\r\n])/$$1/; if(/^[\r\n]*$$/){$$empty=1;}else{if($$empty){print("\n");$$empty=0}print $$_}' \
	    > ${BIN_DIR}/install.sh
	/bin/sh ${BIN_DIR}/install.sh
#	rm ${BIN_DIR}/install.sh

##### periodic #####

# УПЪДБОЙЕ ЖБКМПЧ ДМС ЪБРХУЛБ ЙЪ РМБОЙТПЧЭЙЛБ ЪБДБЮ

periodic : ${PERIODIC_DIR}/clear_quarantine.sh ${PERIODIC_DIR}/clear_scan.sh ${PERIODIC_DIR}/delete_old_warnings.sh ${PERIODIC_DIR}/greylist_del_expired.sh ${PERIODIC_DIR}/greylist_relays_del_expired.sh ${PERIODIC_DIR}/awl_del_expired.sh

${PERIODIC_DIR}/clear_quarantine.sh : ${PERIODIC_M4_DIR}/clear_quarantine.sh.m4 ${SITE_DIR}/conf ${M4_DIR}/conf.default
	cat ${PERIODIC_M4_DIR}/clear_quarantine.sh.m4 \
	| ${M4} \
	    -DconfOS="${OS}" \
	    -DconfOS2="${OS2}" -DconfPKGSRC="${PKGSRC}" -DconfDLFUNC_FNAME="${DLFUNC_FNAME}" \
	    -DconfEXIM_USER="${EXIM_USER}" \
	    -DconfEXIM_GROUP="${EXIM_GROUP}" \
	    -DconfACLS_DIR="${ACLS_DIR}" \
	    -DconfM4_DIR="${M4_DIR}" \
	    -DconfSITE_DIR="${SITE_DIR}" \
	    -DconfFEATURES_DIR="${FEATURES_DIR}" \
	| perl -n -e 's/^ +\t/\t/; print $$_ unless (/^\s*\\\s*$$/)' \
	| perl -p -e 's/^ +\t/\t/' \
	| perl -n -e 's/\s+([\r\n])/$$1/; if(/^[\r\n]*$$/){$$empty=1;}else{if($$empty){print("\n");$$empty=0}print $$_}' \
	    > $@
	chmod 700 $@
	chown root $@

${PERIODIC_DIR}/clear_scan.sh : ${PERIODIC_M4_DIR}/clear_scan.sh.m4 ${SITE_DIR}/conf ${M4_DIR}/conf.default
	cat ${PERIODIC_M4_DIR}/clear_scan.sh.m4 \
	| ${M4} \
	    -DconfOS="${OS}" \
	    -DconfOS2="${OS2}" -DconfPKGSRC="${PKGSRC}" -DconfDLFUNC_FNAME="${DLFUNC_FNAME}" \
	    -DconfEXIM_USER="${EXIM_USER}" \
	    -DconfEXIM_GROUP="${EXIM_GROUP}" \
	    -DconfACLS_DIR="${ACLS_DIR}" \
	    -DconfM4_DIR="${M4_DIR}" \
	    -DconfSITE_DIR="${SITE_DIR}" \
	    -DconfFEATURES_DIR="${FEATURES_DIR}" \
	| perl -n -e 's/^ +\t/\t/; print $$_ unless (/^\s*\\\s*$$/)' \
	| perl -p -e 's/^ +\t/\t/' \
	| perl -n -e 's/\s+([\r\n])/$$1/; if(/^[\r\n]*$$/){$$empty=1;}else{if($$empty){print("\n");$$empty=0}print $$_}' \
	    > $@
	chmod 700 $@
	chown root $@

${PERIODIC_DIR}/delete_old_warnings.sh : ${PERIODIC_M4_DIR}/delete_old_warnings.sh.m4 ${SITE_DIR}/conf ${M4_DIR}/conf.default
	cat ${PERIODIC_M4_DIR}/delete_old_warnings.sh.m4 \
	| ${M4} \
	    -DconfOS="${OS}" \
	    -DconfOS2="${OS2}" -DconfPKGSRC="${PKGSRC}" -DconfDLFUNC_FNAME="${DLFUNC_FNAME}" \
	    -DconfEXIM_USER="${EXIM_USER}" \
	    -DconfEXIM_GROUP="${EXIM_GROUP}" \
	    -DconfACLS_DIR="${ACLS_DIR}" \
	    -DconfM4_DIR="${M4_DIR}" \
	    -DconfSITE_DIR="${SITE_DIR}" \
	    -DconfFEATURES_DIR="${FEATURES_DIR}" \
	| perl -n -e 's/^ +\t/\t/; print $$_ unless (/^\s*\\\s*$$/)' \
	| perl -p -e 's/^ +\t/\t/' \
	| perl -n -e 's/\s+([\r\n])/$$1/; if(/^[\r\n]*$$/){$$empty=1;}else{if($$empty){print("\n");$$empty=0}print $$_}' \
	    > $@
	chmod 700 $@
	chown root $@

${PERIODIC_DIR}/greylist_del_expired.sh : ${PERIODIC_M4_DIR}/greylist_del_expired.sh.m4 ${SITE_DIR}/conf ${M4_DIR}/conf.default
	cat ${PERIODIC_M4_DIR}/greylist_del_expired.sh.m4 \
	| ${M4} \
	    -DconfOS="${OS}" \
	    -DconfOS2="${OS2}" -DconfPKGSRC="${PKGSRC}" -DconfDLFUNC_FNAME="${DLFUNC_FNAME}" \
	    -DconfEXIM_USER="${EXIM_USER}" \
	    -DconfEXIM_GROUP="${EXIM_GROUP}" \
	    -DconfACLS_DIR="${ACLS_DIR}" \
	    -DconfM4_DIR="${M4_DIR}" \
	    -DconfSITE_DIR="${SITE_DIR}" \
	    -DconfFEATURES_DIR="${FEATURES_DIR}" \
	    -DconfBIN_DIR=`pwd`/"${BIN_DIR}" \
	    -DconfCONTRIB_DIR=`pwd`/"${CONTRIB_DIR}" \
	| perl -n -e 's/^ +\t/\t/; print $$_ unless (/^\s*\\\s*$$/)' \
	| perl -p -e 's/^ +\t/\t/' \
	| perl -n -e 's/\s+([\r\n])/$$1/; if(/^[\r\n]*$$/){$$empty=1;}else{if($$empty){print("\n");$$empty=0}print $$_}' \
	    > $@
	chmod 700 $@
	chown root $@

${PERIODIC_DIR}/greylist_relays_del_expired.sh : ${PERIODIC_M4_DIR}/greylist_relays_del_expired.sh.m4 ${SITE_DIR}/conf ${M4_DIR}/conf.default
	cat ${PERIODIC_M4_DIR}/greylist_relays_del_expired.sh.m4 \
	| ${M4} \
	    -DconfOS="${OS}" \
	    -DconfOS2="${OS2}" -DconfPKGSRC="${PKGSRC}" -DconfDLFUNC_FNAME="${DLFUNC_FNAME}" \
	    -DconfEXIM_USER="${EXIM_USER}" \
	    -DconfEXIM_GROUP="${EXIM_GROUP}" \
	    -DconfACLS_DIR="${ACLS_DIR}" \
	    -DconfM4_DIR="${M4_DIR}" \
	    -DconfSITE_DIR="${SITE_DIR}" \
	    -DconfFEATURES_DIR="${FEATURES_DIR}" \
	    -DconfBIN_DIR=`pwd`/"${BIN_DIR}" \
	    -DconfCONTRIB_DIR=`pwd`/"${CONTRIB_DIR}" \
	| perl -n -e 's/^ +\t/\t/; print $$_ unless (/^\s*\\\s*$$/)' \
	| perl -p -e 's/^ +\t/\t/' \
	| perl -n -e 's/\s+([\r\n])/$$1/; if(/^[\r\n]*$$/){$$empty=1;}else{if($$empty){print("\n");$$empty=0}print $$_}' \
	    > $@
	chmod 700 $@
	chown root $@

${PERIODIC_DIR}/awl_del_expired.sh : ${PERIODIC_M4_DIR}/awl_del_expired.sh.m4 ${SITE_DIR}/conf ${M4_DIR}/conf.default
	cat ${PERIODIC_M4_DIR}/awl_del_expired.sh.m4 \
	| ${M4} \
	    -DconfOS="${OS}" \
	    -DconfOS2="${OS2}" -DconfPKGSRC="${PKGSRC}" -DconfDLFUNC_FNAME="${DLFUNC_FNAME}" \
	    -DconfEXIM_USER="${EXIM_USER}" \
	    -DconfEXIM_GROUP="${EXIM_GROUP}" \
	    -DconfACLS_DIR="${ACLS_DIR}" \
	    -DconfM4_DIR="${M4_DIR}" \
	    -DconfSITE_DIR="${SITE_DIR}" \
	    -DconfFEATURES_DIR="${FEATURES_DIR}" \
	    -DconfBIN_DIR=`pwd`/"${BIN_DIR}" \
	    -DconfCONTRIB_DIR=`pwd`/"${CONTRIB_DIR}" \
	| perl -n -e 's/^ +\t/\t/; print $$_ unless (/^\s*\\\s*$$/)' \
	| perl -p -e 's/^ +\t/\t/' \
	| perl -n -e 's/\s+([\r\n])/$$1/; if(/^[\r\n]*$$/){$$empty=1;}else{if($$empty){print("\n");$$empty=0}print $$_}' \
	    > $@
	chmod 700 $@
	chown root $@

periodic-install : install-periodic

install-periodic :
	cat ${M4_DIR}/install-periodic.sh.m4 \
	| ${M4} \
	    -DconfOS="${OS}" \
	    -DconfOS2="${OS2}" -DconfPKGSRC="${PKGSRC}" -DconfDLFUNC_FNAME="${DLFUNC_FNAME}" \
	    -DconfEXIM_USER="${EXIM_USER}" \
	    -DconfEXIM_GROUP="${EXIM_GROUP}" \
	    -DconfACLS_DIR="${ACLS_DIR}" \
	    -DconfFEATURES_DIR="${FEATURES_DIR}" \
	    -DconfM4_DIR="${M4_DIR}" \
	    -DconfSITE_DIR="${SITE_DIR}" \
	    -DconfSYSTEMFILTERS_DIR="${SYSTEMFILTERS_DIR}" \
	    -DconfDELIVERIES_DIR="${DELIVERIES_DIR}" \
	    -DconfPERIODIC_DIR=`pwd`/"${PERIODIC_DIR}" \
	    -DconfPERIODIC_SITE_DIR=`pwd`/"${PERIODIC_SITE_DIR}" \
	| perl -n -e 's/^ +\t/\t/; print $$_ unless (/^\s*\\\s*$$/)' \
	| perl -p -e 's/^ +\t/\t/' \
	| perl -n -e 's/\s+([\r\n])/$$1/; if(/^[\r\n]*$$/){$$empty=1;}else{if($$empty){print("\n");$$empty=0}print $$_}' \
	    > ${BIN_DIR}/install-periodic.sh
	/bin/sh ${BIN_DIR}/install-periodic.sh
#	rm ${BIN_DIR}/install-periodic.sh