# This file is a template makefile used to create the real makefile that
# that is passed to NMAKE to buld the PRC project.  The batch file
# make.bat creates the lists of source files and uses ssed to merge them
# into this file where the ~~~xxx~~~ placeholders are, then runs the
# resultant makefile to build the project.  Thus the bat file and this
# makefile template form a pair of files that do the build in tandem.
# The following directory tree is what the files expect to see:
#
# scripts - contains all of the source scripts
#
# 2das - Contains all of the source 2da files
#
# tlk - Contains the custom tlk file
#
# gfx - Contains all of the graphic images, icons, textures, etc. that
# go in the prc pack.
#
# others - Contains various other files that go in the hak such as
# creature blueprints, item blueprints, etc.
#
# tools - Contains all of the EXE files used by the makefile to do the build.
#
# objs - All of hte compiled script object files are placed here.  If this
# directory does not exist it will be created.
#
# setup - Builds the NSIS Setup.exe install application.  This REQUIRES
# NSIS to be installed on your PC.
#
# There is some outside information that the makefile needs to know, it expects
# this information to be set in variables in the config.make file.  The variables
# it expects to be set are as follows:
#
# NWN_DIR - The folder where you have NWN installed.
#
# PRC_VERSION - The version number of the PRC build, this is only used for the RAR
# file name
#
# PRC_INSTALLVERSION - The version number of the PRC build, this is placed in
# the HIF file and the NSIS install script, so it is shown to the user.
#
# If just run w/o any arguments the makefile will build all haks/erfs/etc. and
# install them in the appropriate spots in your NWN directory.  The following
# additional build targets are supported, they are passed on the command
# line to make.bat, eg. "make rar"
#
# hak - This is the same as specifying no arguments, i.e. the haks/erfs/etc. are
# built and are installed in the NWN directory.
#
# rar - Does what hak does, then builds a rar file containing all of the output
# files.
#

# Include the file with the NWN_DIR definition and version number
!include config.make

# Names of the output files.
# NOTE: For version 2.0 this is going to change and there will be 2 haks,
# the exact names are TBD.
RAR				= $(OUTPATH)\PRC$(PRC_VERSION).rar
ERF				= $(OUTPATH)\prc_consortium.erf
INCLUDEHAK		= $(OUTPATH)\prc_include.hak
HAK				= $(OUTPATH)\prc_consortium.hak
TLK				= $(TLKPATH)\prc_consortium.tlk
HIF				= "$(OUTPATH)\PRC Pack.hif"
#README			= prc_consortium.htm
MISCRESOURCES	= $(OUTPATH)\PRCModuleUpdater.exe 

# Version 2.0 haks
SCRIPTSHAK				= $(OUTPATH)\prc_scripts.hak
SPELLSHAK				= $(OUTPATH)\prc_spells.hak
EPICSPELLSCRIPTSHAK		= $(OUTPATH)\prc_epicspells.hak
2DASHAK					= $(OUTPATH)\prc_2das.hak
MISCHAK					= $(OUTPATH)\prc_misc.hak
TEXTURESHAK				= $(OUTPATH)\prc_textures.hak
CRAFT2DASHAK			= $(OUTPATH)\prc_craft2das.hak
RACEHAK					= $(OUTPATH)\prc_race.hak
PSIONICSHAK				= $(OUTPATH)\prc_psionics.hak
NEWSPELLBOOKHAK				= $(OUTPATH)\prc_newspellbook.hak

# The NSIS script used to build setup.exe
INSTALLSCRIPT	= Setup\install.nsi

# The list of include paths to look for nss include files.  If there are multiple
# folders separate by ';', eg. include1;include2;include3
NSSINCLUDE		= include

# This variable contains all of the files that need to be installed in the
# NWN override directory for the prc pack to work.  If this list is changed
# then the updates should be made here. NOTE: You NEED to put the paths
# to the subdirectories on there as well.
#OVERRIDEFILES	= $(2DAPATH)\classes.2da
OVERRIDEFILES	= 

# If an install path for NSIS is not given, assume "C:\program files\nsis"
!IFNDEF NSISPATH
NSISPATH		= C:\program files\nsis
!ENDIF

# If no pretty install version is defined then just use the PRC_VERSION
!IFNDEF PRC_INSTALLVERSION
PRC_INSTALLVERSION	= PRC_VERSION
!ENDIF

# Define the locations of the various EXE's used by the makefile.  All
# should be in the tools folder, except the script compiler which
# comes with NWN.
RARCOMPILER		= tools\rar
ERFCOMPILER 	= tools\erf
NSSCOMPILER 	= tools\nwnnsscomp
NSISCOMPILER	= "$(NSISPATH)\makensis.exe"
SED				= tools\ssed


# Redefine various paths set by the bat file to give them prettier more
# generic names.
OUTPATH				= CompiledResources
ERFFILESPATH		= $(MAKEERFPATH)
2DAPATH				= $(MAKE2DAPATH)
OBJPATH				= $(MAKEOBJSPATH)
SPELLOBJPATH		= $(MAKESPELLOBJSPATH)
EPICSPELLOBJSPATH	= $(MAKEEPICSPELLOBJSPATH)
SRCPATH				= $(MAKESCRIPTPATH)
SPELLSRCPATH		= $(MAKESPELLSPATH)
EPICSPELLSRCPATH	= $(MAKEEPICSPELLSCRIPTPATH)
TLKPATH				= $(MAKETLKPATH)
CRAFT2DASPATH		= $(MAKECRAFT2DASPATH)
RACE2DASPATH		= $(MAKERACE2DASPATH)
RACESRCPATH			= $(MAKERACESRCPATH)
RACEOBJSPATH		= $(MAKERACEOBJSPATH)
PSIONICSSRCPATH		= $(MAKEPSIONICSSRCPATH)
PSIONICSOBJSPATH	= $(MAKEPSIONICSOBJSPATH)
NEWSPELLBOOKSRCPATH		= $(MAKENEWSPELLBOOKPATH)
NEWSPELLBOOKOBJPATH		= $(MAKENEWSPELLBOOKOBJPATH)
TEMPPATH			= $(MAKETEMPPATH)

# Paths for the various folders in the NWN install directory.
NWN_ERF			= $(NWN_DIR)\erf
NWN_HAK			= $(NWN_DIR)\hak
NWN_TLK			= $(NWN_DIR)\tlk
NWN_OVERRIDE	= $(NWN_DIR)\override

# Arguments for various rules
NSS_FLAGS		= -cgoq -I "$(MAKEDIR)\include"

# suffixes for inference rules.
.SUFFIXES: .nss .ncs .erf .hak .zip .rar

# variables to hold all of the contents of the various directories.

ERFFILES=~~~erffiles~~~

INCLUDE=~~~include~~~

SCRIPTS=~~~scripts~~~

SPELLS=~~~spells~~~

EPICSPELLSCRIPTS=~~~epicspellscripts~~~

OBJS=~~~objs~~~

SPELLOBJS=~~~spellobjs~~~

EPICSPELLOBJS=~~~epicspellobjs~~~

2DAS=~~~2das~~~

GFX=~~~gfx~~~

OTHERS=~~~others~~~

CRAFT2DAS=~~~craft2das~~~

RACE2DAS=~~~race2das~~~

RACESCRIPTS=~~~racescripts~~~

RACEOBJS=~~~raceobjs~~~

PSIONICSSCRIPTS=~~~psionicsscripts~~~

PSIONICSOBJS=~~~psionicsobjs~~~

NEWSPELLBOOKSCRIPTS=~~~newspellbook~~~

NEWSPELLBOOKOBJS=~~~newspellbookobjsobjs~~~

#
# Top level virtual rules that just associate pretty text names with various
# targets
#

# The top rule is the one run by default, we want to build the hak and install.
all: hak

# Rule to build the rar file.
rar: hak $(RAR)


# Rule to build the 'hak' content and install it.
hak110: $(ERF110) $(HAK) install

# Rule to build the 2.0 haks
hak: $(SCRIPTSHAK) $(2DASHAK) $(MISCHAK) $(TEXTURESHAK) $(ERF) \
	$(SPELLSHAK) $(EPICSPELLSCRIPTSHAK) $(RACEHAK) $(PSIONICSHAK) $(NEWSPELLBOOKHAK) \
	$(CRAFT2DASHAK) $(INCLUDEHAK) $(HIF) install 

# Rule to build the NSIS installer.
nsis:
	@echo Setup$(PRC_VERSION).exe
	@$(NSISCOMPILER) /DPRCVERSION=$(PRC_VERSION) \
		/DPRCINSTALLVERSION=$(PRC_INSTALLVERSION) /V1 $(INSTALLSCRIPT)


#################################################################################
# Rules to install content in NWN begin here

# Rule to install the hak content
#install: installcontent installoverrides
install: installcontent 

# Rule that does the real work to install the content (except overrides)
# to the NWN install directory.
installcontent:
	@echo Installing PRC files
	-@copy /y "$(HAK)" "$(NWN_HAK)"
	-@copy /y "$(ERF)" "$(NWN_ERF)"
	-@copy /y "$(INCLUDEHAK)" "$(NWN_HAK)"
	@copy /y "$(TLK)" "$(NWN_TLK)"
	-@copy /y "$(SCRIPTSHAK)" "$(NWN_HAK)"
	-@copy /y "$(SPELLSHAK)" "$(NWN_HAK)"
	-@copy /y "$(2DASHAK)" "$(NWN_HAK)"
	-@copy /y "$(MISCHAK)" "$(NWN_HAK)"
	-@copy /y "$(TEXTURESHAK)" "$(NWN_HAK)"
	-@copy /y "$(CRAFT2DASHAK)" "$(NWN_HAK)"
	-@copy /y "$(EPICSPELLSCRIPTSHAK)" "$(NWN_HAK)"
	-@copy /y "$(RACEHAK)" "$(NWN_HAK)"
	-@copy /y "$(PSIONICSHAK)" "$(NWN_HAK)"
	-@copy /y "$(NEWSPELLBOOKHAK)" "$(NWN_HAK)"
	-@copy /y $(HIF) "$(NWN_HAK)"

# Rule to install all the override files.  This one is a bit tricky, the '!'
# operator tells nmake to run the rule once for each target, and $** will
# expand to each target once, so this in effect copies every file in the
# OVERRIDEFILES variable to NWN_OVERRIDE.
installoverrides: $(OVERRIDEFILES)
	!@copy /y "$**" "$(NWN_OVERRIDE)"

# Rules to install content in NWN end here
#################################################################################


#
# Rules to build the final targets
#

# This rule builds the 2.0 RAR, commented out for now
$(RAR): $(SCRIPTSHAK) $(2DASHAK) $(TEXTURESHAK) $(MISCHAK) $(ERF) \
	$(OVERRIDEFILES) $(TLK) $(MISCRESOURCES) $(CRAFT2DASHAK) \
	$(EPICSPELLSCRIPTSHAK) $(SPELLSHAK) $(RACEHAK) $(INCLUDEHAK) \
	$(HIF) $(PSIONICSHAK) $(NEWSPELLBOOKHAK)
	@echo $(@F)
	-@del "$@"
	@"$(RARCOMPILER)" a -ep -inul $@ $**


# Rule to build the ship RAR.	
#$(RAR): $(HAK) $(ERF110) $(OVERRIDEFILES) $(TLK) $(README) \
#	$(MISCRESOURCES) 
#	@echo $(@F)
#	-@del "$@" 2>nul
#	@"$(RARCOMPILER)" a -ep -inul $@ $**


# Rule to build the ERF file.  Currently script source and object files are
# going here.
#$(ERF110): $(OBJS) $(SCRIPTS) 
#	@echo $(@F)
#	-@del "$@" 2>nul
#	@"$(ERFCOMPILER)" -c $@ $**


# Rule to build the HAK file.
$(HAK): $(2DAS) $(GFX) $(OTHERS) $(CRAFT2DAS) 
	@echo $(@F)
	-@del "$@"
	@"$(ERFCOMPILER)" -c $@ $**


# Rule to build the 2.0 ERF file.  Only areas should be in here, they are
# the only things that do not work in hak files.
$(ERF): $(ERFFILES)
	@echo $(@F)
	-@del "$@"
	@"$(ERFCOMPILER)" -c $@ $**

# Rule to build the include ERF, containing the nss files in the include directory
$(INCLUDEHAK): $(INCLUDE)
	@echo $(@F)
	-@del "$@"
	@"$(ERFCOMPILER)" -c $@ $**

# Rule to build the 2.0 scripts HAK file.
$(SCRIPTSHAK): $(OBJS) $(SCRIPTS)
	@echo $(@F)
	-@del "$@"
	@"$(ERFCOMPILER)" -c $@ $(SCRIPTS)
	@"$(ERFCOMPILER)" -u $@ $(OBJS)
# At some point we may need to use the below method to generate the hak
# it will add the files to the hak one at a time to prevent command line
# too long errors, but it runs orders of magnitude slower.
#	@"$(ERFCOMPILER)" -c $@ howto.txt >nul
#	!@"$(ERFCOMPILER)" -u $@ $** >nul

$(SPELLSHAK): $(SPELLOBJS) $(SPELLS)
	@echo $(@F)
	-@del "$@"
	@"$(ERFCOMPILER)" -c $@ $(SPELLS)
	@"$(ERFCOMPILER)" -u $@ $(SPELLOBJS)
# At some point we may need to use the below method to generate the hak
# it will add the files to the hak one at a time to prevent command line
# too long errors, but it runs orders of magnitude slower.
#	@"$(ERFCOMPILER)" -c $@ howto.txt >nul
#	!@"$(ERFCOMPILER)" -u $@ $** >nul

# Rule to build the 2.0 scripts HAK file.
$(EPICSPELLSCRIPTSHAK): $(EPICSPELLOBJS) $(EPICSPELLSCRIPTS)
	@echo $(@F)
	-@del "$@"
	@"$(ERFCOMPILER)" -c $@ $(EPICSPELLSCRIPTS)
	@"$(ERFCOMPILER)" -u $@ $(EPICSPELLOBJS)
# At some point we may need to use the below method to generate the hak
# it will add the files to the hak one at a time to prevent command line
# too long errors, but it runs orders of magnitude slower.
#	@"$(ERFCOMPILER)" -c $@ howto.txt >nul
#	!@"$(ERFCOMPILER)" -u $@ $** >nul


# Rule to build the 2.0 scripts HAK file.
$(RACEHAK): $(RACE2DAS) $(RACEOBJS) $(RACESCRIPTS) 
	@echo $(@F)
	-@del "$@"
	@"$(ERFCOMPILER)" -c $@ $(RACE2DAS)
	@"$(ERFCOMPILER)" -u $@ $(RACESCRIPTS)
	@"$(ERFCOMPILER)" -u $@ $(RACEOBJS)
# At some point we may need to use the below method to generate the hak
# it will add the files to the hak one at a time to prevent command line
# too long errors, but it runs orders of magnitude slower.
#	@"$(ERFCOMPILER)" -c $@ howto.txt >nul
#	!@"$(ERFCOMPILER)" -u $@ $** >nul

# Rule to build the 2.0 scripts HAK file.
$(PSIONICSHAK): $(PSIONICSSCRIPTS) $(PSIONICSOBJS)
	@echo $(@F)
	-@del "$@"
	@"$(ERFCOMPILER)" -c $@ $(PSIONICSSCRIPTS)
	@"$(ERFCOMPILER)" -u $@ $(PSIONICSOBJS)
# At some point we may need to use the below method to generate the hak
# it will add the files to the hak one at a time to prevent command line
# too long errors, but it runs orders of magnitude slower.
#	@"$(ERFCOMPILER)" -c $@ howto.txt >nul
#	!@"$(ERFCOMPILER)" -u $@ $** >nul

# Rule to build the 2.0 scripts HAK file.
$(NEWSPELLBOOKHAK): $(NEWSPELLBOOKSCRIPTS) $(NEWSPELLBOOKOBJS)
	@echo $(@F)
	-@del "$@"
	@"$(ERFCOMPILER)" -c $@ $(NEWSPELLBOOKSCRIPTS)
	@"$(ERFCOMPILER)" -u $@ $(NEWSPELLBOOKOBJS)
# At some point we may need to use the below method to generate the hak
# it will add the files to the hak one at a time to prevent command line
# too long errors, but it runs orders of magnitude slower.
#	@"$(ERFCOMPILER)" -c $@ howto.txt >nul
#	!@"$(ERFCOMPILER)" -u $@ $** >nul

# Rule to build the 2.0 2das HAK file.
$(2DASHAK): $(2DAS)
	@echo $(@F)
	-@del "$@"
	@"$(ERFCOMPILER)" -c $@ $**


# Rule to build the 2.0 textures HAK file.
$(TEXTURESHAK): $(GFX)
	@echo $(@F)
	-@del "$@"
	@"$(ERFCOMPILER)" -c $@ $**


# Rule to build the 2.0 misc HAK file.
$(MISCHAK): $(OTHERS) 
	@echo $(@F)
	-@del "$@"
	@"$(ERFCOMPILER)" -c $@ $**


# Rule to build the 2.0 craft HAK file.
$(CRAFT2DASHAK): $(CRAFT2DAS) 
	@echo $(@F)
	-@del "$@"
	@"$(ERFCOMPILER)" -c $@ $**

# Rule to build the HIF file from it's template.
$(HIF): PRCPackTemplate.hif config.make
	@echo $(@F)
	@-del $@
	@type PRCPackTemplate.hif | $(SED) -R "s/XXXversionXXX/$(PRC_INSTALLVERSION)/" >$@
	
#
# Rule to add makefile dependencies.  This will run makedep to build the include
# file dependencies for all source scripts files that get built to object files
# (i.e. it ignores include scripts files).
#
depends:
	@echo Updating dependencies
	@tools\makedep -a -w -n -l -s$(SRCPATH) -i$(NSSINCLUDE) -o$(MAKEFILE) $(OBJS)
	@tools\makedep -a -w -n -l -s$(SPELLSRCPATH) -i$(NSSINCLUDE) -o$(MAKEFILE) $(SPELLOBJS)
	@tools\makedep -a -w -n -l -s$(EPICSPELLSRCPATH) -i$(NSSINCLUDE) -o$(MAKEFILE) $(EPICSPELLOBJS)
	@tools\makedep -a -w -n -l -s$(RACESRCPATH) -i$(NSSINCLUDE) -o$(MAKEFILE) $(RACEOBJS)
	@tools\makedep -a -w -n -l -s$(PSIONICSSRCPATH) -i$(NSSINCLUDE) -o$(MAKEFILE) $(PSIONICSOBJS)
	@tools\makedep -a -w -n -l -s$(NEWSPELLBOOKSRCPATH) -i$(NSSINCLUDE) -o$(MAKEFILE) $(NEWSPELLBOOKOBJS)


# The code below will compile scripts by copying them to the TEMPPATH and doing the
# compile there.  This can be used along with an include directory to get all scripts
# to use the shared include files.  These lines need to replace the 2 compile lines
# (the lines after the echo) in each of the inference rules below.
#	@copy /y "$(MAKEDIR)\$<" "$(MAKEDIR)\$(TEMPPATH)"
#	@"$(NSSCOMPILER)" "$(MAKEDIR)\$(TEMPPATH)\$(<F)" "$(MAKEDIR)\$(@D)" >nul
#	-@del "$(MAKEDIR)\$(TEMPPATH)\$(<F)" 2>nul
# 	-@del "$(MAKEDIR)\$(@R).ndb" 2>nul

# Inference rule to compile scripts.
{$(SRCPATH)\}.nss{$(OBJPATH)\}.ncs:
	@echo $(<F)
	@"$(NSSCOMPILER)" $(NSS_FLAGS) "$(MAKEDIR)\$<" "$(MAKEDIR)\$@"

{$(SPELLSRCPATH)\}.nss{$(SPELLOBJPATH)\}.ncs:
	@echo $(<F)
	@"$(NSSCOMPILER)" $(NSS_FLAGS) "$(MAKEDIR)\$<" "$(MAKEDIR)\$@"

{$(EPICSPELLSRCPATH)\}.nss{$(EPICSPELLOBJSPATH)\}.ncs:
	@echo $(<F)
	@"$(NSSCOMPILER)" $(NSS_FLAGS) "$(MAKEDIR)\$<" "$(MAKEDIR)\$@"

{$(RACESRCPATH)\}.nss{$(RACEOBJSPATH)\}.ncs:
	@echo $(<F)
	@"$(NSSCOMPILER)" $(NSS_FLAGS) "$(MAKEDIR)\$<" "$(MAKEDIR)\$@"

{$(PSIONICSSRCPATH)\}.nss{$(PSIONICSOBJSPATH)\}.ncs:
	@echo $(<F)
	@"$(NSSCOMPILER)" $(NSS_FLAGS) "$(MAKEDIR)\$<" "$(MAKEDIR)\$@"
	
{$(NEWSPELLBOOKSRCPATH)\}.nss{$(NEWSPELLBOOKOBJSPATH)\}.ncs:
	@echo $(<F)
	@"$(NSSCOMPILER)" $(NSS_FLAGS) "$(MAKEDIR)\$<" "$(MAKEDIR)\$@"	

#################################################################################
# Depends updates will go here. DO NOT ADD ANYTING BELOW THIS LINE!