PREFIX ?= ~/.local
BINDIR ?= ${PREFIX}/bin
MANDIR ?= ${PREFIX}/man

CFLAGS += -std=c11 -Wall -Wextra -Wpedantic -Wno-overlength-strings
LDADD.kcgi = -lkcgi
LDADD.kcgi-html = -lkcgihtml
LDADD.sqlite3 = -lsqlite3

LITTERBOX = litterbox
TEST_DB = litterbox.sqlite

-include config.mk

LDLIBS = ${LDADD.kcgi} ${LDADD.kcgi-html} ${LDADD.sqlite3}

OBJS += contexts.o
OBJS += css.o
OBJS += events.o
OBJS += html.o
OBJS += networks.o
OBJS += search.o
OBJS += server.o

dev: tags all test

all: scooper

scooper: ${OBJS}
	${CC} ${STATIC} ${LDFLAGS} ${OBJS} ${LDLIBS} -o $@

${OBJS}: server.h

css.c: css.sh default.css color.css
	sh css.sh default.css color.css > $@

test: .test

.test: scooper
	${LITTERBOX} -i -d ${TEST_DB}
	./scooper -c ${TEST_DB}
	rm ${TEST_DB} ${TEST_DB}-*
	touch $@

tags: *.[ch]
	ctags -w *.[ch]

clean:
	rm -f scooper ${OBJS} css.c .test tags

install: scooper scooper.1
	install -d ${DESTDIR}${BINDIR} ${DESTDIR}${MANDIR}/man1
	install scooper ${DESTDIR}${BINDIR}
	install -m 644 scooper.1 ${DESTDIR}${MANDIR}/man1

uninstall:
	rm -f ${DESTDIR}${BINDIR}/scooper ${DESTDIR}${MANDIR}/man1/scooper.1