CC = $(shell which gcc)
PANDOC = $(shell which pandoc)
OBJDUMP = $(shell which objdump)
OBJCOPY = $(shell which objcopy)
READELF = $(shell which readelf)

OUTPUT = libdzonerzy.so

SRC_INC_DIR = src/include
CFLAGS = -Wno-error=unused-parameter -Wno-error=unused-const-variable -std=c99 -O0 -shared -fPIC -I$(SRC_INC_DIR) -Wno-int-conversion -Wno-div-by-zero
LDFLAGS = 

EMBED_TOOL = tools/bin/embed
EMBED_ARGS = -f=h 

CONVERT_TOOL = $(shell which convert)
CONVERT_ARGS = -define webp
:lossless=false -define webp:alpha-compression=1 -define webp:alpha-quality=70 -define webp:emulate-jpeg-size=true -o

DIST = dist
SRC_DIR = src
RAW_DIST = dist/raw
SITE_DIST = dist/site

AWK_UTIL = awk '/^\s*\[[0-9]+\]\s+([^\.].+)/ {print $$2}'
AWK_UTIL_NOCRLF = awk '/^\s*\[[0-9]+\]\s+([^\.].+)/ {printf $$2" "}'

PANDOC_ARGS = --standalone --table-of-contents --section-divs --email-obfuscation=references --css="/main.css" --include-after-body=$(RAW_DIST)/footer.html --include-after-body=$(RAW_DIST)/scripts.html

libdzonerzy:
	@echo  "/* DO NOT EDIT THIS FILE - it is machine generated */" > $(SRC_DIR)/res.h
	@mkdir -p $(DIST)
	@mkdir -p $(RAW_DIST)
	@mkdir -p $(SITE_DIST)
	@$(foreach file, $(wildcard res/*.jpg res/*.gif res/*.png), $(CONVERT_TOOL) $(file) $(CONVERT_ARGS) $(file).webp;)
	@$(foreach file, $(wildcard res/*.jpg res/*.gif res/*.png), $(EMBED_TOOL) -i $(file).webp -o $(SRC_INC_DIR)/$(notdir $(file).webp).h $(EMBED_ARGS);)
	@$(foreach file, $(wildcard res/advisory/*.txt), $(EMBED_TOOL) -i $(file) -t=advisory -o $(SRC_INC_DIR)/$(notdir $(file)).h $(EMBED_ARGS);)
	@rm -f res/*.webp
	@ls $(SRC_INC_DIR)/*.h | xargs -n1 basename | sed 's/^/#include </' | sed 's/$$/>/' >> $(SRC_DIR)/res.h
	$(CC) $(CFLAGS) $(LDFLAGS) -o  $(DIST)/$(OUTPUT) $(SRC_DIR)/libdzonerzy.so.c $(SRC_DIR)/challenge.o

libdzonerzy-dump:
	$(READELF) -S -W $(DIST)/$(OUTPUT)  | $(AWK_UTIL) | xargs dirname {} | sort | uniq | xargs -I{} mkdir -p $(RAW_DIST)/{}
	@$(foreach fn, $(shell $(READELF) -S -W $(DIST)/$(OUTPUT)  | $(AWK_UTIL_NOCRLF)), $(OBJCOPY) $(DIST)/$(OUTPUT) --dump-section $(fn)=/dev/stdout | cat | head -c -1 > $(RAW_DIST)/$(fn);)

libdzonerzy-gen: libdzonerzy-dump
	@mkdir -p $(SITE_DIST)/assets/
	@mkdir -p $(SITE_DIST)/articles/
	@mkdir -p $(SITE_DIST)/advisory/
	@$(foreach page, $(wildcard $(RAW_DIST)/pages/*), $(PANDOC) $(PANDOC_ARGS) --from=markdown --to=html --output=$(shell echo $(SITE_DIST)/$(notdir $(page))  | sed 's/\.md/\.html/') $(page);)
	@$(foreach article, $(wildcard $(RAW_DIST)/articles/*), $(PANDOC) $(PANDOC_ARGS) --from=markdown --to=html --output=$(shell echo $(SITE_DIST)/articles/$(notdir $(article))  | sed 's/\.md/\.html/') $(article);)
	@$(foreach advisory, $(wildcard $(RAW_DIST)/advisory/*), cp  $(advisory) $(SITE_DIST)/advisory/$(notdir $(advisory));)
	@cp $(RAW_DIST)/main.css $(SITE_DIST)/main.css
	@cp $(RAW_DIST)/main.js $(SITE_DIST)/main.js
	@cp $(RAW_DIST)/sitemap.xml $(SITE_DIST)/sitemap.xml
	@cp -r $(RAW_DIST)/embed/* $(SITE_DIST)/assets/
	@cp $(DIST)/libdzonerzy.so $(SITE_DIST)/libdzonerzy.so
	@chmod -R 755 $(SITE_DIST)/*

libdzonerzy-runlocal: libdzonerzy libdzonerzy-gen
	@cd $(SITE_DIST) && python3 -m http.server 8080

clean-libdzonerzy:
	@echo  "/* DO NOT EDIT THIS FILE - it is machine generated */" > $(SRC_DIR)/res.h
	rm -rf $(DIST)
	rm -f $(SRC_INC_DIR)/*.h