############################################################################
# Makefile for WeiDU
############################################################################
# Requires GNU Make. Raise your hand if you can't find gnu make somewhere?
# Thought so. 
# 
# Configuration (uncomment exactly one)
# (0) X86 / Windows / Cygwin, using MINGW as the C compiler
#export ARCHOS    = x86_WIN32
#export OCAMLDIR  = C:\\ocaml-3.08-mingw\\bin
#export OCAMLLIB  = C:\\ocaml-3.08-mingw\\lib
#export CAMLP4LIB = C:\\ocaml-3.08-mingw\\lib\\camlp4
#export OBJEXT    = o
#export ARCH_FILE = arch_mingw
#export ARCH_C_FILES = createprocess_win32
#PROJECT_CMODULES   = reg
#PROJECT2_CMODULES  = reg
export GLOB      = glob_win32
# Configuration (uncomment exactly one)
# (1) X86 / Windows / Cygwin, using MSVC as the C compiler
export ARCHOS    = x86_WIN32
export OCAMLDIR  = C:\\ocaml\\bin
export OCAMLLIB  = C:\\ocaml\\lib
export CAMLP4LIB = C:\\ocaml\\lib\\camlp4
export HAVE_MSVC = 1
export OBJEXT    = obj
export ARCH_FILE = arch_win32
export ARCH_C_FILES = createprocess_win32
export GLOB      = glob_win32
#export ITEMLIST = 1
#
# (2) X86 / Linux, using GCC as the C Compiler
#export ARCHOS    = x86_LINUX
#export OCAMLDIR  = /usr/local/bin
#export OCAMLLIB  = /usr/local/lib/ocaml
#export CAMLP4LIB = /usr/local/lib/ocaml/camlp4
#export OBJEXT    = o
#export ARCH_FILE = arch_unix
#export GLOB      = glob_unix
#
# (3) Mac OS X, using GCC as the C compiler
#export ARCHOS    = x86_LINUX
#export OCAMLDIR  = /usr/local/bin
#export OCAMLLIB  = /usr/local/lib/ocaml
#export CAMLP4LIB = /usr/local/lib/ocaml/camlp4
#export OBJEXT    = o
#export ARCH_FILE = arch_osx
#export GLOB      = glob_unix
#
# (4) X86 / CYGWIN, using GCC as the C Compiler
#export ARCHOS    = x86_CYGWIN
#export OCAMLDIR  = /usr/local/bin
#export OCAMLLIB  = /usr/local/lib/ocaml
#export CAMLP4LIB = /usr/local/lib/ocaml/camlp4
#export OBJEXT    = o
#export ARCH_FILE = arch_cygwin
#PROJECT_CMODULES   = reg
#PROJECT2_CMODULES   = reg
#export GLOB      = glob_unix
#
# (5) X86 / Windows / Cygwin, using MinGW as the C compiler
#export ARCHOS    = x86_WIN32
#export OCAMLDIR  = C:\\ocaml-3.07-mingw\\bin
#export OCAMLLIB  = C:\\ocaml-3.07-mingw\\lib
#export CAMLP4LIB = C:\\ocaml-3.07-mingw\\lib\\camlp4
#export HAVE_MINGW = 1
#export OBJEXT    = o
#export ARCH_FILE = arch_win32
#export ARCH_C_FILES = createprocess_win32
#export GLOB      = glob_win32
#export ITEMLIST = 1
#
############################################################################
# You should not need to change anything else in this file. 
############################################################################

# Just a target to be used by default
.PHONY: weidu all

all : weidu 
# "make weimorph" if you want iwg2

bg1tutu_exe : weimorph bg1tutu_restore

####
#### OCAML stuff
####
# Debugging. Set ECHO= to debug this Makefile 
#ECHO := @

RELEASE    := 1
NATIVECAML := 1
UNSAFE     := 1

# Put here all the byproducts of make
OBJDIR      := obj/$(ARCHOS)
DEPENDDIR   := obj/.depend

# First stuff that makes the executable

WEIDU_BASE_MODULES  := arg stats arch version parsing util xor var \
	eff_table key biff tlk load \
        ids idslexer idslexer2 idsparser \
        bcs bcslexer bcsparser \
        dlg dc \
        baflexer bafparser \
        diff tp dlexer dparser \
        automate kit
ifdef ITEMLIST
WEIDU_BASE_MODULES  += pretty itemlist
endif
WEIDU_MODULES  = $(WEIDU_BASE_MODULES) main 

MODULES        = $(WEIDU_MODULES) iwgconf iwgrule iwglexer iwgparser iwg2
SOURCEDIRS     := src zlib glob xdiff 
MLLS           := dlexer.mll idslexer.mll idslexer2.mll bcslexer.mll baflexer.mll iwglexer.mll
MLYS           := dparser.mly idsparser.mly bcsparser.mly bafparser.mly iwgparser.mly

src/arch.ml : src/$(ARCH_FILE).ml
	cp src/$(ARCH_FILE).ml src/arch.ml



# Include now the common set of rules for OCAML
include Makefile.msvc
include Makefile.ocaml

CAMLFLAGS      += -I zlib -I xdiff 

   # Now the rule to make WeiDU

PROJECT_EXECUTABLE = $(OBJDIR)/weidu$(EXE)
PROJECT_MODULES    = $(WEIDU_MODULES)
ifdef HAVE_MSVC
PROJECT_CMODULES   = reg
PROJECT_CLIBS      = advapi32.lib
endif
ifdef HAVE_MINGW
PROJECT_CMODULES   = reg
PROJECT_CLIBS      = -ladvapi32
endif
#PROJECT_CMODULES   += eff_strings
PROJECT_CMODULES   += zlib adler32 infblock inflate uncompr infcodes inftrees zutil inffast infutil $(GLOB) xdiff
PROJECT_CMODULES   += xemit xpatchi xutils xdiffi xprepare 
PROJECT_CMODULES   += $(ARCH_C_FILES)

PROJECT_LIBS       = unix str 
.PHONY: weidu
weidu: $(PROJECT_EXECUTABLE)
$(PROJECT_EXECUTABLE) : $(PROJECT_MODULES:%=$(OBJDIR)/%.$(CMO)) \
                        $(PROJECT_CMODULES:%=$(OBJDIR)/%.$(OBJEXT))
	@$(NARRATIVE) Linking $(COMPILETOWHAT) $@
	$(CAMLLINK) -o $@ \
                    $(PROJECT_LIBS:%=%.$(CMXA)) \
                    $(PROJECT_LIBS:%=-cclib -l%) \
                    $(PROJECT_CLIBS:%=-cclib %) \
                    $^ 
	cp $(PROJECT_EXECUTABLE) . 


PROJECT2_EXECUTABLE = $(OBJDIR)/weimorph$(EXE)
PROJECT2_MODULES    = $(WEIDU_BASE_MODULES) \
                        stats2 iwgconf iwgrule iwgparser iwglexer iwg2
ifdef HAVE_MSVC
PROJECT2_CMODULES   = reg
PROJECT2_CLIBS      = advapi32.lib 
endif
ifdef HAVE_MINGW
PROJECT2_CMODULES   = reg
PROJECT2_CLIBS      = -ladvapi32
endif
#PROJECT2_CMODULES   += eff_strings
PROJECT2_CMODULES   += zlib adler32 infblock inflate uncompr infcodes inftrees zutil inffast infutil $(GLOB) xdiff
PROJECT2_CMODULES   += xemit xpatchi xutils xdiffi xprepare 
PROJECT2_CMODULES   += $(ARCH_C_FILES)
PROJECT2_LIBS       = unix str 
.PHONY: weimorph
weimorph: $(PROJECT2_EXECUTABLE)
$(PROJECT2_EXECUTABLE) : $(PROJECT2_MODULES:%=$(OBJDIR)/%.$(CMO)) \
                        $(PROJECT2_CMODULES:%=$(OBJDIR)/%.$(OBJEXT))
	@$(NARRATIVE) Linking $(COMPILETOWHAT) $@
	$(AT)$(CAMLLINK) -o $@ \
                    $(PROJECT2_LIBS:%=%.$(CMXA)) \
                    $(PROJECT2_LIBS:%=-cclib -l%) \
                    $(PROJECT2_CLIBS:%=-cclib %) \
                    $^ 
	cp $(PROJECT2_EXECUTABLE) . 

PROJECT3_EXECUTABLE = $(OBJDIR)/bg1tutu_restore$(EXE)
PROJECT3_MODULES    = $(WEIDU_BASE_MODULES) \
                        stats2 bg1tutu_restore
ifdef HAVE_MSVC
PROJECT3_CMODULES   = reg
PROJECT3_CLIBS      = advapi32.lib 
endif
ifdef HAVE_MINGW
PROJECT3_CMODULES   = reg
PROJECT3_CLIBS      = -ladvapi32
endif
#PROJECT3_CMODULES   += eff_strings
PROJECT3_CMODULES   += zlib adler32 infblock inflate uncompr infcodes inftrees zutil inffast infutil $(GLOB) xdiff
PROJECT3_CMODULES   += xemit xpatchi xutils xdiffi xprepare
PROJECT3_CMODULES   += $(ARCH_C_FILES)
PROJECT3_LIBS       = unix str
.PHONY: bg1tutu_restore
weimorph: $(PROJECT3_EXECUTABLE)
$(PROJECT3_EXECUTABLE) : $(PROJECT3_MODULES:%=$(OBJDIR)/%.$(CMO)) \
                        $(PROJECT3_CMODULES:%=$(OBJDIR)/%.$(OBJEXT))
	@$(NARRATIVE) Linking $(COMPILETOWHAT) $@
	$(AT)$(CAMLLINK) -o $@ \
                    $(PROJECT3_LIBS:%=%.$(CMXA)) \
                    $(PROJECT3_LIBS:%=-cclib -l%) \
                    $(PROJECT3_CLIBS:%=-cclib %) \
                    $^ 
	cp $(PROJECT3_EXECUTABLE) . 

###
### Cleaning
###
clean: cleancaml
	rm -f $(PROJECT_EXECUTABLE) $(PROJECT_EXECUTABLE2) WeiDU.exe weidu.asm.exe weimorph.asm.exe bg1tutu_restore.asm.exe src/arch.ml

###
### Distro
###
VER = `grep "let version" WeiDU/src/version.ml | cut -d \" -f 2`
IWG2 = `grep "let iwg2_version" /cygdrive/c/src/WeiDU/src/iwg2.ml | cut -d ' ' -f 4`
BG1TUTU = `grep "let bg1tutu_version" /cygdrive/c/bg1tutu/src/iwg2.ml | cut -d ' ' -f 4`

doc: README-WeiDU.html
README-WeiDU.html : doc/base.tex
	$(MAKE) -C doc

ifdef ITEMLIST
zip : 
	echo cannot make zip -- itemlist included
else
zip : weidu doc clean
	rm iwg2* weimorph* || true
	mv wei*.exe WeiDU.exe
	strip WeiDU.exe || true
	upx --best WeiDU.exe || echo "No EXE Compression"
	(cd .. ; zip -9r WeiDU-$(VER).zip WeiDU )
endif

BGII_DIR = "/cygdrive/c/Program Files/Black Isle/BGII - SoA/"
BG1_DIR = "/cygdrive/c/Program Files/Black Isle/Baldur's Gate/"
BG_BACKUP = "/cygdrive/c/ie/backup/tob/"

iwg2zip: weimorph
	upx --best weimorph.asm.exe || echo "Fine" 
	cp weimorph.asm.exe $(IWD2_DIR)/iwg2.exe
	( cd $(IWD2_DIR) ; rm iwg2/errors/* || echo fine) 
	( cd $(IWD2_DIR) ; /home/weimer/bin/rar a -s -m5 -sfx -r IWG2-v$(IWG2).exe iwg2.exe "iwg2/*" )
	( cd $(IWD2_DIR) ; chmod a+r IWG2-v$(IWG2).exe )

bg1tutu: weimorph
	upx --best weimorph.asm.exe || echo "Fine"
	cp weimorph.asm.exe $(BGII_DIR)bg1tutu.exe
	upx --best bg1tutu_restore.asm.exe || echo "Fine"
	cp bg1tutu_restore.asm.exe $(BGII_DIR)bg1tutu_restore.exe
	( cd $(BGII_DIR) ; rm bg1tutu/errors/* || echo fine)
	( cd $(BGII_DIR) ; rm bg1tutu/copyback/* || echo fine)
	( cd $(BGII_DIR) ; rm bg1tutu/bringback || echo fine)
	( cd $(BGII_DIR) ; mv bg1tutu/backup/bgmain.exe bgmain.exe || echo fine)
	( cd $(BGII_DIR) ; mv bg1tutu/backup/BG1baldur.ini $(BG1_DIR)baldur.ini || echo fine)
	( cd $(BGII_DIR) ; mv bg1tutu/backup/BG2baldur.ini $(BGII_DIR)baldur.ini || echo fine)
	( cd $(BGII_DIR) ; rm bg1tutu/backup/override/* || echo fine)
	( cd $(BGII_DIR) ; rmdir bg1tutu/backup/override || echo fine)
	( cd $(BGII_DIR) ; rm bg1tutu/backup/* || echo fine)
	( cd $(BGII_DIR) ; rm bg1tutu/tutufix/backup/0/* || echo fine)
	( cd $(BGII_DIR) ; rm bg1tutu/tutufix/backup/1/* || echo fine)
	( cd $(BGII_DIR) ; rm bg1tutu/tutufix/backup/2/* || echo fine)
	( cd $(BGII_DIR) ; rm bg1tutu/tutufix/backup/3/* || echo fine)
	( cd $(BGII_DIR) ; rm bg1tutu/tutufix/backup/4/* || echo fine)
	( cd $(BGII_DIR) ; rm bg1tutu/tutufix/backup/5/* || echo fine)
	( cd $(BGII_DIR) ; rm bg1tutu/tutufix/backup/6/* || echo fine)
	( cd $(BGII_DIR) ; rmdir bg1tutu/tutufix/backup/0 || echo fine)
	( cd $(BGII_DIR) ; rmdir bg1tutu/tutufix/backup/1 || echo fine)
	( cd $(BGII_DIR) ; rmdir bg1tutu/tutufix/backup/2 || echo fine)
	( cd $(BGII_DIR) ; rmdir bg1tutu/tutufix/backup/3 || echo fine)
	( cd $(BGII_DIR) ; rmdir bg1tutu/tutufix/backup/4 || echo fine)
	( cd $(BGII_DIR) ; rmdir bg1tutu/tutufix/backup/5 || echo fine)
	( cd $(BGII_DIR) ; rmdir bg1tutu/tutufix/backup/6 || echo fine)
	( cd $(BGII_DIR) ; rm dialog.tlk || echo fine)
	( cd $(BGII_DIR) ; rm chitin.key || echo fine)
	( cd $(BGII_DIR) ; rm bg1tutu.log || echo fine)
	( cd $(BGII_DIR) ; rm weidu.log || echo fine)
	( cd $(BGII_DIR) ; rm override/_[A-F]* || echo fine)
	( cd $(BGII_DIR) ; rm override/_[G-M]* || echo fine)
	( cd $(BGII_DIR) ; rm override/_* || echo fine)
	( cd $(BGII_DIR) ; rm override/[A-F]* || echo fine)
	( cd $(BGII_DIR) ; rm override/* || echo fine)
	( cd $(BGII_DIR) ; rm bg1tutu_v$(BG1TUTU).exe || echo fine)
	( cd $(BGII_DIR) ; rm SETUP-TUTUFIX.DEBUG || echo fine)
	( cd $(BGII_DIR) ; rm backups/* || echo fine)
	( cd $(BGII_DIR) ; rmdir backups || echo fine)
	( cd $(BG_BACKUP) ; cp override/* $(BGII_DIR)/override  || echo fine)
	( cd $(BG_BACKUP) ; cp dialog.tlk $(BGII_DIR)/dialog.tlk  || echo fine)
	( cd $(BG_BACKUP) ; cp chitin.key $(BGII_DIR)/chitin.key  || echo fine)
	( cd $(BGII_DIR) ; "/cygdrive/c/program files/winrar/rar" a -s -m5 -o+ -sfx -r bg1tutu_v$(BG1TUTU).exe bg1tutu.exe bg1tutu_restore.exe BG1Tutu_Readme.htm setup-tutufix.exe setup-tutufix.tp2 setup-dudleyfix.tp2 "bg1tutu/*" )
	( cd $(BGII_DIR) ; chmod a+r bg1tutu_v$(BG1TUTU).exe )
