OS	= @OS@
ARCH	= @ARCH@
ROOT	= @ROOT@
JOBS	= 4

include ./make.include

#--------------------------defines

DS9APP	=SAOImage\ DS9\ $(DS9VERSION)

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

WITHTCL =--with-tcl=$(ROOT)/build/tcl/Development
WITHTK  =--with-tk=$(ROOT)/build/tk/Development $(WITHTCL)

#--------------------------build

.PHONY	: saods9 tcl tk tkmacosx

saods9	: dirs tcl tk \
	tclcheckdns tcliis tclsignal \
	tclzvfs tclxml xmlrpc xpa \
	tkblt tktable tkimg \
	tkmpeg tkhtml tkcon \
	tkmacosx \
	tksao ds9

tcl	: $(TCLDIR)/macosx/Makefile
	@echo ""
	@echo "*** Install $(TCLDIR) ***"
	$(MAKE) -C $(TCLDIR)/macosx -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

$(TCLDIR)/macosx/Makefile :
	@echo ""
	@echo "*** Configure $(TCLDIR) ***"
	cd $(TCLDIR)/macosx; \
	./configure \
	--enable-64bit \
	--enable-symbols \
	--config-cache --cache-file=../../config.cache

tk	: $(TKDIR)/macosx/Makefile
	@echo ""
	@echo "*** Installing $(TKDIR) ***"
	$(MAKE) -C $(TKDIR)/macosx -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

$(TKDIR)/macosx/Makefile :
	@echo ""
	@echo "*** Configure $(TKDIR) ***"
	cd $(TKDIR)/macosx; \
	./configure \
	$(WITHTCL) \
	--enable-64bit \
	--enable-symbols \
	--config-cache --cache-file=../../config.cache

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

$(TKMACOSXDIR)/Makefile :
	@echo ""
	@echo "*** Configure $(TKMACOSXDIR) ***"
	cd $(TKMACOSXDIR); \
	./configure \
	$(WITHTK) \
	--prefix $(ROOT) --exec-prefix $(ROOT) \
	--disable-shared --enable-symbols \
	--config-cache --cache-file=../../config.cache

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

.PHONY	: tkmacosxclean

tkmacosxclean :
	$(MAKE) -C $(TKMACOSXDIR) clean

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

.PHONY	: tkmacosxdistclean

tkmacosxdistclean :
	$(MAKE) -C $(TKMACOSXDIR) distclean

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

.PHONY	: dist

dist	:
	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) 
	rm -rf dist/$(DS9APP)
