# tcl 8.6.13
# tk 8.6.12
# tcllib 1.20
# tklib .7

# tksvg 0.7.2
# awthemes 10.3.0
# ttkthemes 3.2.2
# scidthemes 0.9.3

# tclfitsy 1.0
# tclsignal 1.4.5
# tclxml 3.2
# tclxmlrpc 1.0
# tclzipfs 1.0.1
# tls 1.6.7.5
# tkblt 3.2.23
# tkcon 2.7.11
# tkhtml1 1.0.11
# tkimg 1.4.13
# tkmpeg 1.0.10
# tkagif 1.0.5
# tktable 2.10.8

# ast 9.2.8
# funtools 1.4.8
# zlib 1.2.8
# xpa 2.1.20
# libxslt 1.1.29
# openssl 1.0.2u

DS9_VERSION	= 8.6
XPA_VERSION	= 2.1.20
TCL_VERSION	= 8.6

Tidy = \
	cd $(1); \
	for ff in *.html; \
	do echo ""; echo $$ff; \
	tidy -q -m -w 0 $$ff; \
	done;

TidyFile = \
	cd $(1); \
	echo ""; echo $(2); \
	tidy -q -m -w 0 $(2);

#-------------------------- Defs

DIRS   = $(bindir) $(libdir) $(includedir) $(datadir) man build dist
PREFIX = --prefix $(prefix) --exec-prefix $(exec_prefix) --libdir $(libdir)
STD    = --disable-shared --disable-stubs --enable-symbols

#-------------------------- tkimg tmp files

TKIMGTMPFILES = \
	      tkimg/libjpeg/libjpeg \
	      tkimg/libtiff/build \
	      tkimg/libtiff/contrib \
	      tkimg/libtiff/html \
	      tkimg/libtiff/libtiff \
	      tkimg/libtiff/libtool \
	      tkimg/libtiff/man \
	      tkimg/libtiff/mkg3states \
	      tkimg/libtiff/mkg3states.dSYM \
	      tkimg/libtiff/port \
	      tkimg/libtiff/test \
	      tkimg/libtiff/tif_fax3sm.c \
	      tkimg/libtiff/tools

#-------------------------- main

.NOTPARALLEL :
.PHONY	: all dirs ds9 tclxml tclxmlrpc tclzipfs tls xpa
.PHONY	: tkblt tclfitsy tkcon tkhtml1 tkimg tkmpeg tksvg tkagif tksao tktable
.PHONY	: vector fitsy ast funtools openssl

all	: saods9

dirs	:
	@echo ""
	@echo "*** Cores $(JOBS) ***"
	@echo ""
	@echo "*** Install $@ ***"
	@for d in $(DIRS); do if [ ! -d $$d ]; then mkdir $$d; fi done

#-------------------------- ds9

ds9	: ds9/$(OS)/Makefile
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C ds9/$(OS) -j $(JOBS)

debug	: ds9/$(OS)/Makefile
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C ds9/$(OS)  -j $(JOBS) debug

ds9/$(OS)/Makefile :
	@echo ""
	@echo "*** $@ ***"
	cd ds9/$(OS); \
	./configure \
	$(WITHTK) \
  	--with-xml2-config=$(XML2CONFIG) \
	$(PREFIX) \
	$(STD) \
	$(CACHE)

#--------------------------openssl

openssl	: openssl/Makefile
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C openssl -j $(JOBS)

openssl/Makefile:
	@echo ""
	@echo "*** $@ ***"
	cd openssl; \
	$(SSLCONFIG) --prefix=$(prefix)

#-------------------------- tclfitsy

tclfitsy: tclfitsy/Makefile
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tclfitsy -j $(JOBS) install

tclfitsy/Makefile :
	@echo ""
	@echo "*** $@ ***"
	cd tclfitsy; \
	./configure \
	$(WITHTCL) \
	$(PREFIX) \
	$(STD) \
	$(CACHE)

#-------------------------- tclxml

# no parallel
tclxml	: tclxml/Makefile
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tclxml install

tclxml/Makefile :
	@echo ""
	@echo "*** $@ ***"
	cd tclxml; \
	./configure \
	$(WITHTCL) \
  	--with-xml2-config=$(XML2CONFIG) \
  	--with-xslt-config=$(XSLTCONFIG) \
	$(PREFIX) \
	$(STD) \
	$(CACHE)

#-------------------------- tclxmlrpc

tclxmlrpc: tclxmlrpc/Makefile
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tclxmlrpc -j $(JOBS) install

tclxmlrpc/Makefile :
	@echo ""
	@echo "*** $@ ***"
	cd tclxmlrpc; \
	./configure \
	$(WITHTCL) \
	$(PREFIX) \
	$(STD) \
	$(CACHE)

#-------------------------- tls

# no cache (win)
tls	: tls/Makefile
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tls install

tls/Makefile :
	@echo ""
	@echo "*** $@ ***"
	cd tls; \
	./configure \
 	--with-ssl-dir=$(prefix)/openssl \
	 $(TLSFLAGS) \
	$(WITHTCL) \
	$(PREFIX) \
	$(STD)

#-------------------------- xpa

# no config.cache
xpa	: xpa/Makefile
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C xpa -j $(JOBS) install

xpa/Makefile :
	@echo ""
	@echo "*** $@ ***"
	cd xpa; \
	./configure \
	$(CONFIGFLAGS) $(TARGET) $(XPAFLAGS) \
	$(WITHTCL) \
	$(PREFIX) \
	$(STD) \
	--with-x=disabled

#-------------------------- tkblt

tkblt	: tkblt/Makefile
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tkblt -j $(JOBS) install

tkblt/Makefile :
	@echo ""
	@echo "*** $@ ***"
	cd tkblt; \
	./configure \
	$(WITHTK) \
	$(PREFIX) \
	$(STD) \
	$(CACHE)

#-------------------------- tkcon

tkcon	: tkcon/Makefile
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tkcon -j $(JOBS) install

tkcon/Makefile :
	@echo ""
	@echo "*** $@ ***"
	cd tkcon; \
	./configure \
	$(WITHTK) \
	$(PREFIX) \
	$(CACHE)

#-------------------------- tkhtml1

# no parallel
# make sure htmltokens.c does not get regenerated (windows)
tkhtml1	: tkhtml1/Makefile
	@echo ""
	@echo "*** $@ ***"
	touch tkhtml1/generic/htmltokens.c
	$(MAKE) -C tkhtml1 install

tkhtml1/Makefile :
	@echo ""
	@echo "*** $@ ***"
	cd tkhtml1; \
	./configure \
	$(WITHTK) \
	$(PREFIX) \
	$(STD) \
	$(CACHE)

#-------------------------- tkimg

tkimg	: tkimg/Makefile
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tkimg -j $(JOBS) all
	$(MAKE) -C tkimg/base -j $(JOBS) install
	$(MAKE) -C tkimg/tiff -j $(JOBS) install
	$(MAKE) -C tkimg/libtiff -j $(JOBS) install
	$(MAKE) -C tkimg/jpeg -j $(JOBS) install
	$(MAKE) -C tkimg/libjpeg -j $(JOBS) install
	$(MAKE) -C tkimg/window -j $(JOBS) install
	$(MAKE) -C tkimg/zlib -j $(JOBS) install

# no config.cache
tkimg/Makefile :
	@echo ""
	@echo "*** $@ ***"
	cd tkimg; \
	./configure \
	$(WITHTK) \
	$(PREFIX) \
	$(STD)

#-------------------------- tkmpeg

tkmpeg	: tkmpeg/Makefile
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tkmpeg -j $(JOBS) install

tkmpeg/Makefile :
	@echo ""
	@echo "*** $@ ***"
	cd tkmpeg; \
	./configure \
	$(WITHTK) \
	$(PREFIX) \
	$(STD) \
	$(CACHE)

#-------------------------- tkagif

tkagif	: tkagif/Makefile
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tkagif -j $(JOBS) install

tkagif/Makefile :
	@echo ""
	@echo "*** $@ ***"
	cd tkagif; \
	./configure \
	$(WITHTK) \
	$(PREFIX) \
	$(STD) \
	$(CACHE)

#-------------------------- tksao

tksao	: tksao/Makefile
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tksao -j $(JOBS) install

tksao/Makefile :
	@echo ""
	@echo "*** $@ ***"
	cd tksao; \
	./configure \
	$(WITHTK) \
  	--with-xml2-config=$(XML2CONFIG) \
	$(PREFIX) \
	$(STD) \
	$(CACHE)

#-------------------------- vector

vector	: vector/Makefile
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C vector -j $(JOBS) install

vector/Makefile :
	@echo ""
	@echo "*** $@ ***"
	cd vector; \
	./configure \
	$(WITHTCL) \
	$(PREFIX) \
	$(STD) \
	$(CACHE)

#-------------------------- fitsy

fitsy	: fitsy/Makefile
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C fitsy -j $(JOBS) install

fitsy/Makefile :
	@echo ""
	@echo "*** $@ ***"
	cd fitsy; \
	./configure \
	$(WITHTK) \
	$(PREFIX) \
	$(STD) \
	$(CACHE)

#-------------------------- ast

ast	: ast/Makefile ast/ast.h ast/.libs/libast.a ast/.libs/libast_err.a ast/.libs/libast_pal.a ast/.libs/libast_cminpack.a

ast/Makefile :
	@echo ""
	@echo "*** $@ ***"
	cd ast; \
	./configure \
	$(ASTFLAGS) \
	--without-pthreads --enable-shared=no \
	star_cv_cnf_trail_type=long star_cv_cnf_f2c_compatible=no \
	$(CONFIGFLAGS) \
	$(PREFIX)
	cd ast; touch configure.ac aclocal.m4 configure Makefile.am Makefile.in

ast/ast.h :
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C ast -j ast.h
	$(MAKE) -C ast -j $(JOBS) libast.la
	$(MAKE) -C ast -j $(JOBS) libast_err.la
	$(MAKE) -C ast -j $(JOBS) libast_pal.la
	$(MAKE) -C ast -j $(JOBS) libast_cminpack.la
	cp ast/ast.h $(includedir)/.
	cp ast/src/grf.h $(includedir)/.
	cp ast/src/grf3d.h $(includedir)/.
	cp ast/.libs/libast.a $(libdir)/.
	cp ast/.libs/libast_err.a $(libdir)/.
	cp ast/.libs/libast_pal.a $(libdir)/.
	cp ast/.libs/libast_cminpack.a $(libdir)/.

ast/.libs/libast.a :
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C ast -j ast.h
	cp ast/ast.h $(includedir)/.
	cp ast/grf.h $(includedir)/.
	cp ast/grf3d.h $(includedir)/.

ast/.libs/libast_err.a :
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C ast -j $(JOBS) libast_err.la
	cp ast/.libs/libast_err.a $(libdir)/.

ast/.libs/libast_pal.a :
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C ast -j $(JOBS) libast_pal.la
	cp ast/.libs/libast_pal.a $(libdir)/.

ast/.libs/libast_cminpack.a :
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C ast -j $(JOBS) libast_cminpack.la
	cp ast/.libs/libast_cminpack.a $(libdir)/.

#-------------------------- funtools

funtools : funtools/libfuntools.a

# no parallel
funtools/libfuntools.a : funtools/Makefile
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C funtools lib
	cp funtools/funtools.h $(includedir)/.
	cp funtools/filter/*.h $(includedir)/.
	cp funtools/util/*.h $(includedir)/.
	cp funtools/fitsy/*.h $(includedir)/.
	cp funtools/libfuntools.a $(libdir)/.

funtools/Makefile : funtools/configure
	@echo ""
	@echo "*** $@ ***"
	cd funtools; \
	./configure \
	--with-zlib=$(libdir) \
	$(CONFIGFLAGS) $(TARGET) \
	$(PREFIX) \
	--disable-shared

funtools/configure :
	cd funtools; \
	./mkconfigure

#-------------------------- tksvg

tksvg	: tksvg/Makefile
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tksvg -j $(JOBS) install

tksvg/Makefile :
	@echo ""
	@echo "*** $@ ***"
	cd tksvg; \
	./configure \
	$(WITHTK) \
	$(PREFIX) \
	$(STD) \
	$(CACHE)

#-------------------------- tktable

# no parallel
tktable	: tktable/Makefile
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tktable install

tktable/Makefile :
	@echo ""
	@echo "*** $@ ***"
	cd tktable; \
	./configure \
	$(WITHTK) \
	$(PREFIX) \
	$(STD) \
	$(CACHE)

#-------------------------- language

.PHONY	: language

language:
	cd ds9; \
	grep 'msgcat::mc' library/*.tcl | tclsh8.6 util/mergedict.tcl da iso8859-1; \
	grep 'msgcat::mc' library/*.tcl | tclsh8.6 util/mergedict.tcl de iso8859-1; \
	grep 'msgcat::mc' library/*.tcl | tclsh8.6 util/mergedict.tcl es iso8859-1; \
	grep 'msgcat::mc' library/*.tcl | tclsh8.6 util/mergedict.tcl fr iso8859-1; \
	grep 'msgcat::mc' library/*.tcl | tclsh8.6 util/mergedict.tcl pt iso8859-1; \
	grep 'msgcat::mc' library/*.tcl | tclsh8.6 util/mergedict.tcl cs iso8859-2; \
	grep 'msgcat::mc' library/*.tcl | tclsh8.6 util/mergedict.tcl ja euc-jp; \
	grep 'msgcat::mc' library/*.tcl | tclsh8.6 util/mergedict.tcl zh big5

#-------------------------- tidy

.PHONY	: tidy

tidy	:
	$(call Tidy,ds9/doc)
	$(call Tidy,ds9/doc/release)
	$(call Tidy,ds9/doc/ref)

#-------------------------- scrub

.PHONY	: scrub

scrub :
	find . -name "*[~#]" -exec rm {} \;
	find . -d -name "autom4te.cache" -exec rm -rf {} \;

#-------------------------- clean

.PHONY	: clean
.PHONY	: dirsclean ds9clean ds9parsersclean localclean
.PHONY	: opensslclean
.PHONY	: tclxmlclean tclxmlrpcclean tlsclean
.PHONY	: tkbltclean tclfitsy tkconclean tkhtml1clean tkimgclean
.PHONY	: tkmpegclean tksvgclean tkagifclean
.PHONY	: tktableclean xpaclean
.PHONY	: vectorclean fitsyclean astclean funtoolsclean
.PHONY	: tksaoclean

clean	: tclclean tkclean \
	opensslclean \
	tlsclean tclxmlclean tclxmlrpc xpaclean \
	tkbltclean tclfitsy tkconclean tktableclean tkimgclean \
	tkmpegclean tksvgclean tkagifclean tkhtml1clean \
	vectorclean fitsyclean astclean funtoolsclean ds9clean \
	tksaoclean osclean dirsclean localclean scrub

dirsclean :
	@echo ""
	@echo "*** $@ ***"
	for d in $(DIRS); do rm -rf $$d; done

ds9clean :
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C ds9/$(OS) clean

ds9parsersclean :
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C ds9/$(OS) parsersclean

localclean :
	@echo ""
	@echo "*** $@ ***"
	-rm -f core *~ *#

opensslclean:
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C openssl clean

tclfitsyclean :
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tclfitsy clean

tclxmlclean:
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tclxml clean

tclxmlrpcclean:
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tclxmlrpc clean

tlsclean:
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tls clean

tkbltclean:
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tkblt clean

tkconclean:
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tkcon clean

tkhtml1clean:
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tkhtml1 clean

tkimgclean:
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tkimg clean

tkmpegclean:
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tkmpeg clean

tksvgclean:
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tksvg clean

tkagifclean:
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tkagif clean

tksaoclean :
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tksao clean

vectorclean :
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C vector clean

fitsyclean :
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C fitsy clean

astclean :
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C ast clean

funtoolsclean :
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C funtools clean

tktableclean:
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tktable clean

xpaclean :
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C xpa clean

#-------------------------- distclean

.PHONY	: dirsdistclean ds9distclean localdistclean
.PHONY	: openssldistclean
.PHONY	: tclxmldistclean tclxmlrpcdistclean tlsdistclean
.PHONY	: tkbltdistclean tclfitsydistclean tkcondistclean tkhtml1distclean 
.PHONY	: tkimgdistclean tkmpegdistclean tksvgdistclean tkagifdistclean 
.PHONY	: tktabledistclean xpadistclean
.PHONY	: vectordistclean fitsydistclean
.PHONY	: astdistclean funtoolsdistclean
.PHONY	: tksaodistclean

distclean : tcldistclean tkdistclean \
	  openssldistclean \
	  tclsignaldistclean \
	  tlsdistclean tclxmldistclean tclxmlrpcdistclean xpadistclean \
	  tkbltdistclean tclfitsydistclean tktabledistclean \
	  tkcondistclean tkimgdistclean \
	  tkmpegdistclean tksvgdistclean tkagifdistclean tkhtml1distclean \
	  vectordistclean fitsydistclean astdistclean \
	  vectordistclean fitsydistclean astdistclean \
	  funtoolsdistclean ds9distclean \
	  tksaodistclean osdistclean dirsdistclean localdistclean scrub

dirsdistclean:
	@echo ""
	@echo "*** $@ ***"
	for d in $(DIRS); do rm -rf $$d; done

ds9distclean :
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C ds9/$(OS) distclean

localdistclean :
	@echo ""
	@echo "*** $@ ***"
	-rm -f core *~ *#
	-rm -f config.log config.status config.cache Makefile 

openssldistclean :
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C openssl clean
	rm -f openssl/Makefile
	rm -rf openssl/include
	rm -f openssl/crypto/opensslconf.h
	rm -f openssl/apps/CA.pl
	rm -f openssl/tools/c_rehash
	find openssl/test -type l -exec rm {} \;
	find openssl/apps -type l -exec rm {} \;
	find openssl/certs/demo -type l -exec rm {} \;

tclfitsydistclean :
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tclfitsy distclean
	rm -f tclfitsy/tclfitsyConfig.sh

tclxmldistclean:
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tclxml distclean
	rm -f tclxml/tclxmlConfig.sh
	rm -f tclxml/include/tclxml/tclxml.h

tclxmlrpcdistclean:
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tclxmlrpc distclean
	rm -f tclxmlrpc/tclxmlrpcConfig.sh

tlsdistclean:
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tls distclean
	rm -f tls/tlsConfig.sh

tkcondistclean:
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tkcon distclean

tkbltdistclean:
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tkblt distclean
	rm -f tkblt/tkbltConfig.sh

tkhtml1distclean:
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tkhtml1 distclean
	rm -f tkhtml1/tkhtml1Config.sh

tkimgdistclean: 
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tkimg distclean
	find tkimg -name "pkgIndex.tcl" -exec rm {} \;		
	find tkimg -name "*Config.sh" -exec rm {} \;		
	rm -rf $(TKIMGTMPFILES)

tkmpegdistclean:
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tkmpeg distclean
	rm -f tkmpeg/tkmpegConfig.sh

tksvgdistclean:
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tksvg distclean
	rm -f tksvg/tksvgConfig.sh

tkagifdistclean:
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tkagif distclean
	rm -f tkagif/tkagifConfig.sh

tksaodistclean :
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tksao distclean
	rm -f tksao/tksaoConfig.sh

astdistclean :
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C ast distclean

vectordistclean :
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C vector distclean
	rm -f vector/vectorConfig.sh

fitsydistclean :
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C fitsy distclean
	rm -f fitsy/fitsyConfig.sh

funtoolsdistclean :
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C funtools distclean

tktabledistclean:
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tktable distclean
	rm -f tktable/tktableConfig.sh

xpadistclean :
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C xpa distclean

