| 1 | ### -*-makefile-*- |
| 2 | ### |
| 3 | ### Build script for Exim configuration |
| 4 | ### |
| 5 | ### (c) 2012 Mark Wooding |
| 6 | ### |
| 7 | |
| 8 | ###----- Licensing notice --------------------------------------------------- |
| 9 | ### |
| 10 | ### This program is free software; you can redistribute it and/or modify |
| 11 | ### it under the terms of the GNU General Public License as published by |
| 12 | ### the Free Software Foundation; either version 2 of the License, or |
| 13 | ### (at your option) any later version. |
| 14 | ### |
| 15 | ### This program is distributed in the hope that it will be useful, |
| 16 | ### but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | ### GNU General Public License for more details. |
| 19 | ### |
| 20 | ### You should have received a copy of the GNU General Public License |
| 21 | ### along with this program; if not, write to the Free Software Foundation, |
| 22 | ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 23 | |
| 24 | ###-------------------------------------------------------------------------- |
| 25 | ### Utility macros. |
| 26 | |
| 27 | ## Silent-rules machinery. |
| 28 | V = 0 |
| 29 | v_tag = $(call v_tag_$V,$1) |
| 30 | v_tag_0 = @printf " %-6s %s\n" $1 $@; |
| 31 | |
| 32 | V_GEN = $(call v_tag,GEN) |
| 33 | V_AT = $(V_AT_$V) |
| 34 | V_AT_0 = @ |
| 35 | |
| 36 | ## Splitting things at slashes -- don't leave the slash. |
| 37 | dir-nosl = $(patsubst %/,%,$(dir $1)) |
| 38 | |
| 39 | ###-------------------------------------------------------------------------- |
| 40 | ### Introductory machinery. |
| 41 | |
| 42 | all: |
| 43 | .SECONDEXPANSION: # sorry |
| 44 | |
| 45 | CLEANFILES += $(TARGETS) |
| 46 | |
| 47 | ###-------------------------------------------------------------------------- |
| 48 | ### Main source files. |
| 49 | |
| 50 | ## There's a base set of files, and a number of optional modules. A |
| 51 | ## collection of modules is called a `mode', and we generate an output file |
| 52 | ## for each required mode. |
| 53 | |
| 54 | EARLY = defs.m4 divmap.m4 config.m4 |
| 55 | MAIN = lists.m4 base.m4 |
| 56 | |
| 57 | MODES = |
| 58 | |
| 59 | MODES += satellite |
| 60 | OPTIONS_satellite = satellite.m4 |
| 61 | |
| 62 | MODES += hub |
| 63 | OPTIONS_hub = auth.m4 exchange.m4 local.m4 spam.m4 vhost.m4 |
| 64 | |
| 65 | MODES += usersat |
| 66 | OPTIONS_usersat = auth.m4 local.m4 satellite.m4 |
| 67 | |
| 68 | -include local.mk |
| 69 | |
| 70 | ###-------------------------------------------------------------------------- |
| 71 | ### Resolving what needs to be built. |
| 72 | |
| 73 | HOST_MODES += $(foreach m, $(MODES), \ |
| 74 | $(foreach h, $(HOSTS_$m), $h/$m)) |
| 75 | |
| 76 | CONFIGS = $(foreach m, $(MODES), exim4-$m.conf) |
| 77 | TARGETS += $(CONFIGS) |
| 78 | $(CONFIGS): exim4-%.conf: $(EARLY) $$(HOOKS_$$*) $(MAIN) $$(OPTIONS_$$*) |
| 79 | $(V_GEN)m4 -P -DMODE=$* $^ >$@.new |
| 80 | $(V_AT)$(CHECK_$*)exim4 -C$@.new -bV >/dev/null |
| 81 | $(V_AT)mv $@.new $@ |
| 82 | |
| 83 | all: $(TARGETS) |
| 84 | |
| 85 | host-mode = $(notdir $(filter $1/%, $(HOST_MODES))) |
| 86 | |
| 87 | HOSTS = $(sort $(call dir-nosl, $(HOST_MODES))) |
| 88 | |
| 89 | ###-------------------------------------------------------------------------- |
| 90 | ### Propagating configuration to remote hosts. |
| 91 | |
| 92 | THISHOST = $(shell hostname) |
| 93 | OTHERHOSTS = $(filter-out $(THISHOST), $(HOSTS)) |
| 94 | |
| 95 | ROOT = sudo |
| 96 | |
| 97 | install_rune = \ |
| 98 | exim4 -C/etc/exim4/exim4.conf.new -bV >/dev/null && \ |
| 99 | mv /etc/exim4/exim4.conf.new /etc/exim4/exim4.conf && \ |
| 100 | service exim4 reload |
| 101 | |
| 102 | install/$(THISHOST): exim4-$(call host-mode,$(THISHOST)).conf |
| 103 | $(ROOT) cp $< /etc/exim4/exim4.conf.new |
| 104 | $(ROOT) sh -c '$(install_rune)' |
| 105 | |
| 106 | $(addprefix install/, $(OTHERHOSTS)): \ |
| 107 | install/%: exim4-$$(call host-mode,$$*).conf |
| 108 | $(ROOT) scp $< root@$*:/etc/exim4/exim4.conf.new |
| 109 | $(ROOT) ssh root@$* '$(install_rune)' |
| 110 | |
| 111 | install: $(addprefix install/, $(HOSTS)) |
| 112 | |
| 113 | clean:; rm -f $(CLEANFILES) |
| 114 | .PHONY: clean |
| 115 | |
| 116 | ###----- That's all, folks -------------------------------------------------- |