SHELL	= /bin/sh
SRCS	= postmap.c 
OBJS	= postmap.o
HDRS	= 
TESTSRC	= 
DEFS	= -I. -I$(INC_DIR) -D$(SYSTYPE)
CFLAGS	= $(DEBUG) $(OPT) $(DEFS)
TESTPROG= 
PROG	= postmap
INC_DIR	= ../../include
LIBS	= ../../lib/lib$(LIB_PREFIX)global$(LIB_SUFFIX) \
	../../lib/lib$(LIB_PREFIX)util$(LIB_SUFFIX)
NOSLEEP = LD_PRELOAD=../../lib/nosleep.so

.c.o:;	$(CC) $(CFLAGS) -c $*.c

$(PROG):	$(OBJS) $(LIBS)
	$(CC) $(CFLAGS) $(SHLIB_RPATH) -o $@ $(OBJS) $(LIBS) $(SYSLIBS)

$(OBJS): ../../conf/makedefs.out

Makefile: Makefile.in
	cat ../../conf/makedefs.out $? >$@

update: ../../bin/$(PROG)

../../bin/$(PROG): $(PROG)
	cp $(PROG) ../../bin

tests:	test1 test2 fail_test quote_test file_test lmdb_abb_test \
	lmdb_bulk_test lmdb_incr_test cdb_bulk_test mode_conflict_test \
	json_tests

root_tests:

test1:	$(PROG) map.in map-abc1.ref map-ghi1.ref map-uABC1.ref
	@echo ; echo RUN test1
	rm -f main.cf
	cp ../../test-conf/main.cf .
	touch -t 197101010000 main.cf
	$(SHLIB_ENV) $(VALGRIND) ./$(PROG) -c . map.in
	for key in abc ghi; \
	do \
	    $(SHLIB_ENV) $(VALGRIND) ./$(PROG) -c . -q $${key} map.in | diff map-$${key}1.ref -; \
	done
	$(SHLIB_ENV) $(VALGRIND) ./$(PROG) -c . -f map.in
	for key in ABC; \
	do \
	    $(SHLIB_ENV) $(VALGRIND) ./$(PROG) -c . -fq $${key} map.in | diff map-u$${key}1.ref -; \
	done
	rm -f map.in.db main.cf
	@echo PASS test1

test2:	$(PROG) map.in map-abc2.ref map-ghi2.ref map-uABC2.ref
	@echo ; echo RUN test2
	rm -f main.cf
	cp ../../test-conf/main.cf .
	touch -t 197101010000 main.cf
	$(SHLIB_ENV) $(VALGRIND) ./$(PROG) -c . map.in
	for key in abc ghi; \
	do \
	    echo $${key} | $(SHLIB_ENV) $(VALGRIND) ./$(PROG) -c . -q - map.in | diff map-$${key}2.ref -; \
	done
	$(SHLIB_ENV) $(VALGRIND) ./$(PROG) -c . -f map.in
	for key in ABC; \
	do \
	    echo $${key} | $(SHLIB_ENV) $(VALGRIND) ./$(PROG) -c . -fq - map.in | diff map-u$${key}2.ref -; \
	done
	rm -f map.in.db main.cf
	@echo PASS test2

fail_test: $(PROG) aliases fail_test.in fail_test.ref
	@echo ; echo RUN fail_test
	rm -f main.cf
	cp ../../test-conf/main.cf .
	touch -t 197101010000 main.cf
	-($(SHLIB_ENV) MAIL_CONFIG=. $(NOSLEEP) sh fail_test.in || exit 0) >fail_test.tmp 2>&1
	diff fail_test.ref fail_test.tmp
	rm -f fail_test.tmp main.cf
	@echo PASS fail_test

quote_test: $(PROG) aliases quote_test.in quote_test.ref
	@echo ; echo RUN quote_test
	rm -f main.cf
	cp ../../test-conf/main.cf .
	touch -t 197101010000 main.cf
	rm -f quote_test_map.*
	$(SHLIB_ENV) MAIL_CONFIG=. sh quote_test.in >quote_test.tmp 2>&1
	diff quote_test.ref quote_test.tmp
	rm -f quote_test.tmp quote_test_map.* main.cf
	@echo PASS quote_test

file_test: $(PROG) file_test.in file_test.ref
	@echo ; echo RUN file_test
	rm -f main.cf
	cp ../../test-conf/main.cf .
	touch -t 197101010000 main.cf
	rm -f file_test_map.* postmap-file-1 postmap-file-2
	$(SHLIB_ENV) MAIL_CONFIG=. $(NOSLEEP) sh file_test.in >file_test.tmp 2>&1
	diff file_test.ref file_test.tmp
	rm -f file_test.tmp file_test_map.* postmap-file-1 postmap-file-2 \
	    main.cf
	@echo PASS file_test

mode_conflict_test: $(PROG) mode_conflict_test.in mode_conflict_test.ref
	@echo ; echo RUN mode_conflict_test
	rm -f main.cf
	cp ../../test-conf/main.cf .
	touch -t 197101010000 main.cf
	$(SHLIB_ENV) MAIL_CONFIG=. $(NOSLEEP) sh -x mode_conflict_test.in >mode_conflict_test.tmp 2>&1
	diff mode_conflict_test.ref mode_conflict_test.tmp
	rm -f mode_conflict_test.tmp main.cf
	@echo PASS mode_conflict_test

cdb_bulk_test: $(PROG)
	@echo ; echo RUN cdb_bulk_test
	rm -f cdb_bulk.cdb main.cf
	cp ../../test-conf/main.cf .
	tr A-Z a-z < /usr/share/dict/words| \
	    sed -e 's/.*/&	&/' -e 10000q| LANG=C sort -u >cdb_bulk
	touch -t 197101010000 main.cf
	($(SHLIB_ENV) $(VALGRIND) ./postmap -c . cdb:cdb_bulk; \
	$(SHLIB_ENV) $(VALGRIND) ./postmap -c . -s cdb:cdb_bulk | \
	    LANG=C sort > cdb_bulk.tmp)
	cmp cdb_bulk cdb_bulk.tmp
	rm -f cdb_bulk cdb_bulk.tmp cdb_bulk.cdb main.cf
	@echo PASS cdb_bulk_test

lmdb_abb_test: $(PROG) lmdb_abb lmdb_abb.ref
	@echo ; echo RUN lmdb_abb_test
	rm -f main.cf
	cp ../../test-conf/main.cf .
	touch -t 197101010000 main.cf
	rm -f lmdb_abb.lmdb
	($(SHLIB_ENV) $(VALGRIND) ./postmap -c . lmdb:lmdb_abb; \
	$(SHLIB_ENV) $(VALGRIND) ./postmap -c . -s lmdb:lmdb_abb | sort) >lmdb_abb.tmp 2>&1
	diff lmdb_abb.ref lmdb_abb.tmp
	rm -f lmdb_abb.tmp lmdb_abb.lmdb
	@echo PASS lmdb_abb_test

lmdb_bulk_test: $(PROG)
	@echo ; echo RUN lmdb_bulk_test
	rm -f lmdb_retry.lmdb main.cf
	cp ../../test-conf/main.cf .
	tr A-Z a-z < /usr/share/dict/words| \
	    sed -e 's/.*/&	&/' -e 10000q| LANG=C sort -u >lmdb_retry
	echo lmdb_map_size=10240 >>main.cf
	touch -t 197101010000 main.cf
	($(SHLIB_ENV) $(VALGRIND) ./postmap -c . lmdb:lmdb_retry; \
	$(SHLIB_ENV) $(VALGRIND) ./postmap -c . -s lmdb:lmdb_retry | \
	    LANG=C sort > lmdb_retry.tmp)
	cmp lmdb_retry lmdb_retry.tmp
	rm -f lmdb_retry lmdb_retry.tmp lmdb_retry.lmdb main.cf
	@echo PASS lmdb_bulk_test

lmdb_incr_test: $(PROG)
	@echo ; echo RUN lmdb_incr_test
	rm -f lmdb_retry.lmdb main.cf
	cp ../../test-conf/main.cf .
	tr A-Z a-z < /usr/share/dict/words| \
	    sed -e 's/.*/&	&/' -e 1000q| LANG=C sort -u >lmdb_retry
	echo lmdb_map_size=10240 >>main.cf
	touch -t 197101010000 main.cf
	($(SHLIB_ENV) $(VALGRIND) ./postmap -ic . lmdb:lmdb_retry <lmdb_retry; \
	$(SHLIB_ENV) $(VALGRIND) ./postmap -c . -s lmdb:lmdb_retry | \
	    LANG=C sort > lmdb_retry.tmp)
	cmp lmdb_retry lmdb_retry.tmp
	rm -f lmdb_retry lmdb_retry.tmp lmdb_retry.lmdb main.cf
	@echo PASS lmdb_incr_test

json_tests: json_query_test json_queries_test json_sequence_test \
	json_header_test json_body_test

json_query_test: $(PROG)
	@echo ; echo RUN json_query_test
	rm -f main.cf
	cp ../../test-conf/main.cf .
	touch -t 197101010000 main.cf
	# Exercise code in postmap_query().
	echo '{"foo": "fooval"}' >json_query.tmp
	$(SHLIB_ENV) ${VALGRIND} ./$(PROG)  -jq foo \
		'inline:{{foo = fooval}}' | diff json_query.tmp -
	($(SHLIB_ENV) ${VALGRIND} ./$(PROG)  -jq bar \
		'inline:{{foo = fooval}}' || exit 0) | diff /dev/null -
	rm -f json_query.tmp main.cf
	@echo PASS json_query_test

json_queries_test: $(PROG)
	@echo ; echo RUN json_queries_test
	# Exercise json formatting in postmap_queries() with non-message input.
	rm -f main.cf
	cp ../../test-conf/main.cf .
	touch -t 197101010000 main.cf
	echo '{"foo": "fooval"}' >json_queries.tmp
	(echo foo; echo bar) | $(SHLIB_ENV) ${VALGRIND} \
		./$(PROG) -jq - 'inline:{{foo = fooval}}' | \
		diff json_queries.tmp -
	rm -f json_queries.tmp main.cf
	@echo PASS json_queries_test

json_sequence_test: $(PROG)
	# Exercise json formatting in postmap_seq().
	@echo ; echo RUN json_sequence_test
	rm -f main.cf
	cp ../../test-conf/main.cf .
	touch -t 197101010000 main.cf
	rm -f json_sequence.tmp
	(echo '{"bar": "barval"}'; echo '{"foo": "fooval"}') |\
		sort >json_sequence.tmp
	$(SHLIB_ENV) ${VALGRIND} ./$(PROG) -js \
		'inline:{{foo = fooval} {bar = barval}}' | sort | \
		diff json_sequence.tmp -
	rm -f json_sequence.tmp main.cf
	@echo PASS json_sequence_test

json_header_test: $(PROG)
	# Exercise json formatting in postmap_queries() in 'header' mode.
	@echo ; echo RUN json_header_test
	rm -f main.cf
	cp ../../test-conf/main.cf .
	touch -t 197101010000 main.cf
	echo '{"Subject: test": "got subject"}' > json_header.tmp
	(echo Subject: test; echo body) | $(SHLIB_ENV) ${VALGRIND} ./$(PROG) \
		-hjq - 'regexp:{{ /Subject/ got subject} { /./ got other }}' | \
		diff json_header.tmp -
	rm -f json_header.tmp main.cf
	@echo PASS json_header_test

json_body_test: $(PROG)
	# Exercise json formatting in postmap_queries() in 'body' mode.
	@echo ; echo RUN json_body_test
	rm -f main.cf
	cp ../../test-conf/main.cf .
	touch -t 197101010000 main.cf
	echo '{"body": "got other"}' > json_body.tmp
	(echo Subject: test; echo body) | $(SHLIB_ENV) ${VALGRIND} ./$(PROG) \
		-bjq - 'regexp:{{ /Subject/ got subject} { /./ got other }}' | \
		diff json_body.tmp -
	rm -f json_body.tmp main.cf
	@echo PASS json_body_test

clean:
	rm -f *.o *core $(PROG) $(TESTPROG) *.tmp junk *.db main.cf master.cf

tidy:	clean

depend: $(MAKES)
	(sed '1,/^# do not edit/!d' Makefile.in; \
	set -e; for i in [a-z][a-z0-9]*.c; do \
	    $(CC) -E $(DEFS) $(INCL) $$i | grep -v '[<>]' | sed -n -e '/^# *1 *"\([^"]*\)".*/{' \
	    -e 's//'`echo $$i|sed 's/c$$/o/'`': \1/' \
	    -e 's/o: \.\//o: /' -e p -e '}' ; \
	done | LANG=C sort -u) | grep -v '[.][o][:][ ][/]' >$$$$ && mv $$$$ Makefile.in
	@$(EXPORT) make -f Makefile.in Makefile 1>&2

# do not edit below this line - it is generated by 'make depend'
postmap.o: ../../include/argv.h
postmap.o: ../../include/check_arg.h
postmap.o: ../../include/clean_env.h
postmap.o: ../../include/dict.h
postmap.o: ../../include/dict_db.h
postmap.o: ../../include/dict_proxy.h
postmap.o: ../../include/header_opts.h
postmap.o: ../../include/mail_conf.h
postmap.o: ../../include/mail_dict.h
postmap.o: ../../include/mail_params.h
postmap.o: ../../include/mail_parm_split.h
postmap.o: ../../include/mail_task.h
postmap.o: ../../include/mail_version.h
postmap.o: ../../include/maillog_client.h
postmap.o: ../../include/mime_state.h
postmap.o: ../../include/mkmap.h
postmap.o: ../../include/msg.h
postmap.o: ../../include/msg_vstream.h
postmap.o: ../../include/myflock.h
postmap.o: ../../include/mymalloc.h
postmap.o: ../../include/readlline.h
postmap.o: ../../include/rec_type.h
postmap.o: ../../include/set_eugid.h
postmap.o: ../../include/split_at.h
postmap.o: ../../include/stringops.h
postmap.o: ../../include/sys_defs.h
postmap.o: ../../include/vbuf.h
postmap.o: ../../include/vstream.h
postmap.o: ../../include/vstring.h
postmap.o: ../../include/vstring_vstream.h
postmap.o: ../../include/warn_stat.h
postmap.o: postmap.c
