
ifdef DEBUG
CCLITE=gcc
STFLAGS=
CFLAGS=-DDEBUG
else
CCLITE=i386-uclibc-gcc
#CCLITE=i386-uclibc-linux-gcc
STFLAGS=-DDECRYPT_ONLY -static
CFLAGS=-Os
endif

WIN32CC=i586-mingw32-gcc

SPECFLAGS=$(CFLAGS) -O2 -I. -DBYTE_ORDER=LITTLE_ENDIAN

CRYPTO_FILES = aes.c aes.h pkcs5.c sha2.c sha2.h Makefile
CRYPTO_MAIN = posix win32
CRYPTO_STUBS = $(patsubst %, stub_%, $(CRYPTO_MAIN))
CRYPTO_INCS  = $(patsubst %, %.h, $(CRYPTO_STUBS))

all: # encrypt encryptSelf.posix.o encryptSelf.win32.o

posix: posix.c $(CRYPTO_FILES)
	$(CCLITE) $(SPECFLAGS) $(STFLAGS) -o $@ posix.c
	ls -l $@
	if [ -z $(DEBUG) ] ; then \
		strip $@ ; \
		strip -R .comment $@ ; \
	fi
	ls -l $@

win32r.o: win32.rc
	i586-mingw32-windres -i win32.rc -O coff -o win32r.o

win32: win32.c $(CRYPTO_FILES) win32r.o Makefile
	$(WIN32CC) $(SPECFLAGS) $(STFLAGS) -o $@ win32.c \
		win32r.o -mwindows
	ls -l $@
	if [ -z $(DEBUG) ] ; then \
		i586-mingw32-strip $@ ; \
	fi
	ls -l $@

UPX_FLAGS= --best --crp-ms=999999 --overlay=strip
UPX_WIN32= --compress-icons=1 --compress-resources=1 --strip-relocs=1

stub_posix: posix
ifdef NOUPX
	cp $< $@
else
	rm -f stub_a stub_b
	upx $(UPX_FLAGS) --nrv2b -o stub_a $<
	upx $(UPX_FLAGS) --nrv2d -o stub_b $<
	mv `ls -1Sr stub_a stub_b |head -1` $@
	rm -f stub_a stub_b
endif

stub_win32 : win32
ifdef NOUPX
	cp $< $@
else
	rm -f stub_a stub_b
	upx $(UPX_FLAGS) $(UPX_WIN32) --nrv2b -o stub_a $<
	upx $(UPX_FLAGS) $(UPX_WIN32) --nrv2d -o stub_b $<
	mv `ls -1Sr stub_a stub_b |head -1` $@
	rm -f stub_a stub_b
endif

stub_posix.h: stub_posix gen_dat.pl
	./gen_dat.pl stub_posix $< > $@

stub_win32.h: stub_win32 gen_dat.pl
	./gen_dat.pl stub_win32 $< > $@

encryptSelf.posix.o: encryptSelf.c stub_posix.h stub_win32.h # $(CRYPTO_INCS)
	gcc $(CFLAGS) -c  -o $@ $<

encryptSelf.win32.o: encryptSelf.c stub_win32.h stub_posix.h # $(CRYPTO_INCS)
	$(WIN32CC) -c $(SPECFLAGS) -o $@ $<

encrypt: encrypt.c encryptSelf.posix.o
	gcc -g $(CFLAGS) -o $@ encrypt.c encryptSelf.posix.o -lcrypto

clean:
	rm -f *~ *.o $(CRYPTO_MAIN) $(CRYPTO_STUBS) $(CRYPTO_INCS) encrypt

install:
