! Makefile.conf
!    Makefile template for Scid for Unix operating systems.
!
! This file is NOT an actual Makefile; it is a template file used by 
! the configure program to produce the actual Makefile for Scid with
! approriate settings for your system. Just type "./configure" in the 
! current directory to run the configuration program.
!
! All lines starting with "!" (such as these) are removed, and certain
! variables enclosed in "@" symbols (such as @TCL_VERSION@) are set.
!
##### Makefile for Scid for Unix operating systems.

### Compiler: Most Unix systems use g++ for compiling and linking.
#
COMPILE = @COMPILE@
LINK = @LINK@
INSTALL = @INSTALL@

# BINDIR: where the Scid programs are copied for "make install".
#
BINDIR = @BINDIR@

# SHAREDIR: where scid.eco is copied for "make install".
#
SHAREDIR = @SHAREDIR@

### TCL_VERSION: Set this according to the version of Tcl/Tk you have
#   installed that you want Scid to use: 8.0, 8.1, 8.2, 8.3, etc.
#
TCL_VERSION = @TCL_VERSION@

# TCL_INCLUDE, TCL_LIBRARY: these are the compiler options
#    needed for linking Scid with Tcl/Tk.  The program "./configure"
#    will try to determine them automatically, but if it cannot, you
#    can use the examples below for help in setting these variables.
#
# @CONFIG_RESULT@
#
TCL_INCLUDE = @TCL_INCLUDE@
TCL_LIBRARY = @TCL_LIBRARY@


########################################
### Compiler options:

### TB: Using Nalimov tablebases with Scid. Use "TB = -DSCID_USE_TB" for 
#      tablebase support, or just "TB = " for no tablebase capability.
#      Use "TB = -DSCID_USE_TB -DT41_INCLUDE" to include use of 4-1
#      (King + 3 pieces vs lone king) tablebases.
#
TB = @TB@

### SCIDFLAGS: Scid customization flags.
#
SCIDFLAGS = @SCIDFLAGS@

### OPTIMIZE: Optimization options for C++ compiler.
#      -O3 is the most optimization for g++. I have found -O2 to do
#      just as well, but someone reported a noticeable difference in speed
#      between -O3 and -O2 so the default here is -O3.
#      On some systems, adding "-fno-rtti" and "-fno-exceptions" produces
#      smaller, faster programs since Scid does not use those C++ features.
#
OPTIMIZE = @OPTIMIZE@ @THREADS@

### DEBUG: Debug flags. Use "-g" to include debugging information and
#       "-DNDEBUG" to disable debugging information and assertions.
#
DEBUG = @DEBUG@

### WARNINGS: I always compile with all warnings on (-Wall), and all the
#       files should compile warning-free using g++.
#
WARNINGS = @WARNINGS@

### PROFILE: Set this to "-pg" for profiling in g++ and gcc.
#
PROFILE = @PROFILE@

### CPP_FLAGS: Flags for C++ compilation.
#
CPP_FLAGS = -MMD $(PROFILE) $(OPTIMIZE) $(WARNINGS) $(DEBUG) $(SCIDFLAGS) $(TB)


ifeq ($(INSTALL),install_mac)
### SCID_VERSION: Set this to the release version or it will find it by itself.
SCID_VERSION = `grep 'set scidVersion ' tcl/start.tcl | sed -E -e 's/^set scidVersion \"(.+)\"/\1/'`

### SCID_APP_BIN: This will find the correct path to tkscid.
SCID_APP_BIN = $$(cd "$$(dirname "$$0")/$$(dirname $$(readlink "$$0"))"; pwd)/
endif
############################################################
#
# You should not need to edit anything below this line.
#
############################################################

### EXECS: executable programs compiled from C++ files.
#
EXECS= tkscid

### SCIDOBJS:
SCIDCPP= $(wildcard src/*.cpp)
SCIDOBJS= $(SCIDCPP:.cpp=.o)

POLYGLOT= $(wildcard src/polyglot/*.cpp)
XOBJS= $(POLYGLOT:.cpp=.o)

DEPENDS = ${SCIDOBJS:.o=.d} ${XOBJS:.o=.d}

### OBJS: Will be "$(SCIDOBJS)", "$(POLYGLOTOBJS)"
OBJS= @OBJS@

### TCLS: all the .tcl files that make up "scid".
#
TCLS= tcl/start.tcl

### SCRIPTS:
# Small extra programs. Most are written in Tcl using tkscid, but
# a few contributed ones may be in Python or other languages.
#
SCRIPTS= sc_epgn sc_spell sc_eco sc_import sc_remote scidpgn pgnfix spliteco spf2spi

####################

### Type "make" or "make all" to make all programs:
#
all: all_scid all_engines

all_scid: scid $(SCRIPTS) $(EXECS)

all_engines: scidlet phalanx-scid


scidlet: src/engine.o src/recog.o src/misc.o src/position.o
	$(COMPILE) $(CPP_FLAGS) -o scidlet engines/scidlet/scidlet.cpp src/engine.o src/recog.o src/misc.o src/position.o

phalanx-scid:
	cd engines/phalanx-scid/ && $(MAKE) && cd ../../

### To copy all executables to $BINDIR, with read and execute permission 
#   for all users, and put extra files in $SHAREDIR, type "make install".
#

install: $(INSTALL)

install_scid: all_scid
	install -m 755 -d "$(DESTDIR)$(SHAREDIR)"
	install -m 755 -d "$(DESTDIR)$(BINDIR)"
	install -m 755 $(SCRIPTS) $(EXECS) "$(DESTDIR)$(BINDIR)"
	rm -f "$(DESTDIR)$(BINDIR)/scid"
	@echo "#!/bin/sh" > "$(DESTDIR)$(BINDIR)/scid"
	@echo 'exec "$(BINDIR)/tkscid" "$(SHAREDIR)/tcl/start.tcl" "$$@"' >> "$(DESTDIR)$(BINDIR)/scid"
	chmod 755 "$(DESTDIR)$(BINDIR)/scid"
	chmod +x "$(DESTDIR)$(BINDIR)/scid"
	install -m 644 -p scid.eco "$(DESTDIR)$(SHAREDIR)"
	install -m 755 -d "$(DESTDIR)$(SHAREDIR)/books"
	install -m 666 ./books/*.* "$(DESTDIR)$(SHAREDIR)/books/"
	install -m 755 -d "$(DESTDIR)$(SHAREDIR)/bases"
	if test -d ./bases; then install -m 666 ./bases/*.* "$(DESTDIR)$(SHAREDIR)/bases/" ; fi
	install -m 755 -d "$(DESTDIR)$(SHAREDIR)/html"
	cp -r ./html/* "$(DESTDIR)$(SHAREDIR)/html/"
	install -m 755 -d "$(DESTDIR)$(SHAREDIR)/img/"
	cp -r ./img/* "$(DESTDIR)$(SHAREDIR)/img/"
	install -m 755 -d "$(DESTDIR)$(SHAREDIR)/tcl/"
	cp -r ./tcl/* "$(DESTDIR)$(SHAREDIR)/tcl/"

install_engines: all_engines
	install -m 755 -d "$(DESTDIR)$(SHAREDIR)/engines"
	install -m 755 -d "$(DESTDIR)$(SHAREDIR)/engines/phalanx-scid"
	install ./engines/phalanx-scid/phalanx-scid "$(DESTDIR)$(SHAREDIR)/engines/phalanx-scid"

install_mac: all
	install -m 755 -d dist/Scid.app/Contents/MacOS
	install -m 755 -d dist/Scid.app/Contents/Resources/bin
	install -m 755 -d dist/Scid.app/Contents/Resources/share/scid/data
	install -m 755 -d dist/Scid.app/Contents/Resources/share/scid/books
	install -m 755 -d dist/Scid.app/Contents/Resources/share/scid/html
	install -m 755 -d dist/Scid.app/Contents/Resources/share/scid/sounds
	#install -m 755 -d dist/Scid.app/Contents/Resources/share/scid/engines
	install -m 755 -d dist/Scid.app/Contents/Resources/Scripts
	#install -m 755 $(SCRIPTS) $(EXECS) dist/Scid.app/Contents/Resources/bin/
	install -m 755 $(EXECS) dist/Scid.app/Contents/Resources/bin/
	install -m 644 -p scid.eco dist/Scid.app/Contents/Resources/share/scid/data/
	install -m 666 -p ./books/* dist/Scid.app/Contents/Resources/share/scid/books/
	install -m 644 -p ./sounds/* dist/Scid.app/Contents/Resources/share/scid/sounds/
	install -m 755 -p ./engines/phalanx-scid/phalanx-scid dist/Scid.app/Contents/Resources/bin/
	install -m 644 -p ./resources/macos/Scid*.icns dist/Scid.app/Contents/Resources/
	install -m 755 -d dist/Scid.app/Contents/Resources/share/scid/img/
	install -m 755 -d dist/Scid.app/Contents/Resources/share/scid/tcl/
	ditto ./tcl dist/Scid.app/Contents/Resources/share/scid/tcl
	ditto ./img dist/Scid.app/Contents/Resources/share/scid/img
	ditto ./html dist/Scid.app/Contents/Resources/share/scid/html
	install -m 666 resources/macos/Info.plist dist/Scid.app/Contents/
	perl -pi -e "s|x\.x\</string\>|$(SCID_VERSION)</string\>|" dist/Scid.app/Contents/Info.plist
	cd dist/Scid.app/Contents/Resources/bin/ && ln -sf ../share/scid/sounds
	cd dist/Scid.app/Contents/Resources/bin/ && ln -sf ../share/scid/html
	LANG=C sed -e 's|exec .*tkscid|exec "$(SCID_APP_BIN)tkscid"|' < scid > dist/Scid.app/Contents/Resources/bin/scid
	for script in $(SCRIPTS); do sed -e 's|exec .*tkscid|exec "$(SCID_APP_BIN)tkscid"|' < $${script} > dist/Scid.app/Contents/Resources/bin/$${script};done
	chmod +x dist/Scid.app/Contents/Resources/bin/*
	cd dist/Scid.app/Contents/MacOS && ln -sf ../Resources/bin/scid
	#cd dist/Scid.app/Contents/MacOS && ln -sf ../Resources/bin/tkscid scid
	#cd dist/Scid.app/Contents/Resources/Scripts && ln -sf ../bin/scid AppMain.tcl

scid_dmg: install_mac
	install -m 755 -d dist/bitmaps
	install -m 755 -d dist/bitmaps2
	install -m 755 -d dist/Scripts
	install -m 755 -d dist/Scripts/CB-MEGA
	install -m 644 -p ChangeLog ./dist/Change\ Log.txt
	install -m 644 -p CHANGES ./dist/Changes.txt
	install -m 644 -p COPYING ./dist/Copying.txt
	install -m 644 -p TODO ./dist/To\ Do.txt
	install -m 644 -p ./bitmaps/* ./dist/bitmaps/
	install -m 644 -p ./bitmaps2/* ./dist/bitmaps2/
	#install -m 755 -p ./scripts/* ./dist/Scripts/
	install -m 755 -p ./scripts/eco2pgn.py ./dist/Scripts/
	install -m 755 -p ./scripts/pgnfix.py ./dist/Scripts/
	install -m 755 -p ./scripts/twic2scid.py ./dist/Scripts/
	install -p ./scripts/CB-Mega/* ./dist/Scripts/CB-Mega/
	hdiutil create -fs "HFS+" -volname Scid -srcfolder dist "Scid $(SCID_VERSION) (`sw_vers -productName` `sw_vers -productVersion` `uname -p`).dmg"

uninstall:
	rm -rf $(SHAREDIR)
	rm -f $(BINDIR)/scid $(BINDIR)/sc_addmove $(BINDIR)/sc_epgn
	rm -f $(BINDIR)/sc_spell $(BINDIR)/sc_eco $(BINDIR)/sc_import
	rm -f $(BINDIR)/sc_remote $(BINDIR)/sc_tree $(BINDIR)/scidpgn
	rm -f $(BINDIR)/pgnfix $(BINDIR)/spliteco
	rm -f $(BINDIR)/tkscid
	rm -f $(BINDIR)/scidlet

### To remove Scid files placed in the BINDIR and SHAREDIR directories,
#   type "make distclean".
#
distclean:
	cd $(BINDIR) && rm -f $(EXECS) $(SCRIPTS)
	-rm -f $(SHAREDIR)/scid.eco

### To remove object and executable files: type "make clean".
#
clean:
	rm -f scidlet scidlet.d src/*.o src/polyglot/*.o $(EXECS) scid $(SCRIPTS)
	rm -f src/*.d src/polyglot/*.d
	cd engines/phalanx-scid/ && make clean && cd ../../
	rm -Rf dist

### To make the executable files smaller: type "make strip".
#
strip:
	strip $(EXECS)

### To compress scid and executables with gzexe: type "make gzexe".
#
gzexe:
	gzexe $(EXECS) scid


scid: $(TCLS)
	rm -f ./scid
	cat $(TCLS) > ./scid
	chmod +x scid

sc_epgn: scripts/sc_epgn.tcl
	cp scripts/sc_epgn.tcl ./sc_epgn
	chmod +x sc_epgn

sc_spell: scripts/sc_spell.tcl
	cp scripts/sc_spell.tcl ./sc_spell
	chmod +x sc_spell

sc_eco: scripts/sc_eco.tcl
	cp scripts/sc_eco.tcl ./sc_eco
	chmod +x sc_eco

sc_import: scripts/sc_import.tcl
	cp scripts/sc_import.tcl ./sc_import
	chmod +x sc_import

sc_remote: scripts/sc_remote.tk
	cp scripts/sc_remote.tk ./sc_remote
	chmod +x sc_remote

scidpgn: scripts/scidpgn.tcl
	cp scripts/scidpgn.tcl ./scidpgn
	chmod +x scidpgn

spliteco: scripts/spliteco.tcl
	cp scripts/spliteco.tcl ./spliteco
	chmod +x spliteco

tbstats: scripts/tbstats.tcl
	cp scripts/tbstats.tcl ./tbstats
	chmod +x tbstats

wmtest: scripts/wmtest.tk
	cp scripts/wmtest.tk ./wmtest
	chmod +x wmtest

pgnfix: scripts/pgnfix.py
	cp scripts/pgnfix.py ./pgnfix
	chmod +x pgnfix

eco2pgn: scripts/eco2pgn.py
	cp scripts/eco2pgn.py ./eco2pgn
	chmod +x eco2pgn

twic2pgn: scripts/twic2pgn.py
	cp scripts/twic2pgn.py ./twic2pgn
	chmod +x twic2pgn

spf2spi: scripts/spf2spi.tcl
	cp scripts/spf2spi.tcl ./spf2spi
	chmod +x spf2spi


tkscid: $(OBJS) $(XOBJS)
	$(LINK) $(CPP_FLAGS) $(XOBJS) $(OBJS) $(TCL_LIBRARY) -o tkscid

### Generic rule for .cpp files:
#
src/%.o: src/%.cpp
	$(COMPILE) $(CPP_FLAGS) $(TCL_INCLUDE) -o $@ -c $<

-include ${DEPENDS}
### End of Makefile
