#
#  Makefile.in
#
#  Written by Jari Ruusu, April 3 2024
#
#  Copyright 2002-2024 by Jari Ruusu.
#  Redistribution of this file is permitted under the GNU Public License.
#

CC = @CC@ @CPPFLAGS@ @CFLAGS@ @DEFS@
LINK = @CC@ @LDFLAGS@
STRIP = @STRIP_PROG@
MD5SUM = @MD5SUM_PROG@
MD5 = @MD5_PROG@
prefix = @prefix@
exec_prefix = @exec_prefix@
builddir = @builddir@
datarootdir = @datarootdir@
@SET_MAKE@
SKIP_STRIP = true

all x86 i586 amd64: aespipe

aespipe: aespipe.o @OPTIMIZED_OBJS@ sha512.o rmd160.o
	$(LINK) -o aespipe aespipe.o @OPTIMIZED_OBJS@ sha512.o rmd160.o @LIBS@
aespipe.o: $(srcdir)/aespipe.c $(srcdir)/aes.h $(srcdir)/md5.h $(srcdir)/sha512.h $(srcdir)/rmd160.h
	$(CC) -o aespipe.o -c $(srcdir)/aespipe.c
aes.o: $(srcdir)/aes.c $(srcdir)/aes.h
	$(CC) -DCONFIGURE_DETECTS_BYTE_ORDER=1 -DDATA_ALWAYS_ALIGNED=1 -o aes.o -c $(srcdir)/aes.c
aes-x86.o: $(srcdir)/aes-x86.S $(srcdir)/aes.h
	$(CC) -o aes-x86.o -c $(srcdir)/aes-x86.S
aes-amd64.o: $(srcdir)/aes-amd64.S $(srcdir)/aes.h
	$(CC) -o aes-amd64.o -c $(srcdir)/aes-amd64.S
aes-intel32.o: $(srcdir)/aes-intel32.S $(srcdir)/aes.h
	$(CC) -o aes-intel32.o -c $(srcdir)/aes-intel32.S
aes-intel64.o: $(srcdir)/aes-intel64.S $(srcdir)/aes.h
	$(CC) -o aes-intel64.o -c $(srcdir)/aes-intel64.S
aes-armv4.o: $(srcdir)/aes-armv4.S
	$(CC) -o aes-armv4.o -c $(srcdir)/aes-armv4.S
aes-armv8sca.o: $(srcdir)/aes-armv8sca.S
	$(CC) -o aes-armv8sca.o -c $(srcdir)/aes-armv8sca.S
md5.o: $(srcdir)/md5.c $(srcdir)/md5.h
	$(CC) -o md5.o -c $(srcdir)/md5.c
md5-x86.o: $(srcdir)/md5-x86.S $(srcdir)/md5.h
	$(CC) -o md5-x86.o -c $(srcdir)/md5-x86.S
md5-amd64.o: $(srcdir)/md5-amd64.S $(srcdir)/md5.h
	$(CC) -o md5-amd64.o -c $(srcdir)/md5-amd64.S
md5-2x-amd64.o: $(srcdir)/md5-2x-amd64.S $(srcdir)/md5.h
	$(CC) -o md5-2x-amd64.o -c $(srcdir)/md5-2x-amd64.S
sha512.o: $(srcdir)/sha512.c $(srcdir)/sha512.h
	$(CC) -o sha512.o -c $(srcdir)/sha512.c
rmd160.o: $(srcdir)/rmd160.c $(srcdir)/rmd160.h
	$(CC) -o rmd160.o -c $(srcdir)/rmd160.c

clean:
	rm -f *.o aespipe test-file[12345] config.log config.status configure.lineno configure.ac~ configure~
	rm -f -r test-dir1 autom4te.cache
distclean: clean
	rm -f Makefile

install: aespipe
	mkdir -p "$(DESTDIR)@bindir@"
	rm -f "$(DESTDIR)@bindir@/aespipe"
	cp aespipe "$(DESTDIR)@bindir@/aespipe"
	$(SKIP_STRIP) "$(DESTDIR)@bindir@/aespipe"
	chmod 0755 "$(DESTDIR)@bindir@/aespipe"
	mkdir -p "$(DESTDIR)@mandir@/man1"
	rm -f "$(DESTDIR)@mandir@/man1/aespipe.1"
	cp $(srcdir)/aespipe.1 "$(DESTDIR)@mandir@/man1/aespipe.1"
	chmod 0644 "$(DESTDIR)@mandir@/man1/aespipe.1"

install-strip:
	$(MAKE) SKIP_STRIP=$(STRIP) install

tests: aespipe
	dd if=/dev/zero of=test-file1 bs=1024 count=33
	echo 09876543210987654321 >test-file4
	./aespipe -v -p 3 -e AES128 <test-file1 >test-file3 3<test-file4
	echo 12345678901234567890 >test-file4
	$(MAKE) test-part2 PAR="-e AES128" MD=7c1cfd4fdd0d7cc847dd0942a2d48e48 MD5WORK=test-part@MD5WORKPART@
	$(MAKE) test-part2 PAR="-e AES192" MD=51c91bcc04ee2a4ca00310b519b3228c MD5WORK=test-part@MD5WORKPART@
	$(MAKE) test-part2 PAR="-e AES256" MD=1bf92ee337b653cdb32838047dec00fc MD5WORK=test-part@MD5WORKPART@
	$(MAKE) test-part2 PAR="-e AES256 -H rmd160" MD=c85eb59da18876ae71ebd838675c6ef4 MD5WORK=test-part@MD5WORKPART@
	$(MAKE) test-part2 PAR="-e AES256 -C 10" MD=dadad48a6d3d9b9914199626ed7b710c MD5WORK=test-part@MD5WORKPART@
	@GPGWORKPART@rm -fr test-dir1
	@GPGWORKPART@mkdir test-dir1
	@GPGWORKPART@$(MAKE) test-part2 PAR="-e AES128 -K $(srcdir)/gpgkey1.asc -G test-dir1" MD=fa5c9a84bc8f6257830c3cbe60294c69 MD5WORK=test-part@MD5WORKPART@
	@GPGWORKPART@$(MAKE) test-part2 PAR="-e AES192 -K $(srcdir)/gpgkey1.asc -G test-dir1" MD=ddec9544a36100156aef353ec2bf9740 MD5WORK=test-part@MD5WORKPART@
	@GPGWORKPART@$(MAKE) test-part2 PAR="-e AES256 -K $(srcdir)/gpgkey1.asc -G test-dir1" MD=cb38b603f96f0deac1891d423983d69c MD5WORK=test-part@MD5WORKPART@
	@GPGWORKPART@$(MAKE) test-part2 PAR="-e AES128 -K $(srcdir)/gpgkey2.asc -G test-dir1" MD=f9825b79873f5c439ae9371c1a929a6c MD5WORK=test-part@MD5WORKPART@
	@GPGWORKPART@$(MAKE) test-part2 PAR="-e AES192 -K $(srcdir)/gpgkey2.asc -G test-dir1" MD=489991b779213f60219f09c575c08247 MD5WORK=test-part@MD5WORKPART@
	@GPGWORKPART@$(MAKE) test-part2 PAR="-e AES256 -K $(srcdir)/gpgkey2.asc -G test-dir1" MD=2a1d0d3fce83fbe5f3edcca95fbab3b7 MD5WORK=test-part@MD5WORKPART@
	@GPGWORKPART@$(MAKE) test-part2 PAR="-e AES128 -K $(srcdir)/gpgkey3.asc -G test-dir1" MD=fabe7422f534820838dfd4571ba14ade MD5WORK=test-part@MD5WORKPART@
	@GPGWORKPART@$(MAKE) test-part2 PAR="-e AES192 -K $(srcdir)/gpgkey3.asc -G test-dir1" MD=3eadc976525f9df7e18d56676ec730c8 MD5WORK=test-part@MD5WORKPART@
	@GPGWORKPART@$(MAKE) test-part2 PAR="-e AES256 -K $(srcdir)/gpgkey3.asc -G test-dir1" MD=3be488a60dd77bcab9fbeba4a428c3d5 MD5WORK=test-part@MD5WORKPART@
	echo 1234567890123456789012345678901 >test-file4
	$(MAKE) test-part2 PAR="-e AES -H unhashed1" MD=293b09053055af7ca5235dc6a5bc0b74 MD5WORK=test-part@MD5WORKPART@
	echo 12345678901234567890123456789012 >test-file4
	$(MAKE) test-part2 PAR="-e AES -H unhashed1" MD=6b157917570250ef4370bf9acae49279 MD5WORK=test-part@MD5WORKPART@
	echo 123456789012345678901234567890123456789012 >test-file4
	$(MAKE) test-part2 PAR="-e AES -H unhashed1" MD=6b157917570250ef4370bf9acae49279 MD5WORK=test-part@MD5WORKPART@
	echo 1234567890123456789012345678901234567890123 >test-file4
	$(MAKE) test-part2 PAR="-e AES -H unhashed1" MD=e12fd55fbae9fc0e03517593e253e239 MD5WORK=test-part@MD5WORKPART@
	dd if=/dev/zero of=test-file1 bs=512 count=73
	echo 09876543210987654321 >test-file4
	./aespipe -v -p 3 -e AES128 <test-file1 >test-file3 3<test-file4
	echo 12345678901234567890 >test-file4
	@GPGWORKPART@$(MAKE) test-part2 PAR="-K $(srcdir)/gpgkey1.asc -G test-dir1" MD=58eb118f3eadab10f89aac2dd5ecbc79 MD5WORK=test-part@MD5WORKPART@
	@GPGWORKPART@$(MAKE) test-part2 PAR="-K $(srcdir)/gpgkey2.asc -G test-dir1" MD=72b990b09cf692b27a31440588929dd3 MD5WORK=test-part@MD5WORKPART@
	@GPGWORKPART@$(MAKE) test-part2 PAR="-K $(srcdir)/gpgkey3.asc -G test-dir1" MD=b8d45f6bd3aba2fe627f704db2c392ae MD5WORK=test-part@MD5WORKPART@
	dd if=/dev/zero of=test-file1 bs=16 count=35
	echo 09876543210987654321 >test-file4
	./aespipe -v -p 3 -e AES128 <test-file1 >test-file3 3<test-file4
	echo 12345678901234567890 >test-file4
	$(MAKE) test-part2 PAR="-e AES128" MD=0af0b54857ad0bf3941e68c27924610e MD5WORK=test-part@MD5WORKPART@
	$(MAKE) test-part2 PAR="-e AES192" MD=0517f71802b757818be319216272ec41 MD5WORK=test-part@MD5WORKPART@
	$(MAKE) test-part2 PAR="-e AES256" MD=733082aba642bdf1d37fc0ea7eabbdbf MD5WORK=test-part@MD5WORKPART@
	rm -f -r test-file[12345] test-dir1
	@echo "*** Test results ok ***"
test-part2:
	./aespipe -v -p 3 $(PAR) <test-file3 >test-file1 3<test-file4
	$(MAKE) $(MD5WORK)
	cmp test-file2 test-file5
	./aespipe -v -d -P test-file4 $(PAR) <test-file1 >test-file2
	cmp test-file3 test-file2
test-part3:
	$(MD5SUM) test-file1 >test-file2
	echo "$(MD)  test-file1" >test-file5
test-part4:
	$(MD5) test-file1 >test-file2
	echo "MD5 (test-file1) = $(MD)" >test-file5
test-part5:
	echo "NO MD5 TEST" >test-file2
	echo "NO MD5 TEST" >test-file5

.PHONY: all x86 i586 amd64 clean distclean install install-strip tests test-part2 test-part3 test-part4 test-part5
