Generalize and straigthen handling of the soft-exit-status *!*
[s-mailx.git] / mk-mk.in
blobc2a2c19f5a55eb5bacad08fa4a54909772113ef6
1 #@ mk-mk.in -- mk.mk template, completed by mk-conf.sh.
2 #@ Please see INSTALL and make.rc instead.
4 SIZE_CHECK = @if ( command -v size ) > /dev/null 2>&1; then size $(@); fi
6 _version_from_header = VERSION="`< version.h $(sed) \
7                 -e '/ VERSION /b X' -e d -e ':X' \
8                 -e 's/[^\"]*\"v\([^\"]\{1,\}\)\"/\1/'`"
10 OBJ = $(OBJ_SRC:.c=.o)
11 .SUFFIXES: .o .c .y
12 .c.o:
13         $(ECHO_CC)$(CC) $(CFLAGS) $(INCS) -c $(<)
14 .c: ;
16 all: $(VAL_UAGENT) $(OPTIONAL_PRIVSEP)
18 $(VAL_UAGENT): $(OBJ) uman.1 urc.rc
19         $(ECHO_LINK)$(CC) $(LDFLAGS) -o $(@) $(OBJ) $(LIBS)
20         $(SIZE_CHECK)
22 $(OBJ): config.h nail.h nailfuns.h $(AMALGAM_DEP)
23 $(AMALGAM_TARGET) accmacvar.o: okeys.h version.h
24 $(AMALGAM_TARGET) dotlock.o: dotlock.h
25 $(AMALGAM_TARGET) lex_input.o: cmd_tab.h
26 $(AMALGAM_TARGET) mime_types.o: mime_types.h
27 $(AMALGAM_TARGET) pop3.o smtp.o urlcrecry.o: rfc1321.h
28 $(AMALGAM_TARGET) smtp.o: smtp_gssapi.h
29 $(AMALGAM_TARGET) termcap.o: tcaps.h
31 $(VAL_PRIVSEP): privsep.o
32         $(ECHO_LINK)$(CC) $(LDFLAGS) -o $(@) privsep.o $(BASE_LIBS)
33         $(SIZE_CHECK)
35 privsep.o: privsep.c config.h nail.h dotlock.h
36         $(ECHO_CC)$(CC) $(CFLAGS) $(BASE_INCS) -c privsep.c
38 test: all
39         $(ECHO_TEST)\
40         MAKE=$(MAKE) \
41         awk=$(awk) cat=$(cat) cksum=$(cksum) rm=$(rm) sed=$(sed) grep=$(grep) \
42         $(SHELL) ./cc-test.sh --check-only ./$(VAL_UAGENT)
44 mime_types.h: mime.types
45         $(ECHO_GEN)\
46         LC_ALL=C < ./mime.types > $(@) $(awk) '\
47         function add(mt, ln) {\
48                 gsub(/[[:space:]]+/, " ", ln);\
49                 i = split(ln, i_a);\
50                 e = "";\
51                 for (j = 1; j <= i; ++j) {\
52                         k = i_a[j];\
53                         e = (e ? e " " : "") k\
54                 }\
55                 if (e) {\
56                         if (e_a[mt])\
57                                 e_a[mt] = e_a[mt] " ";\
58                         e_a[mt] = e_a[mt] e\
59                 }\
60         }\
61         /^[[:space:]]*#/ {next}\
62         /^[[:space:]]*$$/ {ltype = ""; next}\
63         /^[[:space:]]/ {\
64                 if (!ltype)\
65                         print "FAULTY CONTINUATION: " $$0 >> "/dev/stderr";\
66                 add(ltype, $$0);\
67                 next\
68         }\
69         /^(@([thH]@)? )?[[:alpha:]]/ {\
70                 if ($$1 ~ /^@([thH]@)?$$/) {\
71                         pa = $$1;\
72                         $$1 = $$2;\
73                         $$2 = ""\
74                 } else\
75                         pa = "";\
76                 if ($$1 !~ /^([[:alnum:]]+)\/([[:alnum:]_+-]+)$$/)\
77                         print "FAULTY MIME TYPE: <" $$1 ">" >> "/dev/stderr";\
78                 ltype = $$1; $$1 = "";\
79                 if (pa)\
80                         p_a[ltype] = pa;\
81                 if (!nt_a[ltype])\
82                         no_a[++no_ai] = nt_a[ltype] = ltype;\
83                 add(ltype, $$0)\
84         }\
85         END {\
86                 for (z = 1; z <= no_ai; ++z) {\
87                         t = no_a[z];\
88                         j = index(t, "/");\
89                         mt = toupper(substr(t, 1, j - 1));\
90                         j = substr(t, j + 1);\
91                         l = length(j);\
92                         if (!p_a[t])\
93                                 mt = "_MT_" mt;\
94                         else {\
95                                 tm = p_a[t];\
96                                 if (tm == "@" || tm == "@t@")\
97                                         mt = "_MT_" mt " | _MT_PLAIN";\
98                                 else if (tm == "@h@")\
99                                         mt = "_MT_" mt " | _MT_SOUP_h";\
100                                 else if (tm == "@H@")\
101                                         mt = "_MT_" mt " | _MT_SOUP_H";\
102                         }\
103                         print "   {" mt ", " l ", \"" j e_a[t] "\"},"\
104                 }\
105         }\
106         '
108 uman.1: nail.1
109         $(ECHO_GEN)\
110         LC_ALL=C _SYSCONFRC="$(VAL_SYSCONFRC)" _UAGENT="$(VAL_UAGENT)" \
111                 $(_version_from_header) \
112         < ./nail.1 > $(@) $(awk) '\
113         BEGIN { written = 0 }\
114         /\.\\"--MKMAN-START--/, /\.\\"--MKMAN-END--/ {\
115                 if (written++ != 0)\
116                         next;\
117                 OFS = "";\
118                 print ".ds VV \\\\%v", ENVIRON["VERSION"];\
119                 \
120                 un = toupper(ENVIRON["_UAGENT"]);\
121                 ln = tolower(un);\
122                 cn = toupper(substr(ln, 1, 1)) substr(ln, 2);\
123                 print ".ds UU \\\\%", un;\
124                 print ".ds UA \\\\%", cn;\
125                 print ".ds uA \\\\%", ln;\
126                 \
127                 path = ENVIRON["_SYSCONFRC"];\
128                 gsub("/", "/\\:", path);\
129                 print ".ds UR \\\\%", path;\
130                 OFS = " ";\
131                 next\
132         }\
133         {print}\
134         '
136 urc.rc: nail.rc
137         $(ECHO_GEN)\
138         LC_ALL=C _SYSCONFRC="$(VAL_SYSCONFRC)" _UAGENT="$(VAL_UAGENT)" \
139                 $(_version_from_header) \
140         < ./nail.rc > $(@) $(awk) 'BEGIN {written = 0}\
141         /^#--MKRC-START--/, /^#--MKRC-END--/ {\
142                 if (written == 1)\
143                         next;\
144                 written = 1;\
145                 OFS = "";\
146                 ln = tolower(ENVIRON["_UAGENT"]);\
147                 cn = toupper(substr(ln, 1, 1)) substr(ln, 2);\
148                 print "#@ ", ENVIRON["_SYSCONFRC"];\
149                 print "#@ Configuration file for ", cn, "(1) v",\
150                         ENVIRON["VERSION"];\
151                 OFS = " ";\
152                 next\
153         }\
154         {print}\
155         '
157 install:
158         $(ECHO_BLOCK_BEGIN)\
159         __mkdir() {\
160                 _dir="$(DESTDIR)$${1}";\
161                 if [ -d "$${_dir}" ]; then :; else \
162                         $(mkdir) -m 0755 -p "$${_dir}";\
163                 fi;\
164         };\
165         __copyfile() {\
166                 _mode=$${1} _src=$${2} _xdst=$${3} _dst="$(DESTDIR)$${3}";\
167                 $(cp) -f "$${_src}" "$${_dst}";\
168                 $(chmod) $${_mode} "$${_dst}";\
169                 \
170                 echo "rm -f \"\$${DESTDIR}$${_xdst}\"" >> \
171                         "$(VAL_UAGENT)-uninstall.sh";\
172         };\
173         __copychownfile() {\
174                 _mode=$${1} _ident=$${2} _src=$${3} \
175                         _xdst=$${4} _dst="$(DESTDIR)$${4}";\
176                 $(cp) -f "$${_src}" "$${_dst}";\
177                 $(chown) $${_ident} "$${_dst}";\
178                 $(chmod) $${_mode} "$${_dst}";\
179                 \
180                 echo "rm -f \"\$${DESTDIR}$${_xdst}\"" >> \
181                         "$(VAL_UAGENT)-uninstall.sh";\
182         };\
183         __stripfile() {\
184                 _file=$${1};\
185                 if [ "$(OPT_DEBUG)" != 0 ]; then :;\
186                 elif [ -n "$(HAVE_STRIP)" ]; then \
187                         $(strip) "$${_file}";\
188                 fi;\
189         };\
190         \
191         echo '#!/bin/sh -' > "$(VAL_UAGENT)-uninstall.sh";\
192         echo '#@ Uninstall script for $(VAL_UAGENT)' >> \
193                 "$(VAL_UAGENT)-uninstall.sh";\
194         echo >> "$(VAL_UAGENT)-uninstall.sh";\
195         echo 'DESTDIR="$(DESTDIR)"' >> "$(VAL_UAGENT)-uninstall.sh";\
196         echo 'DESTDIR=' >> "$(VAL_UAGENT)-uninstall.sh";\
197         echo >> "$(VAL_UAGENT)-uninstall.sh";\
198         \
199         if [ -n "$(DESTDIR)" ]; then __mkdir ''; fi;\
200         __mkdir "$(VAL_BINDIR)";\
201         __mkdir "$(VAL_MANDIR)/man1";\
202         __mkdir "$(VAL_SYSCONFDIR)";\
203         \
204         __stripfile "$(VAL_UAGENT)";\
205         __copyfile 0555 "$(VAL_UAGENT)" "$(VAL_BINDIR)/$(VAL_UAGENT)";\
206         __copyfile 0444 ./uman.1 "$(VAL_MANDIR)/man1/$(VAL_UAGENT).1";\
207         if [ -f "$(DESTDIR)$(VAL_SYSCONFDIR)/$(VAL_SYSCONFRC)" ]; then :; else \
208                 __copyfile 0444 ./urc.rc "$(VAL_SYSCONFDIR)/$(VAL_SYSCONFRC)";\
209         fi;\
210         \
211         if [ -n "$(OPTIONAL_PRIVSEP)" ]; then \
212                 __mkdir "$(VAL_LIBEXECDIR)";\
213                 \
214                 __stripfile "$(VAL_PRIVSEP)";\
215                 m='o=rx' o=;\
216                 if [ -n "$(_____PRIVSEP_GROUP)" ]; then \
217                         m="g=rxs,$${m}" o=":$(VAL_PRIVSEP_GROUP)";\
218                 else \
219                         m="g=rx,$${m}";\
220                 fi;\
221                 if [ -n "$(VAL_PRIVSEP_USER)" ]; then \
222                         m="u=rxs,$${m}" o="$(VAL_PRIVSEP_USER)$${o}";\
223                 else \
224                         m="u=rx,$${m}";\
225                 fi;\
226                 __copychownfile "$${m}" "$${o}" "$(VAL_PRIVSEP)" \
227                         "$(VAL_LIBEXECDIR)/$(VAL_PRIVSEP)";\
228         fi;\
229         \
230         if [ -z "$(DESTDIR)" ]; then \
231                 __copyfile 0555 "$(VAL_UAGENT)-uninstall.sh" \
232                         "$(VAL_BINDIR)/$(VAL_UAGENT)-uninstall.sh";\
233         fi;\
234         $(ECHO_BLOCK_END)
236 uninstall:
237         @echo 'Invoke $(VAL_UAGENT)-uninstall.sh to uninstall $(VAL_UAGENT)'
239 clean:
240         $(ECHO_CMD)\
241         $(rm) -f $(OBJ) $(VAL_UAGENT) ./mime_types.h ./uman.1 ./urc.rc;\
242         if [ -n "$(OPTIONAL_PRIVSEP)" ]; then \
243                 $(rm) -f ./privsep.o ./$(VAL_PRIVSEP);\
244         fi;\
245         \
246         if [ -f "$(VAL_UAGENT)-uninstall.sh" ]; then \
247                 $(rm) -f ./$(VAL_UAGENT)-uninstall.sh;\
248         fi
250 distclean: clean
251         $(ECHO_CMD)\
252         $(rm) -f ./config.* ./*.mk
254 # MUST work with "grep=grep sed=sed cmp=cmp mv=mv", otherwise need to change
255 # mk-release.inc
256 _update-version:
257         @LC_ALL=C; export LC_ALL;\
258         if [ -z "$(VERSION)" ]; then \
259                 eval VERSION="`(git describe --tags) 2>/dev/null`";\
260                 if [ -n "$${VERSION}" ]; then \
261                         VERSION="`echo $${VERSION} |\
262                                 $(sed) -e 's/^v\{0,1\}\(.*\)/\1/'`";\
263                         [ "`git status --porcelain |\
264                                 $(awk) '\
265                                         BEGIN {n=0}\
266                                         /version\.h/ {next}\
267                                         /^\?\?/ {next}\
268                                         {++n}\
269                                         END {print n}\
270                                 '`" -ne 0 ] &&\
271                                 VERSION="$${VERSION}-dirty";\
272                 else \
273                         $(_version_from_header);\
274                         echo "$${VERSION}" | $(grep) -q -F dirty ||\
275                                 VERSION="$${VERSION}-dirty";\
276                 fi;\
277         fi;\
278         vmaj="`echo \"$${VERSION}\" |\
279                 $(sed) -e 's/^\([^.]\{1,\}\).*/\1/'`";\
280         vmin="`echo \"$${VERSION}\" |\
281                 $(sed) -e 's/^[^.]\{1,\}\.\([^.]\{1,\}\).*/\1/'`";\
282         [ "$${vmin}" = "$${VERSION}" ] && VERSION="$${VERSION}.0" vmin=0;\
283         vupd="`echo \"$${VERSION}\" |\
284                 $(sed) -e 's/^[^.]\{1,\}\.[^.]\{1,\}\.\([^.-]\{1,\}\).*/\1/'`";\
285         [ "$${vupd}" = "$${VERSION}" ] && VERSION="$${VERSION}.0" vupd=0;\
286         trap "$(rm) ./version.tmp" 0 1 2 15;\
287         printf > ./version.tmp "#define VERSION \"v$${VERSION}\"\n";\
288         printf >> ./version.tmp "#define VERSION_MAJOR \"$${vmaj}\"\n";\
289         printf >> ./version.tmp "#define VERSION_MINOR \"$${vmin}\"\n";\
290         printf >> ./version.tmp "#define VERSION_UPDATE \"$${vupd}\"\n";\
291         $(cmp) ./version.tmp ./version.h >/dev/null 2>&1 && exit;\
292         $(mv) ./version.tmp ./version.h;\
293         trap : 0 1 2 15
295 # s-mk-mode