CC=cc
CFLAGS=-std=gnu99 -I/usr/pkg/include -I../deps/ -I../deps/libb64-1.2/include -DENABLE_WWW=1
DEPS = bbs.h
JAMLIB = ../deps/jamlib/jamlib.a
ZMODEM = ../deps/Xmodem/libzmodem.a
LUA = ../deps/lua/liblua.a
B64 = ../deps/libb64-1.2/src/libb64.a
MICROHTTPD=-lmicrohttpd
ODOORS = ../deps/odoors/libs-`uname -s`/libODoors.a
JSMN = ../deps/jsmn/libjsmn.a
CDK = ../deps/cdk-5-20161210/libcdk.a
UUID = ../deps/libuuid/.libs/libuuid.a

all: magicka magimail magiedit ticproc mgpost magichat filecenter dosbox_shim magiftpd reset_pass

${LUA}:
	cd ../deps/lua && $(MAKE) -f Makefile bsd MAKEFLAGS=

${JAMLIB}:
	cd ../deps/jamlib && $(MAKE) -f Makefile.linux MAKEFLAGS=

${ZMODEM}:
	cd ../deps/Xmodem && $(MAKE) MAKEFLAGS=

${B64}: 
	cd ../deps/libb64-1.2 && $(MAKE) MAKEFLAGS=

${ODOORS}:
	cd ../deps/odoors/ && $(MAKE)

$(JSMN):
	cd ../deps/jsmn/ && $(MAKE)

$(CDK):
	cd ../deps/cdk-5.0-20161210/ && export CPPFLAGS=-I/usr/pkg/include && ./configure --with-ncurses
	cd ../deps/cdk-5.0-20161210/ && $(MAKE)

$(UUID):
	cd ../deps/libuuid/ && ./configure
	cd ../deps/libuuid/ && $(MAKE)

OBJ =  ../deps/aha/aha.o ../deps/hashids/hashids.o inih/ini.o bbs.o main.o users.o main_menu.o mail_menu.o doors.o bbs_list.o chat_system.o email.o files.o settings.o lua_glue.o strings.o bluewave.o www.o www_email.o www_msgs.o www_last10.o www_files.o www_blog.o hashmap/hashmap.o menus.o nodelist.o blog.o
%.o: %.c $(DEPS)
	$(CC) -c -o $@ $< $(CFLAGS)

magicka: $(OBJ) ${LUA} ${ZMODEM} ${JAMLIB} ${B64} ${JSMN} ${UUID}
	$(CC) -o ../magicka $^ $(CFLAGS) -R/usr/pkg/lib -L/usr/pkg/lib -lsqlite3 -lutil -lm -lssl -lcrypto -lssh -liconv $(MICROHTTPD)

magiedit: $(ODOORS)
	cd ../utils/magiedit && $(MAKE)

magimail: $(JAMLIB)
	cd ../utils/magimail && $(MAKE) freebsd

ticproc:
	cd ../utils/ticproc && $(MAKE)

mgpost: $(JAMLIB)
	cd ../utils/mgpost && $(MAKE)

magichat: $(JSMN)
	cd ../utils/magichat && $(MAKE)

filecenter: $(CDK)
	cd ../utils/filecenter && $(MAKE)

dosbox_shim:
	cd ../utils/dosbox_shim && $(MAKE)

magiftpd:
	cd ../utils/magiftpd && $(MAKE)

reset_pass:
	cd ../utils/reset_pass && $(MAKE)

.PHONY: clean

clean:
	rm -f $(OBJ) ../magicka
	cd ../deps/lua && $(MAKE) clean
	cd ../deps/jamlib && $(MAKE) -f Makefile.linux clean
	cd ../deps/Xmodem && $(MAKE) clean
	cd ../deps/libb64-1.2 && $(MAKE) clean
	cd ../utils/magimail && $(MAKE) cleanfreebsd
	cd ../utils/magiedit && $(MAKE) clean
	cd ../deps/odoors/ && rm -rf libs-`uname -s` objs-`uname -s` exe-`uname -s`
	cd ../utils/ticproc && $(MAKE) clean
	cd ../utils/mgpost && $(MAKE) clean
	cd ../utils/magichat && $(MAKE) clean
	cd ../deps/cdk-5.0-20161210 && $(MAKE) clean
	cd ../utils/filecenter && $(MAKE) clean
	cd ../utils/magiftpd && $(MAKE) clean
	cd ../utils/reset_pass && $(MAKE) clean
	cd ../utils/dosbox_shim && $(MAKE) clean