# CC = cc

SRCCOMMON =\
src/global-state.c src/data-structure.c src/hashing.c src/file-loading.c \
3rdparty/stdextensions/src/ds/stringutils.c 3rdparty/stdextensions/src/fileutils.c 3rdparty/stdextensions/src/util/logging.c \
3rdparty/vec/src/vec.c 3rdparty/threadpool2/src/threadpool.c
# 3rdparty/vec/src/vec.c 3rdparty/C-Thread-Pool/thpool.c
# 3rdparty/vec/src/vec.c 3rdparty/stdextensions/src/ds/threadpool.c


SRCGUI = src/picunic-gui.c src/gui-loop.c src/raylib-tools.c
SRCCLI =

NAMEGUI = picunic-gui
NAMECLI = picunic-cli
PREFIX = /usr/local

INCLUDEPATHS =-Iinclude -I3rdparty -I3rdparty/raylib/src -I3rdparty/raygui/src -I3rdparty/stdextensions/include \
-I3rdparty/vec/src -I3rdparty/threadpool2
# -I3rdparty/vec/src -I3rdparty/C-Thread-Pool

CFLAGSGUI = -Wall -Wextra -Wpedantic -Wno-unknown-pragmas --std=c99 -D_DEFAULT_SOURCE ${INCLUDEPATHS}

ifeq ($(OS),Windows_NT)
	LDFLAGSGUI = -L3rdparty/raylib/src -lraylib -lm -lpthread -lwinmm -lgdi32 -ldxguid -lregex -ltre -lintl -liconv -static
else
	LDFLAGSGUI = -L3rdparty/raylib -lraylib -lGL -lm -lpthread -ldl -lrt -lX11
endif

CFLAGSCLI =
LDFLAGSCLI =

all: prepare-debug ${NAMEGUI} threadpool2r
release: clean prepare-release ${NAMEGUI} threadpool2r

run-${NAMEGUI}: prepare-debug ${NAMEGUI}
	bin/${NAMEGUI}

prepare-debug:
	$(eval CFLAGSGUI += -O2 -g -ggdb3 -DSEx_LOGGING)
	@echo "Debug version"

prepare-release:
	$(eval CFLAGSGUI += -O3 -s)
	@echo "Release version"

raylib:
	git apply --directory=3rdparty/raylib patches/0001-config-changes.patch
	$(MAKE) -C ./3rdparty/raylib/src
	git apply -R --directory=3rdparty/raylib patches/0001-config-changes.patch

threadpool2:
	git apply --directory=3rdparty/threadpool2 patches/0002-overrule-queue-size.patch

threadpool2r:
	git apply -R --directory=3rdparty/threadpool2 patches/0002-overrule-queue-size.patch

optionsgui:
	@echo ""
	@echo "========================================="
	@echo "Opts:"
	@echo "CFLAGS   = ${CFLAGSGUI}"
	@echo "LDFLAGS  = ${LDFLAGSGUI}"
	@echo "CC       = ${CC}"
	@echo "========================================="
	@echo ""

optionscli:
	@echo ""
	@echo "========================================="
	@echo "Opts:"
	@echo "CFLAGS   = ${CFLAGSCLI}"
	@echo "LDFLAGS  = ${LDFLAGSCLI}"
	@echo "CC       = ${CC}"
	@echo "========================================="
	@echo ""

${NAMEGUI}: raylib optionsgui threadpool2
	mkdir -p bin
	${CC} ${SRCGUI} ${SRCCOMMON} ${CFLAGSGUI} ${LDFLAGSGUI} -o bin/${NAMEGUI}
#	bin/${NAMEGUI}

${NAMECLI}: optionscli
	mkdir -p bin
	${CC} {SRCCOMMON} ${SRCCLI} ${CFLAGSCLI} ${LDFLAGSCLI} -o bin/${NAMECLI}

clean:
	rm -rf bin
	rm -rf obj

install:
	cp -i bin/${NAMEGUI} ${PREFIX}/bin/${NAMEGUI}

uninstall:
	rm -i ${PREFIX}/bin/${NAMEGUI}

.PHONY: all install uninstall clean
