# pssh makefile

.PHONY=bin rcp pssh-arm all install clean build
.SUFFIXES=

OPTIMIZATION = -Os
WARNINGS = -W -Wall -Wundef -Wshadow -Wmissing-declarations -Wmissing-prototypes -Wpointer-arith -Wno-format -Wuninitialized

# CFLAGS = -g
# LDFLAGS = -g


# OpenSSL
BN_SRCS = bn/bn_armstubs.c 
AES_SRCS = aes/aes_armstubs.c aes/aes_tables.c
DH_SRCS = dh/dh_key.c dh/dh_lib.c
DES_SRCS = des/des_enc.c des/set_key.c des/des_tables.c
DSA_SRCS = dsa/dsa_lib.c dsa/dsa_ossl.c dsa/dsa_sign.c dsa/dsa_vrf.c
EVP_SRCS = evp/digest.c evp/e_des3.c evp/e_aes.c evp/e_null.c evp/evp_enc.c evp/m_md5.c evp/m_sha1.c 
HMAC_SRCS = hmac/hmac.c 
MD5_SRCS = md5/md5_dgst.c md5/md5_one.c
RSA_SRCS = rsa/rsa_eay.c rsa/rsa_lib.c rsa/rsa_none.c rsa/rsa_pk1.c 
SHA_SRCS = sha/sha1dgst.c

OPENSSL_SRCS = $(addprefix openssl/, \
	mem_clr.c \
	$(AES_SRCS) $(BN_SRCS) $(DH_SRCS) $(DES_SRCS) $(DSA_SRCS) $(EVP_SRCS) \
	$(HMAC_SRCS) $(MD5_SRCS) $(RSA_SRCS) $(SHA_SRCS) \
)

# Non-OpenSSL crypto (sha2, Fortuna)
FORTUNA_SRCS = fortuna/fortuna.c fortuna/fortuna_gen.c fortuna/fortuna_acc.c
SHA2_SRCS = sha2/sha2.c

# crypto (OpenSSL and otherwise)
CRYPTO_SRCS = $(addprefix crypto/, \
	rand.c $(FORTUNA_SRCS) $(OPENSSL_SRCS) $(SHA2_SRCS) \
)

# OpenSSH
OPENSSH_SRCS = $(addprefix openssh/, \
	bufaux.c buffer.c cipher.c compress.c dh.c kex.c kexdh.c key.c mac.c \
	match.c ssh-dss.c ssh-rsa.c uuencode.c openbsd-compat/base64.c \
	openbsd-compat/bsd-snprintf.c \
)

# ssh (OpenSSH and custom)
SSH_SRCS = $(addprefix ssh/, \
	connection.c keyfile.c keyimport.c packetizer.c session.c ssh.c transport.c \
	$(OPENSSH_SRCS) \
)

# util
UTIL_SRCS = $(addprefix util/, \
	armstubs.c formutils.c queue.c stdlib.c xmalloc.c \
)

# vt100 (PuTTY)
VT100_SRCS = $(addprefix vt100/, \
	tree234.c terminal.c wcwidth.c misc.c \
	palm/palmprint.c palm/palmucs.c palm/palmwindow.c \
	vt100.c \
)

# Palm forms
FORM_SRCS = $(addprefix forms/, \
	about.c connectionprefsform.c detailsform.c displayprefsform.c \
	hostkeysform.c hostkeydetailsform.c keyboardprefsform.c loginform.c \
	mainform.c memoform.c passwordform.c passphraseform.c \
	publickeysform.c publickeychoiceform.c publickeydetailsform.c \
	terminalform.c CollapseUtilsSony.c \
)

# Palm data
DATA_SRCS = $(addprefix data/, \
	connectionlist.c hostkeys.c memolist.c prefs.c publickeys.c \
	recordlist.c \
)

# Everything 68K
SRCS = $(VT100_SRCS) main.c $(CRYPTO_SRCS) $(FORM_SRCS) $(DATA_SRCS) $(SSH_SRCS) \
	$(UTIL_SRCS)
OBJECTS = $(addprefix build/,$(SRCS:.c=.o))

# rsrc
RSRC_HDRS = rsrc/rsrc.h crypto/cryptorsrc.h
RSRC_SRCS = crypto/openssl/aes/aes_tables.rcp crypto/openssl/des/des_tables.rcp rsrc/pssh.rcp
RSRC_OBJS = $(addprefix build/,$(RSRC_SRCS:.rcp=.ro))

OBJDIRS = $(addprefix build/,$(sort $(dir $(SRCS) $(dir $(RSRC_SRCS)))))


all: install

install: pssh.prc
	open pssh.prc

pssh.prc: pssh.def pssh pssh-arm $(RSRC_OBJS)
	build-prc -o pssh.prc pssh.def pssh arm/pssh-arm $(RSRC_OBJS)

pssh: $(OBJDIRS) $(OBJECTS) pssh-sections.o pssh-sections.ld
	m68k-palmos-gcc $(LDFLAGS) $(OBJECTS) pssh-sections.o pssh-sections.ld -lNetSocket -lPalmOSGlue -o pssh

pssh-sections.o: pssh-sections.s
	m68k-palmos-gcc -c pssh-sections.s

pssh-sections.s pssh-sections.ld: pssh.def
	m68k-palmos-multigen pssh.def

pssh-arm: 
	make -C arm pssh-arm

# fixme need to handle ARM-endian resources better
build/crypto/openssl/aes/aes_tables.ro : crypto/openssl/aes/aes_tables.rcp
	pilrc -LE32 -ro -I crypto -I rsrc $< $@

build/%.ro : %.rcp
	pilrc -ro -I crypto -I rsrc $< $@

$(OBJDIRS): 
	mkdir -p $@


# 68K C file targets

define compile
	m68k-palmos-gcc $(CFLAGS) $(OPTIMIZATION) $(WARNINGS) \
	   -I. -Iutil -Iutil/oem -Irsrc $(addprefix -I,$1) \
	   -c $2 -o $3
endef

build/crypto/openssl/aes/%.o  : crypto/openssl/aes/%.c
	$(call compile, crypto crypto/openssl crypto/openssl/aes,  $<, $@ )

build/crypto/openssl/bn/%.o   : crypto/openssl/bn/%.c
	$(call compile, crypto crypto/openssl crypto/openssl/bn,   $<, $@ )

build/crypto/openssl/des/%.o  : crypto/openssl/des/%.c
	$(call compile, crypto crypto/openssl crypto/openssl/des,  $<, $@ )

build/crypto/openssl/dh/%.o   : crypto/openssl/dh/%.c
	$(call compile, crypto crypto/openssl crypto/openssl/dh,   $<, $@ )

build/crypto/openssl/dsa/%.o  : crypto/openssl/dsa/%.c
	$(call compile, crypto crypto/openssl crypto/openssl/dsa,  $<, $@ )

build/crypto/openssl/evp/%.o  : crypto/openssl/evp/%.c
	$(call compile, crypto crypto/openssl crypto/openssl/evp,  $<, $@ )

build/crypto/openssl/hmac/%.o : crypto/openssl/hmac/%.c
	$(call compile, crypto crypto/openssl crypto/openssl/hmac, $<, $@ )

build/crypto/openssl/md5/%.o  : crypto/openssl/md5/%.c
	$(call compile, crypto crypto/openssl crypto/openssl/md5,  $<, $@ )

build/crypto/openssl/sha/%.o  : crypto/openssl/sha/%.c
	$(call compile, crypto crypto/openssl crypto/openssl/sha,  $<, $@ )


build/crypto/fortuna/%.o  : crypto/fortuna/%.c
	$(call compile, crypto crypto/fortuna,  $<, $@ )

build/crypto/openssl/%.o  : crypto/openssl/%.c
	$(call compile, crypto crypto/openssl,  $<, $@ )

build/crypto/sha2/%.o  : crypto/sha2/%.c
	$(call compile, crypto crypto/sha2,  $<, $@ )


build/crypto/%.o : crypto/%.c
	$(call compile, crypto, $<, $@)

build/ssh/openssh/openbsd-compat/%.o  : ssh/openssh/openbsd-compat/%.c
	$(call compile, ssh ssh/openssh ssh/openssh/openbsd-compat,  $<, $@ )

build/ssh/openssh/%.o  : ssh/openssh/%.c
	$(call compile, ssh ssh/openssh crypto,  $<, $@ )

build/ssh/%.o  : ssh/%.c
	$(call compile, ssh crypto,  $<, $@ )


build/data/%.o  : data/%.c
	$(call compile, data,  $<, $@ )


build/forms/%.o  : forms/%.c
	$(call compile, forms,  $<, $@ )


build/util/%.o  : util/%.c
	$(call compile, ,  $<, $@ )


build/vt100/%.o  : vt100/%.c
	$(call compile, vt100 vt100/palm,  $<, $@ )


build/%.o  : %.c
	$(call compile, ,  $<, $@ )


clean: 
	make -C arm clean
	rm -rf build
	rm -f *~ *.grc  *.bin *.o *.prc pssh
	rm -f $(addsuffix *~,rsrc/ crypto/openssl/objects/ $(sort $(dir $(SRCS))))
