DEFS       = -DAUTHENTICATION
CFLAGS     = -std=c99 -Wall -Wextra -O2 $(DEFS)
LDFLAGS    =
FUSE_CFLAGS = -D_FILE_OFFSET_BITS=64 -I/usr/local/include
FUSE_LIBS   = -L/usr/local/lib -lfuse3

AMIGA_DEBUG  = -DNDEBUG
# AMIGA_DEBUG  = -DDEBUG -DTDEBUG=3
AMIGA_CC     = vc -c99 -O1 -dontwarn=153,129 $(AMIGA_DEBUG) -D__amigaos__ $(DEFS) -I${VBCC}targets/m68k-amigaos/roadshow_netinclude_h
AMIGA_AS     = vasmm68k_mot -quiet -m68000 -align -phxass -no-opt -x $(AMIGA_DEBUG) -I${VBCC}targets/m68k-amigaos/include_i

help:
	@echo "all ........... builds fitz-serve, fitz-mount, fitz-test"
	@echo "install ....... installs fitz, fitz-serve, fitz-mount to /usr/local/bin"
	@echo "amiga ......... builds fitz, the unified amiga client/server"
	@echo "test-client ... runs the client test suite"
	@echo "test .......... runs the client-server test suite (POSIX+FUSE)"
	@echo "test-remote ... runs the client-server test suite against"
	@echo "                remote server on HOST=$(HOST) PORT=$(PORT)"

all: fitz-serve fitz-mount fitz-test

# unix gets two separate binaries, because if FUSE is not available,
# the server might still be used
fitz-serve: unix-server.c argparse.c argparse.h fitz-protocol.h fitz-common.c fitz-common-unix.c fitz-version.h
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ unix-server.c fitz-common.c fitz-common-unix.c argparse.c -lpthread

fitz-mount: unix-client.c argparse.c argparse.h fitz-protocol.h fitz-common.c fitz-common-unix.c fitz-version.h
	$(CC) $(CFLAGS) $(FUSE_CFLAGS) $(LDFLAGS) -o $@ unix-client.c fitz-common.c fitz-common-unix.c argparse.c $(FUSE_LIBS) -lpthread

fitz-test: fitz-test.c
	$(CC) $(CFLAGS) -o $@ fitz-test.c

fitz-test-amiga: fitz-test.c
	$(AMIGA_CC) +aos68k fitz-test.c -o fitz-test-amiga -lamiga
	
amiga-comparetree: amiga-comparetree.c
	$(AMIGA_CC) +aos68k amiga-comparetree.c -o amiga-comparetree -lamiga

kprintf.o: kprintf.asm
	$(AMIGA_AS) -Fhunk kprintf.asm -o kprintf.o
speck64be.o: speck64be.asm
	$(AMIGA_AS) -Fhunk speck64be.asm -o speck64be.o

# Amiga unified binary: mount and serve subcommands in one executable.
fitz: amiga-main.c amiga-client.c amiga-server.c amiga-util.c kprintf.o speck64be.o fitz-protocol.h fitz-common.c debug.h fitz-version.h
	$(AMIGA_CC) +aos68knostartup ${VBCC}targets/m68k-amigaos/lib/minstart.o amiga-main.c amiga-client.c amiga-server.c amiga-util.c fitz-common.c kprintf.o speck64be.o -o $@
# 	$(AMIGA_CC) +aos68k amiga-main.c amiga-client.c amiga-server.c amiga-util.c fitz-common.c kprintf.o speck64be.o -o $@

amiga: fitz fitz-test-amiga amiga-comparetree

install-amiga: fitz
	copy fitz c:

install: fitz-posix fitz-mount fitz-serve
	install fitz-posix /usr/local/bin/fitz
	install fitz-mount fitz-serve /usr/local/bin/

clean:
	rm -f fitz-serve fitz-mount fitz kprintf.o fitz-test fitz-test-amiga amiga-util.o amiga-comparetree speck64be.o *.o
	
release: fitz
	
release-clean: fitz
	rm -Rf *.o .hg .hgignore .hgtags DESIGN SECURITY TODO GOTCHAS

test: fitz-serve fitz-mount
	@lua5.3 tests/run.lua 2>&1 | tee tests/last_run.log; exit $${PIPESTATUS[0]}

# Run tests against an already-running remote server (e.g. on Amiga).
# Usage: gmake test-remote HOST=192.168.1.x [PORT=17711]
HOST = a3000
PORT = 17712
test-remote: fitz-mount
	@test -n "$(HOST)" || { echo "Usage: make test-remote HOST=x.x.x.x [PORT=17711]"; exit 1; }
	@lua5.3 tests/run.lua --remote "$(HOST):$(PORT)" 2>&1 | tee tests/last_run.log; exit $${PIPESTATUS[0]}

test-client: fitz-test
	./fitz-test test-mount

test-clean:
	@sh tests/clean.sh

showdiff:
	hg extdiff -p kdiff3

.PHONY: all clean amiga test test-remote test-clean test-client help install install-amiga showdiff release release-clean

