#
# the following works with UnixUtils
# GNU Make version 3.78.1 Built for Windows32
#
TCLSH       = bin\tclkitsh-win32.exe
BINARY      = bin\tcl2doxy.exe
BINARY2     = bin\tcl2doxy
DOXYGEN     = doxygen
HHC         = hhc
#
# uncomment and/or update the following two lines for cygwin
# GNU Make 3.81 built for i686-pc-cygwin
#
#TCLSH       = bin/tclkitsh-win32.exe
#BINARY      = bin/tcl2doxy.exe
#BINARY2     = bin/tcl2doxy
#
# uncomment and/or update the following two lines for linux
# GNU Make 3.81 built for i486-pc-linux-gnu
#
#TCLSH       = bin/tclkit-linux-x86
#BINARY      = bin/tcl2doxy
#BINARY2     = bin/tcl2doxy.exe
#
# these settings should work for all platforms
# the forward slashes are correct, because they are used as arguments for Tcl
#
FICKLE      = fickle-2.04/fickle.tcl
TACCLE      = taccle-1.1/taccle.tcl
WRAPPER     = ${TCLSH} bin/build.tcl
ZIPPER      = ${TCLSH} release/release.tcl zipper-1.1/zipper.tcl
TESTBINARY  = $(TCLSH) src/tcl2doxy.tcl
RELEASE     = release/tcl2doxy.zip
#
# nothing to change from here
#
SRC    = src/tcl2doxy.tcl
SRC   += src/lexer.tcl
SRC   += src/parser.tcl
SRC   += src/parser.tab.tcl
SRC   += src/syntree.tcl

TESTCASES  = test/tcl2doxy.output
TESTCASES += test/parser.output
TESTCASES += test/lexer.output
TESTCASES += test/syntree.output
TESTCASES += test/ToDo.output
TESTCASES += test/Empty.output
TESTCASES += test/SingleSpace.output
TESTCASES += test/SingleNewline.output
TESTCASES += test/Comments.output
TESTCASES += test/Switch.output
TESTCASES += test/Quotes.output
TESTCASES += test/Parentheses.output

tcl2doxy: $(SRC) Makefile

bin: $(BINARY)

doc: doc/tcl2doxy.chm

test: $(SRC) Makefile
	make $(TESTCASES)

testbin: bin
	make TESTBINARY=$(BINARY) $(TESTCASES)

$(BINARY): $(SRC) Makefile README LICENSE
	$(WRAPPER) $(SRC) README LICENSE -o $(BINARY2)
	$(WRAPPER) $(SRC) README LICENSE -o $(BINARY)

.PHONY: clean
clean:
	echo file delete -force -- $(TESTCASES) | $(TCLSH)
	echo file delete -force -- src/parser.tcl | $(TCLSH)
	echo file delete -force -- src/parser.tab.tcl | $(TCLSH)
	echo file delete -force -- src/lexer.tcl | $(TCLSH)
	echo file delete -force -- $(BINARY) | $(TCLSH)
	echo foreach i [ glob -nocomplain -directory bin tcl2doxy* ] { file delete -force -- [ set i ] } | $(TCLSH)

doc/tcl2doxy.chm: $(SRC) Makefile doc/Doxyfile bin/build.tcl release/release.tcl
	$(DOXYGEN) doc/Doxyfile
	-$(HHC) doc/html/index.hhp

.PHONY: release
release: bin doc
	$(ZIPPER) $(SRC) README Makefile LICENSE $(BINARY) $(BINARY2) bin/build.tcl doc/Doxyfile doc/tcl2doxy.chm -o $(RELEASE)

test/tcl2doxy.output: src/tcl2doxy.tcl $(SRC)
	$(TESTBINARY) -deparse $< >$@
	$(TESTBINARY) $< >$@
	$(TESTBINARY) -lexer $< >$(@:.output=.1.output)
	$(TESTBINARY) -parser $< >$(@:.output=.2.output)
	-$(TESTBINARY) -unknown $< >$(@:.output=.3.output)

test/parser.output: src/parser.tcl $(SRC)
	$(TESTBINARY) -deparse $< >$@
	$(TESTBINARY) $< >$@

test/lexer.output: src/lexer.tcl $(SRC)
	$(TESTBINARY) -deparse $< >$@
	$(TESTBINARY) $< >$@

test/syntree.output: src/syntree.tcl $(SRC)
	$(TESTBINARY) -deparse $< >$@
	$(TESTBINARY) $< >$@

%.tcl: %.fcl
	$(TCLSH) $(FICKLE) $<

%.tcl: %.tac
	$(TCLSH) $(TACCLE) -v -d $<
	echo puts -nonewline [ open $(<:.tac=.tmp.tcl) w ] {namespace eval yy } | $(TCLSH)
	echo puts            [ open $(<:.tac=.tmp.tcl) a ] [ binary format c 123 ] | $(TCLSH)
	echo puts -nonewline [ open $(<:.tac=.tmp.tcl) a ] [ read [ open $(<:.tac=.tab.tcl) r ] ] | $(TCLSH)
	echo puts -nonewline [ open $(<:.tac=.tmp.tcl) a ] [ binary format c 125 ] | $(TCLSH)
	echo file mtime $(<:.tac=.tmp.tcl) [ file mtime $(<:.tac=.tab.tcl) ] | $(TCLSH)
	echo file rename -force $(<:.tac=.tmp.tcl) $(<:.tac=.tab.tcl) | $(TCLSH)

%.output: %.tcl $(SRC)
	$(TESTBINARY) -deparse $< >$(<:%.tcl=%.output)
	$(TESTBINARY) -scriptlevel $< >$(<:%.tcl=%.output)


