CFLAGS = -O -DNDEBUG
# CFLAGS = -Wall -g

prefix = /usr/local
# prefix = $(HOME)



all: mail-transcode

install: mail-transcode mail-transcode.1
	install -d $(prefix)/bin $(prefix)/share/man/man1
	install -p -s mail-transcode $(prefix)/bin
	install -p mail-transcode.1 $(prefix)/share/man/man1

clean:; rm -f mail-transcode test*.in test*.lf test*.out



check: test1 test2 test3 test4 test5 test6 test7 test8 test9 test10 \
	test11 test12 test13

%: %.in %.out; @cmp -s $+ && echo $*: OK || echo $*: FAIL
%.in: %.lf; @perl -pe 's/\r?\n/\r\n/' $< >$@

test1.lf:
	@echo 'unused-header1: multipart/none; boundary=foo'		>$@
	@echo 'unused-header2: base64'					>>$@
	@echo 'Content-Transfer-Encoding: binary'			>>$@
	@echo ''							>>$@
	@echo 'Test'							>>$@
test1.out: test1.in all
	@./mail-transcode -q $< | ./mail-transcode >$@

test2.lf:
	@echo 'unused-header1: multipart/none; boundary=foo'		>$@
	@echo 'unused-header2: base64'					>>$@
	@echo 'Content-Transfer-Encoding: binary'			>>$@
	@echo ''							>>$@
	@echo 'Test'							>>$@
test2.out: test2.in all
	@./mail-transcode -b $< | ./mail-transcode >$@

test3.lf:
	@echo 'unused-header1: multipart/none; boundary=foo'		>$@
	@echo 'unused-header2: base64'					>>$@
	@echo 'content-transfer-encoding: 8bit'				>>$@
	@echo ''							>>$@
	@echo 'Test'							>>$@
test3.out: test3.in all
	@./mail-transcode -q $< | ./mail-transcode >$@

test4.lf:
	@echo 'Content-Type: multipart/alternative; boundary="foo"'	>$@
	@echo ''							>>$@
	@echo ''							>>$@
	@echo '--foo'							>>$@
	@echo 'Unused-Header: header1'					>>$@
	@echo ''							>>$@
	@echo 'Untouched body'						>>$@
	@echo ''							>>$@
	@echo '--foo'							>>$@
	@echo 'Content-Transfer-Encoding: quoted-printable'		>>$@
	@echo ''							>>$@
	@echo 'Test  =20'						>>$@
	@echo ''							>>$@
	@echo '--foo--'							>>$@
test4.out: test4.in all
	@./mail-transcode $< | ./mail-transcode -q >$@

test5.lf:
	@echo 'Content-Type: multipart/alternative; boundary="foo"'	>$@
	@echo ''							>>$@
	@echo ''							>>$@
	@echo '--foo'							>>$@
	@echo 'Content-Transfer-Encoding: base64'			>>$@
	@echo ''							>>$@
	@echo 'VGVzdA0K'						>>$@
	@echo ''							>>$@
	@echo '--foo'							>>$@
	@echo 'Unused-Header: header1'					>>$@
	@echo ''							>>$@
	@echo 'Untouched body'						>>$@
	@echo ''							>>$@
	@echo '--foo--'							>>$@
test5.out: test5.in all
	@./mail-transcode $< | ./mail-transcode -b >$@

test6.lf:
	@echo 'Content-Transfer-Encoding: quoted-printable'		>$@
	@echo ''							>>$@
	@echo 'Test with two options'					>>$@
test6.out: test6.in all
	@./mail-transcode $< | ./mail-transcode -b -q >$@

test7.in:
	@echo './mail-transcode version 1.0'				>$@
test7.out: all
	@./mail-transcode -v >$@

test8.in:
	@echo 'Usage: ./mail-transcode [-v] [-b] [-q] [file]'		>$@
test8.out: all
	@./mail-transcode -? 2>$@ || true

test9.in:
	@echo 'Usage: ./mail-transcode [-v] [-b] [-q] [file]'		>$@
test9.out: all
	@./mail-transcode one two 2>$@ || true

test10.in:
	@echo 'mail-transcode: Input must have both headers and a body'	>$@
test10.out: all
	@./mail-transcode /dev/null 2>$@ || true

test11.in:
	@echo 'Content-Transfer-Encoding: binary'			>$@
	@echo ''							>>$@
	@echo 'Test with LF instead of CRLF'				>>$@
test11.out: test11.in all
	@./mail-transcode -q $< | ./mail-transcode >$@

test12.lf:
	@echo 'Content-Transfer-Encoding: quoted-printable'		>$@
	@echo ''							>>$@
	@echo 'Test with raw =-signs'					>>$@
	@echo 'Test with almost escapes: =AZ and =3'			>>$@
test12.in:
	@echo 'Content-Transfer-Encoding: binary'			>$@
	@echo ''							>>$@
	@echo 'Test with raw =-signs'					>>$@
	@echo 'Test with almost escapes: =AZ and =3'			>>$@
test12.out: test12.lf all
	@./mail-transcode $< >$@

test13.lf:
	@echo 'Content-Transfer-Encoding: quoted-printable'		>$@
	@echo 'Content-Type: text/plain; charset="iso-8859-1"'		>>$@
	@echo ''							>>$@
	@echo 'Fran=E7ois'						>>$@
test13.out: test13.in all
	@./mail-transcode $< | ./mail-transcode -q >$@
