CC ?= cc
CFLAGS ?= -std=c99 -O2 -Wall -Wextra -Werror
LIBDIR ?= vendor/libmcp
PREFIX ?= /usr/local
CGIDIR ?= $(PREFIX)/lib/cgi-bin

all: memoryd memoryd-http

memoryd: memoryd.c $(LIBDIR)/mcp.h
	$(CC) $(CFLAGS) -I$(LIBDIR) -o $@ memoryd.c -lm

memoryd-http: memoryd-http.c vendor/cgi/cgi.h $(LIBDIR)/mcp.h
	$(CC) $(CFLAGS) -I$(LIBDIR) -Ivendor/cgi -o $@ memoryd-http.c

test: memoryd memoryd-http
	./tests/run.sh
	./tests/test-http.sh

install: memoryd memoryd-http
	mkdir -p $(DESTDIR)$(PREFIX)/bin
	cp memoryd $(DESTDIR)$(PREFIX)/bin/
	install -d $(DESTDIR)$(CGIDIR)
	install -m 0755 memoryd-http $(DESTDIR)$(CGIDIR)/memoryd-http

clean:
	rm -f memoryd memoryd-http

.PHONY: all test install clean
