# pssh makefile

all: install


.PHONY=bin rcp all install clean build armstuff
.SUFFIXES=

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

# CFLAGS = -g
# LDFLAGS = -g


# Peal
PEAL = ../peal
PEAL_SRCS = util/peal.c
PEAL_FILES = util/peal.c util/peal.h util/elf.h util/elf32.h util/elf_common.h
util/peal.c: $(PEAL)/m68k/peal.c
	cp $(PEAL)/m68k/peal.c util/peal.c
util/peal.h: $(PEAL)/m68k/peal.h
	cp $(PEAL)/m68k/peal.h util/peal.h
util/elf.h: $(PEAL)/m68k/elf.h
	cp $(PEAL)/m68k/elf.h util/elf.h
util/elf32.h: $(PEAL)/m68k/elf32.h
	cp $(PEAL)/m68k/elf32.h util/elf32.h
util/elf_common.h: $(PEAL)/m68k/elf_common.h
	cp $(PEAL)/m68k/elf_common.h util/elf_common.h


# OpenSSL
BN_SRCS = bn/bn_armstubs.c 
AES_SRCS = aes/aes_armstubs.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) \
)

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

# OpenSSH
OPENSSH_SRCS = $(addprefix openssh/, \
	bufaux.c buffer.c cipher.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 PuTTY and custom)
SSH_SRCS = $(addprefix ssh/, \
	connection.c keyfile.c keyimport.c packetizer.c session.c ssh.c transport.c sshzlib.c \
	$(OPENSSH_SRCS) \
)

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

# vt100 (PuTTY)
VT100_SRCS = vt100/vt100.c

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

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

# Everything 68K
SRCS = main.c $(CRYPTO_SRCS) $(FORM_SRCS) $(DATA_SRCS) $(SSH_SRCS) \
	$(UTIL_SRCS) $(VT100_SRCS) $(PEAL_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)))))


install: pssh.prc
	-open pssh.prc

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

pssh: $(PEAL_FILES) $(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

armstuff: 
	make -C 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/openssl/%.o  : crypto/openssl/%.c
	$(call compile, crypto crypto/openssl,  $<, $@ )


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 $(PEAL_FILES)
	rm -f *~ *.grc  *.bin *.o *.prc pssh pssh-sections.ld pssh-sections.s
	rm -f $(addsuffix *~,rsrc/ crypto/openssl/objects/ $(sort $(dir $(SRCS))))
	rm -f $(addsuffix .gdb_history,rsrc/ crypto/openssl/objects/ $(sort $(dir $(SRCS))))
