version = dev
dl = git
dbg-flags = $(if $(dbg),-g)

# for nix
prefix = ${out}

images = static/default-avatar.webp static/query.webp static/heart.webp static/retweet.webp static/reply.webp static/file.webp static/follow.webp
#$(addsuffix .webp, $(basename $(wildcard static/*.svg)))
styles = $(addsuffix .css, $(basename $(wildcard static/*.scss)))

# .PHONY: all
# all: parsav parsavd

parsav parsavd: parsav.t config.lua pkgdata.lua $(images) $(styles)
	terra $(dbg-flags) $<
parsav.o parsavd.o: parsav.t config.lua pkgdata.lua $(images) $(styles)
	env parsav_link=no terra $(dbg-flags) $<
parsav.ll parsavd.ll: parsav.t config.lua pkgdata.lua $(images) $(styles)
	env parsav_emit_type=ll parsav_link=no terra $(dbg-flags) $<
parsav.s parsavd.ss: parsav.ll
	llc --march=$(target) $<

static/%.svg.clean: static/%.svg
	svgcleaner $< $@
static/%.webp: static/%.png
	cwebp -q 90 $< -o $@
static/%.png: static/%.svg
	inkscape -f $< -C -d 180 -e $@
static/%.css: static/%.scss
	sassc -t compressed $< $@

clean:
	rm parsav parsav.o $(images) $(styles)

install: parsav parsavd
	mkdir $(prefix)/bin
	cp $^ $(prefix)/bin/

dep: dep.mbedtls dep.mongoose dep.mjson
dep.mbedtls: lib/mbedtls/library/libmbedtls.a \
	lib/mbedtls/library/libmbedcrypto.a \
	lib/mbedtls/library/libmbedx509.a
dep.mongoose: lib/mongoose/libmongoose.a
dep.mjson: lib/mjson/libmjson.a

static-libs = lib/mjson/libmjson.a \
		      lib/mongoose/libmongoose.a \
		      lib/mbedtls/library/libmbedx509.a \
		      lib/mbedtls/library/libmbedcrypto.a \
		      lib/mbedtls/library/libmbedtls.a

dist-kind ?= o
parsav-dist = parsav-$(dist-kind)dist-$(version)
$(parsav-dist).txz: parsav.$(dist-kind) parsavd.$(dist-kind) dist/readme.md dist/GNUmakefile dist/BSDmakefile dist/makefile dist/default.nix $(static-libs)
	mkdir -p $(parsav-dist)
	cp -u $^ $(parsav-dist)/
	tar cf   $(parsav-dist){.tar,}
	xz -c9e  $(parsav-dist).tar > $@
	rm -r    $(parsav-dist){.tar,}

lib:
	mkdir $@

lib%.a %.a: %.o
	ar rcs $@ $<
	ranlib $@ # bsd-ism

# generate a shim static library so mongoose cooperates
# with the build apparatus. note that parsav is designed
# to be fronted by a real web server like nginx if SSL
# is to be used, so we don't turn on SSL in mongoose
lib/mongoose/mongoose.o: lib/mongoose/mongoose.c lib/mongoose/mongoose.h lib/mongoose
	$(CC) -c $< -o lib/mongoose/mongoose.o \
		-DMG_ENABLE_THREADS=1 \
		-DMG_ENABLE_IPV6=1 \
		-DMG_ENABLE_HTTP_WEBDAV=1 \
		-DMG_ENABLE_HTTP_WEBSOCKET=0

lib/mjson/mjson.o: lib/mjson/src/mjson.c lib/mjson/src/mjson.h lib/mjson
	$(CC) -c $< -o lib/mjson/mjson.o \
		-DMJSON_MAX_DEPTH=16 \
		-DMJSON_ENABLE_BASE64=0 \
		-DMJSON_ENABLE_NEXT=1 \
		-DMJSON_ENABLE_RPC=0

lib/mbedtls/library/%.a: lib/mbedtls 
	$(MAKE) -C lib/mbedtls/library $*.a

ifeq ($(dl), git)
clone = git clone --depth 1 # save time
ensurelib = mkdir -p lib && cd lib
lib/mongoose: 
	$(ensurelib) && $(clone) https://github.com/cesanta/mongoose.git
lib/mjson: 
	$(ensurelib) && $(clone) https://github.com/cesanta/mjson.git
lib/mbedtls: 
	$(ensurelib) && $(clone) https://github.com/ARMmbed/mbedtls.git
else
lib/%: lib/%.tar.gz
	$(ensurelib) && tar zxf $*.tar.gz
	mv lib/$$(tar tf $< | head -n1) $@

ifeq ($(dl), wget)
    dlfile = mkdir -p $(dir $@) && wget "$1" -O "$@"
endif

ifeq ($(dl), curl)
    dlfile = mkdir -p $(dir $@) && curl "$1" -o "$@"
endif

lib/mongoose.tar.gz:
	$(call dlfile,https://api.github.com/repos/cesanta/mongoose/tarball/master)
lib/mjson.tar.gz:
	$(call dlfile,https://api.github.com/repos/cesanta/mjson/tarball/master)
lib/mbedtls.tar.gz:
	$(call dlfile,https://api.github.com/repos/ARMmbed/mbedtls/tarball/master)
endif
