#
# Copyright 1998 - 2008 Double Precision, Inc.  See COPYING for
# distribution information.

DEBUGLIB=@DEBUGLIB@

CONFIG_STATUS_DEPENDENCIES = ../../tcpd/couriertls.config

INCLUDES=`@COURIERAUTHCONFIG@ --cppflags`

CLEANFILES=libexecdir.h localstatedir.h config module.esmtp permissions.dat
DISTCLEANFILES=esmtp.authpam imapd.cnf esmtpd.cnf

BUILT_SOURCES=localstatedir.h libexecdir.h

moduledir=$(libexecdir)/courier/modules/esmtp

databindir=$(datadir)
databin_SCRIPTS=esmtpd esmtpd-ssl makesmtpaccess makeacceptmailfor \
	makepercentrelay mkesmtpdcert

sysconf_DATA=esmtpd.cnf esmtpd.dist esmtpd-msa.dist esmtpd-ssl.dist \
	esmtp.authpam module.esmtp

module.esmtp: courier.config
	cp -f $(srcdir)/courier.config module.esmtp

esmtpd.cnf: $(top_builddir)/config.status

esmtpd.cnf: esmtpd.cnf.@ssllib@
	cp $< $@
	touch $@

module_PROGRAMS=courieresmtp courieresmtpd addcr
INSTALL=@INSTALL@ `test ! -w /etc || echo -o @mailuser@ -g @mailgroup@`
INSTALL_PROGRAM=${INSTALL} -m 550

EXTRA_DIST=courier.config staticlist.c esmtp.authpam.dist esmtpd.cnf.gnutls

noinst_LTLIBRARIES=librewrite.la

install-data-hook:
	test -w /etc || exit 0 ; cd $(DESTDIR)$(moduledir) || exit 1 ; chown @mailuser@ . ; chgrp @mailgroup@ .

ldaddlibdl=@LIBDL@

courieresmtp_SOURCES=courieresmtp.c esmtpclient.c esmtpconfig.c esmtpconfig.h \
		smtproutes.h smtproutes.c staticlist.c

courieresmtpd_SOURCES=courieresmtpd.c esmtpconfig.c esmtpconfig.h esmtpiov.c \
		esmtpiov.h staticlist.c

courieresmtp_DEPENDENCIES=../../courier/libs/libmodule.la \
			librewrite.la \
			../../imap/libimapd.la \
			../../courier/libs/libcommon.la \
			../../courier/libs/libcourier.la \
			@dblibrary@ \
			../../rfc1035/librfc1035.a \
			../../rfc2045/librfc2045.la \
			../../rfc822/librfc822.la \
			@SASLLIBS@ \
			../../tcpd/libtlsclient.la \
			../../numlib/libnumlib.la \
			../../waitlib/libwaitlib.a \
			../../tcpd/libspipe.la \
			../../md5/libmd5.la \
			../../random128/librandom128.la \
			../../unicode/libunicode.la \
			../../soxwrap/libsoxwrap.a \
			../../soxwrap/soxlibs.dep

commonLDADD=../../courier/libs/libmodule.la librewrite.la \
			../../imap/libimapd.la \
			../../courier/libs/libcommon.la \
			../../courier/libs/libcourier.la \
			../../rfc1035/librfc1035.a \
			../../rfc822/librfc822.la \
			../../rfc2045/librfc2045.la \
			@SASLLIBS@ \
			../../tcpd/libtlsclient.la \
			../../numlib/libnumlib.la \
			../../waitlib/libwaitlib.a \
			../../tcpd/libspipe.la \
			@dblibrary@ \
			../../md5/libmd5.la \
			../../random128/librandom128.la \
			../../soxwrap/libsoxwrap.a \
			../../unicode/libunicode.la \
			`cat ../../soxwrap/soxlibs.dep` \
			@NETLIBS@ $(ldaddlibdl) $(DEBUGLIB)

courieresmtp_LDADD=-lcourierauthsaslclient $(commonLDADD)

courieresmtp_LDFLAGS=`@COURIERAUTHCONFIG@ --ldflags`

courieresmtpd_DEPENDENCIES=$(courieresmtp_DEPENDENCIES) \
				../../tcpd/libspipe.la
courieresmtpd_LDADD=$(commonLDADD) ../../tcpd/libspipe.la
courieresmtpd_LDFLAGS=`@COURIERAUTHCONFIG@ --ldflags` \
		-lcourierauthsasl -lcourierauth

addcr_SOURCES=addcr.c

install-data-local:
	test -f $(DESTDIR)$(sbindir)/courieresmtpd && exit 0; $(mkinstalldirs) $(DESTDIR)$(sbindir) || :; rm -f $(DESTDIR)$(sbindir)/courieresmtpd && $(LN_S) $(moduledir)/courieresmtpd $(DESTDIR)$(sbindir)/courieresmtpd
	-$(mkinstalldirs) $(DESTDIR)$(bindir)
	test -f $(DESTDIR)$(bindir)/addcr && exit 0; rm -f $(DESTDIR)$(bindir)/addcr && $(LN_S) $(moduledir)/addcr $(DESTDIR)$(bindir)/addcr
	-$(mkinstalldirs) $(DESTDIR)$(sysconfdir)/esmtpacceptmailfor.dir
	-$(mkinstalldirs) $(DESTDIR)$(sysconfdir)/esmtppercentrelay.dir
	-$(mkinstalldirs) $(DESTDIR)$(sbindir)
	for f in $(databin_SCRIPTS) ; do \
		rm -f $(DESTDIR)$(sbindir)/$$f ; \
		$(LN_S) $(datadir)/$$f $(DESTDIR)$(sbindir)/$$f ; done
	cat </dev/null >>$(DESTDIR)$(sysconfdir)/esmtpauthclient
	chmod 600 $(DESTDIR)$(sysconfdir)/esmtpauthclient
	test -w /etc || exit 0; \
		chown @mailuser@ $(DESTDIR)$(sysconfdir)/esmtpauthclient ; \
		chgrp @mailgroup@ $(DESTDIR)$(sysconfdir)/esmtpauthclient
	rm -f $(DESTDIR)$(sbindir)/esmtpd-msa $(DESTDIR)$(sbindir)/makesmtpaccess-msa
	$(LN_S) esmtpd $(DESTDIR)$(sbindir)/esmtpd-msa
	$(LN_S) makesmtpaccess $(DESTDIR)$(sbindir)/makesmtpaccess-msa

uninstall-local:
	rm -f $(DESTDIR)$(sbindir)/courieresmtpd
	rm -f $(DESTDIR)$(bindir)/addcr
	rm -f $(DESTDIR)$(sbindir)/esmtpd-msa
	rm -f $(DESTDIR)$(sbindir)/makesmtpaccess-msa
	for f in $(databin_SCRIPTS) ; do \
		rm -f $(DESTDIR)$(sbindir)/$$f ; done
	test "`cat $(DESTDIR)$(sysconfdir)/esmtpauthclient`" = "" || exit 0; \
		rm -f $(DESTDIR)$(sysconfdir)/esmtpauthclient

install-perms:
	@$(MAKE) install-perms-recursive
	@$(MAKE) install-perms-local

install-perms-recursive:
	@>permissions.dat
	@SUBDIRS="$(SUBDIRS)" ; for f in $$SUBDIRS ; do ( cd $$f && $(MAKE) $(AM_MAKEFLAGS) install-perms ) ; cat /dev/null >>$$f/permissions.dat ; cat $$f/permissions.dat >>permissions.dat ; done

install-perms-local:
	@echo $(moduledir) 755 @mailuser@ @mailgroup@ >>permissions.dat
	@echo $(sysconfdir)/module.esmtp 644 @mailuser@ @mailgroup@ config >>permissions.dat
	@echo $(moduledir)/courieresmtp 550 @mailuser@ @mailgroup@ >>permissions.dat
	@echo $(moduledir)/courieresmtpd 550 @mailuser@ @mailgroup@ >>permissions.dat
	@echo $(moduledir)/addcr 555 @mailuser@ @mailgroup@ >>permissions.dat
	@echo $(sbindir)/courieresmtpd 555 @mailuser@ @mailgroup@ >>permissions.dat
	@echo $(bindir)/addcr 555 @mailuser@ @mailgroup@ >>permissions.dat
	@for f in $(databin_SCRIPTS) ; do \
	   echo $(sbindir)/$$f 555 @mailuser@ @mailgroup@ >>permissions.dat ; \
	   echo $(datadir)/$$f 555 @mailuser@ @mailgroup@ >>permissions.dat ; \
		done
	@echo $(sbindir)/esmtpd-msa 555 @mailuser@ @mailgroup@ >>permissions.dat
	@echo $(sbindir)/makesmtpaccess-msa 555 @mailuser@ @mailgroup@ >>permissions.dat
	@echo $(sysconfdir)/esmtpd.dist 644 @mailuser@ @mailgroup@ config >>permissions.dat
	@echo $(sysconfdir)/esmtpd-msa.dist 644 @mailuser@ @mailgroup@ config >>permissions.dat
	@echo $(sysconfdir)/esmtpd-ssl.dist 644 @mailuser@ @mailgroup@ config >>permissions.dat
	@echo $(sysconfdir)/esmtpacceptmailfor.dir 755 @mailuser@ @mailgroup@ >>permissions.dat
	@echo $(sysconfdir)/esmtppercentrelay.dir 755 @mailuser@ @mailgroup@ >>permissions.dat
	@echo $(sysconfdir)/esmtp.authpam 644 @mailuser@ @mailgroup@ config >>permissions.dat
	@echo $(sysconfdir)/esmtpd.cnf 644 @mailuser@ @mailgroup@ config >>permissions.dat
	@echo $(sysconfdir)/esmtpauthclient 600 @mailuser@ @mailgroup@ config >>permissions.dat

librewrite_la_SOURCES=esmtp.c
librewrite_la_LDFLAGS=-static

localstatedir.h: config.status
	echo "#define LOCALSTATEDIR \"$(localstatedir)\"" >localstatedir.h

libexecdir.h: config.status
	echo "#define LIBEXECDIR \"$(libexecdir)\"" >libexecdir.h
