PLATFORM=win
MAKEFILENAME=Makefile.win
include Makefile
HAXE_OUTPUT=haxe.exe
HAXELIB_OUTPUT=haxelib.exe
PREBUILD_OUTPUT=prebuild.exe
EXTENSION=.exe
PACKAGE_SRC_EXTENSION=.zip
ARCH?=32
DUNE_COMMAND=dune.exe

ifeq ($(ARCH),64)
NEKO_ARCH_STR=64
endif

kill:
	-@taskkill /F /IM haxe.exe

# allow Ocaml/Mingw as well
NATIVE_LIBS += -I "c:/program files/mingw/lib/"

# use make WODI=wodi32 -f Makefile.win to build using WODI 32bit
ifdef WODI
NATIVE_LIBS += -I "/opt/${WODI}/lib"
endif

# use make MSVC=1 -f Makefile.win to build for OCaml/MSVC

ifeq (${MSVC}, 1)
NATIVE_LIBS = shell32.lib libs/extc/extc_stubs.obj libs/extc/process_stubs.obj libs/extc/zlib/zlib.lib libs/objsize/c_objsize.obj
endif

ifeq (${MSVC_OUTPUT}, 1)
FILTER=sed 's/File "\([^"]\+\)", line \([0-9]\+\), \(.*\)/\1(\2): \3/' tmp.cmi
endif

ifeq (${FD_OUTPUT}, 1)
FILTER=sed -e '/File/{ N; s/File "\([^"]\+\)", line \([0-9]\+\), characters \([0-9-]\+\):[\r\n]*\(.*\)/\1:\2: characters \3 : \4/ }' -e 's/_build\/src\//src\//' tmp.cmi
endif

ifdef FILTER
CC_CMD=($(COMPILER) $(ALL_CFLAGS) -c $< 2>tmp.cmi && $(FILTER)) || ($(FILTER) && exit 1)
endif

PACKAGE_FILES=$(HAXE_OUTPUT) $(HAXELIB_OUTPUT) std LICENSE \
	"$$(cygcheck $(CURDIR)/$(HAXE_OUTPUT) | grep zlib1.dll | sed -e 's/^\s*//')" \
	"$$(cygcheck $(CURDIR)/$(HAXE_OUTPUT) | grep libpcre2-8-0.dll | sed -e 's/^\s*//')" \
	"$$(cygcheck $(CURDIR)/$(HAXE_OUTPUT) | grep libmbedcrypto.dll | sed -e 's/^\s*//')" \
	"$$(cygcheck $(CURDIR)/$(HAXE_OUTPUT) | grep libmbedtls.dll | sed -e 's/^\s*//')" \
	"$$(cygcheck $(CURDIR)/$(HAXE_OUTPUT) | grep libmbedx509.dll | sed -e 's/^\s*//')"

# haxelib should depends on haxe, but we don't want to do that...
haxelib_win:
	cd $(CURDIR)/extra/haxelib_src && \
	HAXE_STD_PATH=$$(cygpath -m $(CURDIR)/std) $(CURDIR)/$(HAXE_OUTPUT) client.hxml && \
	nekotools boot run.n
	mv extra/haxelib_src/run$(EXTENSION) $(HAXELIB_OUTPUT)

echo_package_files:
	echo $(PACKAGE_FILES)

package_win:
	mkdir -p out
	rm -rf $(PACKAGE_FILE_NAME) $(PACKAGE_FILE_NAME).zip temp.zip
	# Copy the package contents to $(PACKAGE_FILE_NAME)
	# Using poor man's cp (zip then unzip), because cp in cygwin is quite broken
	mkdir -p $(PACKAGE_FILE_NAME)
	7z a -y -tzip -mx0 temp.zip $(PACKAGE_FILES) > log.txt || type log.txt
	cd extra && 7z a -y -tzip -mx0 ../temp.zip CONTRIB.txt CHANGES.txt > log.txt || type log.txt
	7z x -y temp.zip -o$(PACKAGE_FILE_NAME) > log.txt || type log.txt
	rm temp.zip
	# archive
	7z a -r -tzip out/$(PACKAGE_FILE_NAME)_bin.zip $(PACKAGE_FILE_NAME) > log.txt || type log.txt
	rm -r $(PACKAGE_FILE_NAME)
	rm log.txt extra/log.txt

package_choco:
	mkdir -p OUTPUT
	7z x -y out/$(PACKAGE_FILE_NAME)_bin.zip -oout > log.txt || type log.txt
	mv out/$(PACKAGE_FILE_NAME) out/choco
	cp extra/choco/haxe.nuspec out/choco/haxe.nuspec
	sed -i 's/@SNAPSHOT_VERSION@/$(HAXE_VERSION_SHORT)-SNAP$(COMMIT_DATE)/g' out/choco/haxe.nuspec
	sed -i 's/@NEKO_VERSION@/$(NEKO_VERSION)/g' out/choco/haxe.nuspec
	cd out/choco && choco pack
	mv out/choco/haxe.*.nupkg out
	rm -rf out/choco

$(INSTALLER_TMP_DIR)/neko-win.zip: $(INSTALLER_TMP_DIR)
	curl -L https://github.com/HaxeFoundation/neko/releases/download/$(NEKO_VERSION_TAG)/neko-$(NEKO_VERSION)-win$(NEKO_ARCH_STR).zip -o installer/neko-win.zip

package_installer_win: $(INSTALLER_TMP_DIR)/neko-win.zip package_win
	$(eval OUTFILE := $(PACKAGE_OUT_DIR)/$(PACKAGE_FILE_NAME)_installer.zip)
	rm -rf $(INSTALLER_TMP_DIR)/resources
	# neko
	mkdir $(INSTALLER_TMP_DIR)/resources
	cd $(INSTALLER_TMP_DIR) && 7z x -y neko-win.zip
	rm $(INSTALLER_TMP_DIR)/neko-win.zip
	mv $(INSTALLER_TMP_DIR)/neko* $(INSTALLER_TMP_DIR)/resources/neko
	# haxe
	7z x -y $(PACKAGE_OUT_DIR)/$(PACKAGE_FILE_NAME)_bin.zip -o$(INSTALLER_TMP_DIR)/resources
	mv $(INSTALLER_TMP_DIR)/resources/haxe* $(INSTALLER_TMP_DIR)/resources/haxe
	# WinSetup.hx
	cp extra/WinSetup.hx $(INSTALLER_TMP_DIR)/resources/haxe
	# extra
	cp extra/*.nsi $(INSTALLER_TMP_DIR)
	cp extra/*.nsh $(INSTALLER_TMP_DIR)
	cp -rf extra/images $(INSTALLER_TMP_DIR)
	# nsis
	sed -i "s/%%VERSION%%/$(HAXE_VERSION_SHORT)/g" $(INSTALLER_TMP_DIR)/installer.nsi
	sed -i "s/%%VERSTRING%%/$(HAXE_VERSION)/g" $(INSTALLER_TMP_DIR)/installer.nsi
	sed -i "s/%%VERLONG%%/$(HAXE_VERSION)/g" $(INSTALLER_TMP_DIR)/installer.nsi
	sed -i "s/%%NEKO_VERSION%%/$(NEKO_VERSION)/g" $(INSTALLER_TMP_DIR)/installer.nsi
	cd $(INSTALLER_TMP_DIR) && makensis installer.nsi
	7z a -r -tzip $(OUTFILE) $(INSTALLER_TMP_DIR)/*.exe
	dir $(PACKAGE_OUT_DIR)
