#----------------------------------------------------------------------
# Make file for Eskil
#----------------------------------------------------------------------

# This string is used to generate release file names
VERSION = 285
# This string is used to tag the version shown in Eskil
DOTVERSION = 2.8.5

# Path to the TclKits used for creating StarPacks.
TCLKIT = /home/$(USER)/tclkit/v86
TCLKIT_LINUX   = $(TCLKIT)/tclkit-linux
TCLKIT_WIN     = $(TCLKIT)/tclkit-win32.upx.exe
TCLKIT_MAC     = $(TCLKIT)/tclkit-mac-867

# Paths to the libraries used.
# If you do not have access to all these, you can get them from an Eskil kit
# as explained below.
TEXTSEARCH = /home/$(USER)/src/textsearch
DIFFUTIL   = /home/$(USER)/src/DiffUtilTcl/lib.vfs/DiffUtil
WCB        = /home/$(USER)/src/packages/wcb3.8
PDF4TCL    = /home/$(USER)/src/pdf4tcl/pkg
SNIT       = /home/$(USER)/src/packages/tcllib/modules/snit
TABLELIST  = /home/$(USER)/src/packages/tablelist6.22
TWAPI      = /home/$(USER)/src/packages/twapi
TKDND      = /home/$(USER)/src/packages/tkdnd/lib/tkdnd2.4
EMBEDFONT  = /usr/share/fonts/truetype/liberation/LiberationMono-Regular.ttf

# Tools
NAGELFAR    = nagelfar

all: setup

SRCFILES = src/eskil.tcl src/clip.tcl src/dirdiff.tcl src/help.tcl src/map.tcl \
	   src/print.tcl src/registry.tcl src/rev.tcl src/debug.tcl \
	   src/compare.tcl src/merge.tcl src/printobj.tcl src/plugin.tcl \
           src/vcsvfs.tcl src/preprocess.tcl src/startup.tcl src/fourway.tcl

#----------------------------------------------------------------
# Build a dependency tree to other libs needed.
# This is made in a parallell VFS structure, mimicking Eskil's.
# Thus deps.vfs can also be created by downloading an Eskil kit,
# rename it to "deps" and unwrap it with sdx.
#----------------------------------------------------------------

deps.vfs/src/embedfont.ttf:
	@mkdir -p deps.vfs/src
	@cd deps.vfs/src ; ln -fs $(EMBEDFONT) embedfont.ttf
deps.vfs/lib/wcb:
	@mkdir -p deps.vfs/lib
	cd deps.vfs/lib ; ln -fns $(WCB) wcb
deps.vfs/lib/textsearch:
	@mkdir -p deps.vfs/lib
	cd deps.vfs/lib ; ln -fns $(TEXTSEARCH) textsearch
deps.vfs/lib/diffutil:
	@mkdir -p deps.vfs/lib
	cd deps.vfs/lib ; ln -fns $(DIFFUTIL) diffutil
deps.vfs/lib/pdf4tcl:
	@mkdir -p deps.vfs/lib
	cd deps.vfs/lib ; ln -fns $(PDF4TCL) pdf4tcl
deps.vfs/lib/tkdnd:
	@mkdir -p deps.vfs/lib
	cd deps.vfs/lib ; ln -fns $(TKDND) tkdnd
deps.vfs/lib/tablelist:
	@mkdir -p deps.vfs/lib
	cd deps.vfs/lib ; ln -fns $(TABLELIST) tablelist
deps.vfs/lib/snit:
	@mkdir -p deps.vfs/lib/snit
	cd deps.vfs/lib/snit ; ln -fs $(SNIT)/pkgIndex.tcl
	cd deps.vfs/lib/snit ; ln -fs $(SNIT)/snit.tcl
	cd deps.vfs/lib/snit ; ln -fs $(SNIT)/snit2.tcl
	cd deps.vfs/lib/snit ; ln -fs $(SNIT)/main2.tcl
	cd deps.vfs/lib/snit ; ln -fs $(SNIT)/main1.tcl
	cd deps.vfs/lib/snit ; ln -fs $(SNIT)/validate.tcl

#------------------------------------------------------------------
# Setup symbolic links from the VFS to the sources and dependencies
#------------------------------------------------------------------

eskil.vfs/src/eskil.tcl:
	@mkdir -p eskil.vfs/src
	@cd eskil.vfs/src ; for i in $(SRCFILES); do ln -fs ../../$$i ; done
eskil.vfs/src/images:
	@mkdir -p eskil.vfs/src
	@cd eskil.vfs/src ; ln -fs ../../src/images
eskil.vfs/src/embedfont.ttf: deps.vfs/src/embedfont.ttf
	@mkdir -p eskil.vfs/src
	@cd eskil.vfs/src ; ln -fs ../../deps.vfs/src/embedfont.ttf
	@cd src ; ln -fs ../deps.vfs/src/embedfont.ttf
eskil.vfs/examples:
	cd eskil.vfs ; ln -fs ../examples
eskil.vfs/doc:
	cd eskil.vfs ; ln -fs ../doc
eskil.vfs/plugins:
	cd eskil.vfs ; ln -fs ../plugins
eskil.vfs/COPYING:
	cd eskil.vfs ; ln -fs ../COPYING
eskil.vfs/lib/wcb: deps.vfs/lib/wcb
	cd eskil.vfs/lib ; ln -fs ../../deps.vfs/lib/wcb
eskil.vfs/lib/textsearch: deps.vfs/lib/textsearch
	cd eskil.vfs/lib ; ln -fs ../../deps.vfs/lib/textsearch
eskil.vfs/lib/diffutil: deps.vfs/lib/diffutil
	cd eskil.vfs/lib ; ln -fs ../../deps.vfs/lib/diffutil
eskil.vfs/lib/pdf4tcl: deps.vfs/lib/pdf4tcl
	cd eskil.vfs/lib ; ln -fs ../../deps.vfs/lib/pdf4tcl
eskil.vfs/lib/tkdnd: deps.vfs/lib/tkdnd
	cd eskil.vfs/lib ; ln -fs ../../deps.vfs/lib/tkdnd
eskil.vfs/lib/tablelist: deps.vfs/lib/tablelist
	cd eskil.vfs/lib ; ln -fs ../../deps.vfs/lib/tablelist
eskil.vfs/lib/snit: deps.vfs/lib/snit
	cd eskil.vfs/lib ; ln -fs ../../deps.vfs/lib/snit

links: eskil.vfs/src/eskil.tcl \
	eskil.vfs/src/images \
	eskil.vfs/src/embedfont.ttf \
	eskil.vfs/examples\
	eskil.vfs/doc\
	eskil.vfs/plugins\
	eskil.vfs/COPYING\
	eskil.vfs/lib/textsearch\
	eskil.vfs/lib/diffutil\
	eskil.vfs/lib/pdf4tcl\
	eskil.vfs/lib/snit\
	eskil.vfs/lib/tkdnd\
	eskil.vfs/lib/tablelist\
	eskil.vfs/lib/wcb

# Use this with -B to just update the source links when a new source file
# is present
newsrc: eskil.vfs/src/eskil.tcl

src/TAGS: $(SRCFILES)
	etags -o src/TAGS --regex="/proc[ \t]+\([^ \t]+\)/\1/" $(SRCFILES) \
	eskil.vfs/lib/*/*.tcl

setup: links src/TAGS

# Use this to rebuild the docs when command line changes or
# new wiki files are added.
docs:
	echo "<title>Usage</title>" > htdocs/usage.wiki
	echo "" >> htdocs/usage.wiki
	echo "<h1>Command Line Usage</h1>" >> htdocs/usage.wiki
	echo "" >> htdocs/usage.wiki
	echo "<verbatim>" >> htdocs/usage.wiki
	$(TCLKIT_LINUX) eskil.vfs/main.tcl -help | grep -v "  Version " >> htdocs/usage.wiki
	echo "</verbatim>" >> htdocs/usage.wiki
	echo "<title>Documentation</title>" > htdocs/toc.wiki
	echo "" >> htdocs/toc.wiki
	grep title htdocs/*.wiki | grep -v Documentation | \
	sed -e 's/htdocs/[./' -e 's/:<title>/|/' -e 's,</title>,],' | \
	awk '{print $0; print ""};' >> htdocs/toc.wiki
#----------------------------------------------------------------
# Testing
#----------------------------------------------------------------

spell:
	@cat doc/*.txt | ispell -d british -l | sort -u

CHKFILES = $(SRCFILES) $(wildcard plugins/*.tcl) \
	eskil.vfs/lib/psballoon/psballoon.tcl \
	eskil.vfs/lib/pstools/pstools.tcl
NAGELFARFLAGS = -s syntaxdb.tcl -pkgpicky -filter "*Non constant definition*" -quiet -plugin nfplugin.tcl

# Create a common "header" file for all source files.
eskil_h.syntax: $(SRCFILES) src/eskil.syntax nfplugin.tcl
	@echo Creating syntax header file...
	@$(NAGELFAR) $(NAGELFARFLAGS) -header eskil_h.syntax $(SRCFILES)

check: eskil_h.syntax
	@echo Checking...
	@for i in $(CHKFILES); do $(NAGELFAR)  $(NAGELFARFLAGS) eskil_h.syntax $$i ; done

test:
	@./tests/all.tcl $(TESTFLAGS)

run:
	$(TCLKIT_LINUX) eskil.vfs/main.tcl -debug

#----------------------------------------------------------------
# Coverage
#----------------------------------------------------------------

# Source files for code coverage
COVFILES = eskil.vfs/main.tcl eskil.vfs/src/rev.tcl eskil.vfs/src/eskil.tcl eskil.vfs/src/merge.tcl eskil.vfs/src/startup.tcl
IFILES   = $(COVFILES:.tcl=.tcl_i)
LOGFILES = $(COVFILES:.tcl=.tcl_log)
MFILES   = $(COVFILES:.tcl=.tcl_m)

# Instrument source file for code coverage
%.tcl_i: %.tcl eskil_h.syntax
	@$(NAGELFAR) -instrument eskil_h.syntax $<

# Target to prepare for code coverage run. Makes sure log file is clear.
instrument: $(IFILES)
	@rm -f $(LOGFILES)

# Run tests to create log file.
testcover $(LOGFILES): $(IFILES)
	@./tests/all.tcl $(TESTFLAGS)

# Create markup file for better view of result
%.tcl_m: %.tcl_log
	@$(NAGELFAR) -markup $*.tcl

# View code coverage result
icheck: $(MFILES)
	@for i in $(COVFILES) ; do eskil -noparse $$i $${i}_m & done

# Remove code coverage files
clean:
	@rm -f $(LOGFILES) $(IFILES) $(MFILES)

#----------------------------------------------------------------
# Packaging/Releasing
#----------------------------------------------------------------

tagversion:
	echo "Version $(DOTVERSION) `date --iso-8601`" > eskil.vfs/version.txt

wrap: tagversion
	sdx wrap eskil.kit

wrapexe: tagversion
	@\rm -f eskil.linux eskil.exe
	sdx wrap eskil.linux   -runtime $(TCLKIT_LINUX)
	sdx wrap eskil.mac     -runtime $(TCLKIT_MAC)
	cd eskil.vfs/lib ; ln -s $(TWAPI) twapi
	sdx wrap eskil.exe     -runtime $(TCLKIT_WIN)
	rm eskil.vfs/lib/twapi

release: setup wrap wrapexe
	@cp eskil.kit eskil`date +%Y%m%d`.kit
	@cp eskil.kit eskil$(VERSION).kit
	@gzip eskil.linux
	@mv eskil.linux.gz eskil$(VERSION).linux.gz
	@gzip eskil.mac
	@mv eskil.mac.gz eskil$(VERSION).mac.gz
	@zip eskil$(VERSION).win.zip eskil.exe
	@zip eskil`date +%Y%m%d`.win.zip eskil.exe

tofossil:
	fossil unversioned add eskil$(VERSION).kit      --as htdocs/download/eskil$(VERSION).kit
	fossil unversioned add eskil$(VERSION).linux.gz --as htdocs/download/eskil$(VERSION).linux.gz
	fossil unversioned add eskil$(VERSION).mac.gz   --as htdocs/download/eskil$(VERSION).mac.gz
	fossil unversioned add eskil$(VERSION).win.zip  --as htdocs/download/eskil$(VERSION).win.zip
	fossil unversioned list
	@echo 'Remember: fossil unversioned sync'
