# tcl 8.6.8
# tk 8.6.8
# tcliis 1.0
# tcllib 1.18
# tclsignal 1.4
# tclxml 3.2
# tclzvfs 1.0
# tls 1.6.7
# tkblt 3.2
# tkcon 2.7
# tkhtml1 1.0
# tkimg 1.4.6
# tkmpeg 1.0
# tktable 2.10

# ast 8.4.0
# wcssubs 3.9.0
# funtools 1.4.7
# zlib 1.2.8
# xpa 2.1.18+
# libxml2 2.9.4
# libxslt 1.1.29
# openssl 1.0.2k

DS9_VERSION	= 7.6rc
XPA_VERSION	= 2.1.17
TCL_VERSION	= 8.6

#-------------------------- 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 tcliis tclsignal tclxml tclzvfs tls xpa
.PHONY	: tkblt tkcon tkhtml1 tkimg tkmpeg tksao tktable
.PHONY	: ast funtools

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) \
	$(PREFIX) \
	$(STD)

#-------------------------- tcliis

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

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

#-------------------------- tclsignal

tclsignal: tclsignal/Makefile
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tclsignal install

tclsignal/Makefile :
	@echo ""
	@echo "*** $@ ***"
	cd tclsignal; \
	./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) \
	$(TCLXMLFLAGS) \
	$(PREFIX) \
	$(STD) \
	$(CACHE)

#-------------------------- tclzvfs

tclzvfs	: tclzvfs/Makefile
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tclzvfs install

tclzvfs/Makefile :
	@echo ""
	@echo "*** $@ ***"
	cd tclzvfs; \
	./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 \
	$(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/src/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/png -j $(JOBS) install
	$(MAKE) -C tkimg/libpng -j $(JOBS) install
	$(MAKE) -C tkimg/gif -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 \
	CFLAGS='-DPNG_NO_WRITE_gAMA' \
	$(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)

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

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

tksao/Makefile :
	@echo ""
	@echo "*** $@ ***"
	cd tksao; \
	./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/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) \
	--disable-shared
	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
	cp ast/ast.h $(includedir)/.
	cp ast/grf.h $(includedir)/.
	cp ast/grf3d.h $(includedir)/.
	cp ast/.libs/libast.a $(libdir)/.
	cp ast/.libs/libast_err.a $(libdir)/.
	cp ast/.libs/libast_pal.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)/.

#-------------------------- 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) --with-wcslib=$(libdir) \
	$(CONFIGFLAGS) $(TARGET) \
	$(PREFIX) \
	--disable-shared

funtools/configure :
	cd funtools; \
	./mkconfigure

#-------------------------- 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


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

.PHONY	: scrub

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

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

.PHONY	: clean
.PHONY	: dirsclean ds9clean localclean
.PHONY	: tcliisclean tclsignalclean 
.PHONY	: tclxmlclean tclzvfsclean tlsclean
.PHONY	: tkbltclean tkconclean tkhtml1clean tkimgclean
.PHONY	: tkmpegclean tksaoclean astclean funtoolsclean tktableclean xpaclean

clean	: tclclean tkclean \
	tcliisclean tclsignalclean \
	tclzvfsclean tlsclean tclxmlclean xpaclean \
	tkbltclean tkconclean tktableclean tkimgclean \
	tkmpegclean tkhtml1clean \
	tksaoclean astclean funtoolsclean ds9clean \
	tkosclean dirsclean localclean scrub

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

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

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

tcliisclean :
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tcliis clean

tclsignalclean :
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tclsignal clean

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

tclzvfsclean:
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tclzvfs 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

tksaoclean :
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tksao 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	: tcliisdistclean tclsignaldistclean 
.PHONY	: tclxmldistclean tclzvfsdistclean tlsdistclean
.PHONY	: tkbltdistclean tkcondistclean tkhtml1distclean 
.PHONY	: tkimgdistclean tkmpegdistclean 
.PHONY	: tksaodistclean astdistclean funtoolsdistclean
.PHONY	: tktabledistclean xpadistclean

distclean : tcldistclean tkdistclean \
	tcliisdistclean tclsignaldistclean \
	tclzvfsdistclean tlsdistclean tclxmldistclean xpadistclean \
	tkbltdistclean tktabledistclean tkcondistclean tkimgdistclean \
	tkmpegdistclean tkhtml1distclean \
	tksaodistclean astdistclean funtoolsdistclean ds9distclean \
	tkosdistclean 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 

tcliisdistclean :
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tcliis distclean

tclsignaldistclean :
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tclsignal distclean

tclxmldistclean:
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tclxml distclean

tclzvfsdistclean:
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tclzvfs distclean

tlsdistclean:
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tls distclean

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

tkbltdistclean:
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tkblt distclean

tkhtml1distclean:
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tkhtml1 distclean

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

tksaodistclean :
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tksao distclean

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

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

tktabledistclean:
	@echo ""
	@echo "*** $@ ***"
	$(MAKE) -C tktable distclean

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