TCLDIRDIR= $(TCLDIR)/macosx
TKDIRDIR= $(TKDIR)/macosx

XPAFLAGS = --with-x=disabled --enable-posix_spawn
FUNTOOLSFLAGS = --enable-posix_spawn

TCLFILES = \
	 $(TCLDIR)/generic/tcl.h \
	 $(TCLDIR)/generic/tclDecls.h \
	 $(TCLDIR)/generic/tclPlatDecls.h

TKFILES = \
	 $(TKDIR)/generic/tk.h \
	 $(TKDIR)/generic/tkDecls.h \
	 $(TKDIR)/generic/tkPlatDecls.h \
	 $(TKDIR)/generic/tkIntXlibDecls.h \
	 $(TKDIR)/macosx/tkMacOSX.h

all: 	dirs \
	tcl tk \
	tktable tkcon xmlrpc tkblt \
	tclxml tkimg tkmpeg tkhtml \
	xpa tcliis tclcheckdns signal funtools \
	ast wcssubs \
	rice hcompress plio \
	tkmacosx \
	saotk ds9

tcl	: FORCE
	@echo "Installing Tcl..."
	cd $(TCLDIRDIR); CC='$(CC)' CFLAGS='$(OPTS)' \
	./configure \
	--enable-64bit \
	--enable-symbols
	$(MAKE) -C $(TCLDIRDIR) -j $(JOBS)

	# install include, lib
	cp -p $(TCLFILES) include/.
	cp -p build/tcl/Tcl.framework/libtclstub8.6.a lib/.
	cp -rp build/tcl/Tcl.framework/Versions/8.6/Resources/Scripts lib/tcl8.6
	cp -rp build/tcl/Tcl.framework/Versions/8.6/Resources/tcl8 lib/.

	# we need a working tclsh later
	cp -p build/tcl/tclsh8.6 bin/.
	install_name_tool -change /Library/Frameworks/Tcl.framework/Versions/8.6/Tcl @executable_path/../build/tcl/Tcl.framework/Tcl bin/tclsh8.6

tk	: FORCE
	@echo "Installing Tk..."
	cd $(TKDIRDIR); CC='$(CC)' CFLAGS='$(OPTS)' \
	./configure \
	--with-tcl=$(root)/$(TCLDIRDIR) \
	--enable-64bit \
	--enable-symbols
	$(MAKE) -C $(TKDIRDIR) -j $(JOBS)

	# install include, lib
	cp -p $(TKFILES) include/.
	cp -p build/tk/Tk.framework/libtkstub8.6.a lib/.
	cp -rp build/tk/Tk.framework/Versions/8.6/Resources/Scripts lib/tk8.6

	# fix wish8.6
	cp -p build/tk/wish8.6 bin/.
	install_name_tool -change /Library/Frameworks/Tcl.framework/Versions/8.6/Tcl @executable_path/../build/tcl/Tcl.framework/Tcl bin/wish8.6
	install_name_tool -change /Library/Frameworks/Tk.framework/Versions/8.6/Tk @executable_path/../build/tk/Tk.framework/Tk bin/wish8.6

tkblt	: FORCE
	@echo "Installing Tkblt..."
	cd $(TKBLTDIR); CC='$(CC)' CFLAGS='$(OPTS)' \
	./configure \
	--with-tcl=$(root)/build/tcl/Development \
	--with-tk=$(root)/build/tk/Development \
	--prefix $(root) \
	--exec-prefix $(root) \
	--enable-symbols
	$(MAKE) -C $(TKBLTDIR) -j $(JOBS) install

tktable	: FORCE
	@echo "Installing Tktable..."
	cd $(TKTABLEDIR); CC='$(CC)' CFLAGS='$(OPTS)' \
	./configure \
	--with-tcl=$(root)/build/tcl/Development \
	--with-tk=$(root)/build/tk/Development \
	--prefix $(root) \
	--exec-prefix $(root) \
	--disable-shared \
	--enable-symbols
	$(MAKE) -C $(TKTABLEDIR) -j $(JOBS)
	$(MAKE) -C $(TKTABLEDIR) -j $(JOBS) install

tclxml	: FORCE
	@echo "Installing Tclxml..."
	cd $(TCLXMLDIR); CC='$(CC)' CFLAGS='$(OPTS)' \
	./configure \
	--with-tcl=$(root)/build/tcl/Development \
	--prefix $(root) \
	--exec-prefix $(root) \
	--disable-shared \
	--with-xml-static=1
	$(MAKE) -C $(TCLXMLDIR) -j $(JOBS)
	$(MAKE) -C $(TCLXMLDIR) -j $(JOBS) install

tkimg	: libtiff
	@echo "Installing Tkimg..."
	cd $(TKIMGDIR); \
	CC='$(CC)' CFLAGS='$(OPTS) -DPNG_NO_WRITE_gAMA' \
	./configure \
	--with-tcl=$(root)/build/tcl/Development \
	--with-tk=$(root)/build/tk/Development \
	--prefix $(root) \
	--exec-prefix $(root) \
	--disable-shared
	$(MAKE) -C $(TKIMGDIR) -j $(JOBS)
	$(MAKE) -C $(TKIMGDIR) -j $(JOBS) install

tkmpeg	: FORCE
	@echo "Installing Tkmpeg..."
	cd $(TKMPEGDIR); CC='$(CC)' CFLAGS='$(OPTS)' \
	./configure \
	--with-tcl=$(root)/build/tcl/Development \
	--with-tk=$(root)/build/tk/Development \
	--prefix $(root) \
	--exec-prefix $(root) \
	--enable-symbols
	$(MAKE) -C $(TKMPEGDIR) -j $(JOBS) install

tkhtml	: $(HTMLDIR)
	@echo "Installing Tkhtml..."
	cd $(HTMLDIR); CC='$(CC)' CFLAGS='$(OPTS) -D_MACOSX' \
	config_TARGET_TCL_SCRIPT_DIR=$(root)/$(TCLDIRDIR) \
	config_TARGET_TK_SCRIPT_DIR=$(root)/$(TKDIRDIR) \
	config_TARGET_TCL_INC=$(root)/$(TCLDIR)/generic \
	config_TARGET_TK_INC=$(root)/$(TKDIR)/generic \
	config_TARGET_X_INC="-I$(root)/$(TKDIR)/xlib" \
	config_TARGET_X_LIBS=" " \
	$(root)/htmlwidget/configure \
	--with-tcl=$(root)/$(TCLDIRDIR) \
	--with-tk=$(root)/$(TKDIRDIR) \
	--prefix $(root) \
	--enable-shared=no
	$(MAKE) -C $(HTMLDIR) headers libtkhtml.a
	cp $(HTMLDIR)/libtkhtml.a lib/.

tcliis	: FORCE
	@echo "Installing Tcliis..."
	cd $(TCLIISDIR); CC='$(CC)' CFLAGS='$(OPTS)' \
	./configure \
	--with-tcl=$(root)/build/tcl/Development \
	--prefix $(root) \
	--exec-prefix $(root) \
	--enable-symbols
	$(MAKE) -C $(TCLIISDIR) -j $(JOBS) install

tclcheckdns : FORCE
	@echo "Installing Tclcheckdns..."
	cd $(TCLCHECKDNSDIR); CC='$(CC)' CFLAGS='$(OPTS)' \
	./configure \
	--with-tcl=$(root)/build/tcl/Development \
	--prefix $(root) \
	--exec-prefix $(root) \
	--enable-symbols
	$(MAKE) -C $(TCLCHECKDNSDIR) -j $(JOBS) install

tkmacosx	: FORCE
	@echo "Installing Tkmacosx..."
	cd $(TKMACOSXDIR); CC='$(CC)' CFLAGS='$(OPTS)' \
	./configure \
	--with-tcl=$(root)/build/tcl/Development \
	--with-tk=$(root)/build/tk/Development \
	--prefix $(root) \
	--exec-prefix $(root) \
	--enable-symbols
	$(MAKE) -C $(TKMACOSXDIR) -j $(JOBS) install

distclean : tclclean tkclean \
	tktableclean tkbltclean \
	tclxmlclean tkimgclean tkmpegclean tkhtmlclean \
	xpaclean tcliisclean tclcheckdnsclean signalclean funtoolsclean \
	astclean wcssubsclean \
	riceclean hcompressclean plioclean \
	tkmacosxclean \
	saotkclean zvfsclean ds9clean srcclean \
	filesclean dirsclean

tkmacosxclean : FORCE
	$(MAKE) -C $(TKMACOSXDIR) distclean

#--------------------------distribution

ifdef CVS_SERVER

#--------------------------remote

dist : ds9dmg
	scp dist/$(DS9APP).dmg $(USER)@$(DEST):build/$(ARCH)/.
	ssh $(DEST) -l $(USER) 'rm -rf build/$(ARCH)/SAOImage\ DS9.app'
	scp -rp dist/$(DS9APP)/SAOImage\ DS9.app $(USER)@$(DEST):build/$(ARCH)/.

#--------------------------remote

else

#--------------------------local

dist : ds9dmg
	cp dist/$(DS9APP).dmg $(HOME)/build/$(ARCH)/.
	rm -rf $(HOME)/build/$(ARCH)/SAOImage\ DS9.app
	cp -rp dist/$(DS9APP)/SAOImage\ DS9.app $(HOME)/build/$(ARCH)/.

#--------------------------local

endif

ds9dmg	: FORCE
	rm -rf dist/$(DS9APP)
	rm -rf dist/$(DS9APP).dmg
	mkdir dist/$(DS9APP)
	ln -s /Applications dist/$(DS9APP)/.
	cp -r ds9/macosx/README dist/$(DS9APP)/.
	cp -rp bin/SAOImage\ DS9.app dist/$(DS9APP)/.
	hdiutil create -srcfolder dist/$(DS9APP) dist/$(DS9APP) 

