# NetHack 3.7 fetchprereq.nmake
#==============================================================================
#
#  The version of the game this Makefile was designed for
NETHACK_VERSION="3.7.0"

# A brief version for use in macros
NHV=$(NETHACK_VERSION:.=)
NHV=$(NHV:"=)

#  The version of Lua we want
LUA_VERSION=5.4.6
CURLLUASRC=https://www.lua.org/ftp/lua-$(LUA_VERSION).tar.gz
CURLLUADST=lua-$(LUA_VERSION).tar.gz

# The version of pdcursesmod that we want
PDCDIST=pdcursesmod
PDCURSES_VERSION = 4.4.0
PDCURL=https://github.com/Bill-Gray/PDCursesMod/archive/refs/tags/v
CURLPDCSRC=$(PDCURL)$(PDCURSES_VERSION).zip
CURLPDCDST=$(PDCDIST)

#
#  relative directories from root of NetHack tree.
#

LIBSDIR=lib               # libraries and external bits
SUBMSDIR=submodules       # NetHack git submodules
ROOTDIR=..\..\..\..       # root of NetHack tree relative to project file
LIBDIR=$(ROOTDIR)\$(LIBSDIR)
SUBMDIR=$(ROOTDIR)\$(SUBMSDIR)


default: fetchall

fetchall: libdir fetch-Lua fetch-pdcurses ..\..\..\..\include\nhlua.h

fetch-lua: fetch-actual-Lua

fetch-Lua: fetch-actual-Lua

fetch-actual-Lua:
	cd $(LIBDIR)
	curl --insecure -R -O $(CURLLUASRC)
	tar zxf lua-$(LUA_VERSION).tar.gz
	if exist lua-$(LUA_VERSION).tar.gz del lua-$(LUA_VERSION).tar.gz
	cd ..\sys\windows\vs\fetchprereq
	@echo Lua has been fetched into $(LIBDIR)\lua-$(LUA_VERSION)

fetch-pdcurses:
	cd $(LIBDIR)
	curl --insecure -L -R $(PDCURL)$(PDCURSES_VERSION).zip -o $(PDCDIST).zip
	if not exist $(PDCDIST)\*.* mkdir $(PDCDIST)
	tar -zxC $(PDCDIST) --strip-components=1 -f $(CURLPDCDST).zip
	if exist $(CURLPDCDST).zip del $(CURLPDCDST).zip
	cd ..\sys\windows\vs\fetchprereq
	@echo $(PDCDIST) has been fetched into $(LIBDIR)\$(PDCDIST)

..\..\..\..\include\nhlua.h:
	@echo  /* nhlua.h - generated by Makefile from fetchprereq.nmake */ > $@
	@echo #include "lua.h" >> $@
	@echo LUA_API int   (lua_error) (lua_State *L) NORETURN; >> $@
	@echo #include "lualib.h" >> $@
	@echo #include "lauxlib.h" >> $@
	@echo /*nhlua.h*/ >> $@

libdir: 
	@if not exist $(LIBDIR)\*.* echo creating directory $(LIB:\=/)
	@if not exist $(LIBDIR)\*.* mkdir $(LIBDIR)

clean:
	@if exist $(LIBDIR)\$(PDCDIST) rmdir /Q $(LIBDIR)\$(PDCDIST) /s
	@if exist $(LIBDIR)\lua-$(LUA_VERSION) rmdir /Q $(LIBDIR)\lua-$(LUA_VERSION) /s
	@if exist ..\..\..\..\include\nhlua.h del /Q ..\..\..\..\include\nhlua.h

rebuild:
	@if exist $(LIBDIR)\$(PDCDIST) echo nothing to do for lib\$(PDCDIST)
	@if exist $(LIBDIR)\lua-$(LUA_VERSION) echo nothing to do for lib\lua-$(LUA_VERSION)
