VERSION=0.0.1

HAXE=/usr/bin/haxe
SRC=src/Hello.hx

JSLIBS=-L heaps
LIBS=$(JSLIBS) -L hlsdl -L hlopenal

CC=clang
CFLAGS=-O2 -I. -std=c11
CLIBS=-lm -luv -lhl

all: run
all-build: build build-native

build: out/hello.hl out/res
	cp /usr/local/lib/*.hdll out/
	touch out/.stamp

build-native: build build/stamp out/linux/res out/linux/hello-linux64-native
	cp linux-libs/* out/linux

build-windows: build/stamp build/hello.c out/windows/res
	cp windows-libs/* out/windows/
	echo "Now build the exe in windows using the script."

build-web: build out/hello.js
build-web-dist: out/hello-demo.zip

build/stamp: src/Hello.hx
	mkdir -p build
	grep 'final APP_VERSION' src/Hello.hx | tr -d '";' |cut -d'=' -f2 | tr -d ' ' > build/stamp

out/hello-demo.zip: build-web out/res
	(cd out; rm hello-demo.zip; zip -r hello-demo.zip index.html res/ hello.js)

out/res:
	rm -fr out/res/
	mkdir -p out/res/
	cp -R res/ out/

out/linux/res: out/res
	rm -fr out/linux/res
	mkdir -p out/linux
	cp -R out/res out/linux/res

out/windows/res: out/res
	rm -fr out/windows/res
	mkdir -p out/windows
	cp -R out/res out/windows/res

build/hello.c: build/stamp $(SRC)
	$(HAXE) -D no-traces -cp src $(LIBS) --hl build/hello.c --main Hello

out/hello.hl: $(SRC)
	$(HAXE) -cp src $(LIBS) --hl $@ --main Hello --debug

build/hello.o: build/hello.c
	(cd build; $(CC) $(CFLAGS) -c -o hello.o hello.c)

# fixme move to a docker build image so we can control the GLIBC version. and we can target a bunch of specific linuces
out/linux/hello-linux64-native: out/linux/res build/hello.o
	cp linux-libs/* build/
	(cd build; $(CC) $(CFLAGS) hello.o $(CLIBS) *.hdll -o hello)
	cp build/hello out/linux/hello-linux64-native
	cp linux-manifest/* out/linux
	cp linux-libs/* out/linux

out/index.html: index.html
	cp index.html out/

out/hello.js: out/res out/index.html
	$(HAXE) -cp src $(JSLIBS) -js $@ -main Hello

package: out/linux/hello-linux64-native out/windows/hello.exe
	cp -R windows-manifest/* out/windows
	cp -R linux-manifest/* out/linux
	mkdir -p dist
	rm -f dist/hello-linux-$(VERSION).zip
	rm -f dist/hello-windows-$(VERSION).zip
	(cd out/linux; zip -r ../../dist/hello-linux-$(VERSION).zip .)
	(cd out/windows; zip -r ../../dist/hello-windows-$(VERSION).zip .)

clean:
	rm -fr out/
	rm -f src/*~
	rm -f *~
	rm -fr build/

reqs:
	haxelib install heaps
	haxelib install hashlink

run: out/hello.hl out/res
	(cd out; hl hello.hl)

.PHONY: build clean reqs run build-native build-web-dist build-web package
