PLATFORM=win
MAKEFILENAME=Makefile.win
include Makefile
OUTPUT=haxe.exe
EXTENSION=.exe
PACKAGE_SRC_EXTENSION=.zip

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)
CC_PARSER_CMD=($(COMPILER) -pp camlp4o $(ALL_CFLAGS) -c src/syntax/parser.ml 2>tmp.cmi && $(FILTER)) || ($(FILTER) && exit 1)
endif

PACKAGE_FILES=$(OUTPUT) haxelib$(EXTENSION) std _build "$$(cygpath -w "$$(which zlib1.dll)")" "$$(cygpath -w "$$(which libpcre-1.dll)")"

package_win:
	mkdir -p out
	rm -rf $(PACKAGE_FILE_NAME) $(PACKAGE_FILE_NAME).zip temp.zip
	#delete all content which was generated in _build dir except interfaces
	find _build/ -type f ! -name '*.cmi' -delete
	#add ocaml version to the _build dir
	ocaml -version > _build/ocaml.version
	# 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 LICENSE.txt 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
	sed -e 's/@SNAPSHOT_VERSION@/$(HAXE_VERSION_SHORT)-SNAP$(COMMIT_DATE)/g' extra/choco/haxe.nuspec > 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)
	wget -nv http://nekovm.org/media/neko-2.1.0-win.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
	# haxesetup.exe
	haxelib path hxcpp || haxelib install hxcpp
	cd extra; haxelib run hxcpp build-haxesetup.xml
	cp extra/haxesetup.exe $(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
	cd $(INSTALLER_TMP_DIR) && makensis installer.nsi
	7z a -r -tzip $(OUTFILE) $(INSTALLER_TMP_DIR)/*.exe
	dir $(PACKAGE_OUT_DIR)
