SHELL	= /bin/sh
SRCS	= postalias.c 
OBJS	= postalias.o 
HDRS	= 
TESTSRC	= 
DEFS	= -I. -I$(INC_DIR) -D$(SYSTYPE)
CFLAGS	= $(DEBUG) $(OPT) $(DEFS)
TESTPROG= 
PROG	= postalias
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)

tests:	test1 test2 fail_test mode_conflict_test json_tests

root_tests:

test1:	$(PROG) map.in map-abc1.ref map-ghi1.ref map-uABC1.ref
	rm -f 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

test2:	$(PROG) map.in map-abc2.ref map-ghi2.ref map-uABC2.ref
	rm -f 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

fail_test: $(PROG) aliases fail_test.in fail_test.ref
	rm -f main.cf
	touch -t 197101010000 main.cf
	-(${SHLIB_ENV} MAIL_CONFIG=. $(NOSLEEP) sh fail_test.in 2>&1 || exit 0) | sed \
	    -e 's/No error:/Unknown error:/' \
	    -e 's/Success/Unknown error: 0/' > fail_test.tmp
	diff fail_test.ref fail_test.tmp
	rm -f fail_test.tmp main.cf

mode_conflict_test: $(PROG) mode_conflict_test.in mode_conflict_test.ref
	rm -f 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

json_tests: json_query_test json_queries_test json_sequence_test

json_query_test: $(PROG)
	# Exercise code in postalias_query().
	@echo ; echo RUN json_query_test
	echo '{"postmaster": "root"}' >json_query.tmp
	$(SHLIB_ENV) ${VALGRIND} ./$(PROG)  -jq postmaster \
		'inline:{{postmaster = root}}' | diff json_query.tmp -
	($(SHLIB_ENV) ${VALGRIND} ./$(PROG)  -jq other \
		'inline:{{postmaster = root}}' || exit 0) | diff /dev/null -
	rm -f json_query.tmp
	@echo PASS json_query_test

json_queries_test: $(PROG)
	# Exercise json formatting in postalias_queries().
	@echo ; echo RUN json_queries_test
	echo '{"postmaster": "root"}' >json_queries.tmp
	(echo postmaster; echo other) | $(SHLIB_ENV) ${VALGRIND} \
		./$(PROG) -jq - 'inline:{{postmaster = root}}' | \
		diff json_queries.tmp -
	rm -f json_queries.tmp 
	@echo PASS json_queries_test

json_sequence_test: $(PROG)
	# Exercise json formatting in postalias_seq().
	@echo ; echo RUN json_sequence_test
	rm -f json_sequence.tmp
	(echo '{"postmaster": "root"}'; echo '{"root": "real-user"}') |\
		sort >json_sequence.tmp
	$(SHLIB_ENV) ${VALGRIND} ./$(PROG) -js \
		'inline:{{postmaster = root} {root=real-user}}' | sort | \
		diff json_sequence.tmp -
	rm -f json_sequence.tmp
	@echo PASS json_sequence_test

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

clean:
	rm -f *.o *core $(PROG) $(TESTPROG) junk 

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'
postalias.o: ../../include/argv.h
postalias.o: ../../include/check_arg.h
postalias.o: ../../include/clean_env.h
postalias.o: ../../include/dict.h
postalias.o: ../../include/dict_db.h
postalias.o: ../../include/dict_proxy.h
postalias.o: ../../include/mail_conf.h
postalias.o: ../../include/mail_dict.h
postalias.o: ../../include/mail_params.h
postalias.o: ../../include/mail_parm_split.h
postalias.o: ../../include/mail_task.h
postalias.o: ../../include/mail_version.h
postalias.o: ../../include/maillog_client.h
postalias.o: ../../include/mkmap.h
postalias.o: ../../include/msg.h
postalias.o: ../../include/msg_vstream.h
postalias.o: ../../include/myflock.h
postalias.o: ../../include/mymalloc.h
postalias.o: ../../include/readlline.h
postalias.o: ../../include/resolve_clnt.h
postalias.o: ../../include/set_eugid.h
postalias.o: ../../include/split_at.h
postalias.o: ../../include/stringops.h
postalias.o: ../../include/sys_defs.h
postalias.o: ../../include/tok822.h
postalias.o: ../../include/vbuf.h
postalias.o: ../../include/vstream.h
postalias.o: ../../include/vstring.h
postalias.o: ../../include/vstring_vstream.h
postalias.o: ../../include/warn_stat.h
postalias.o: postalias.c
