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

CFLAGS= $(CCOPT) -I. -I../include -I$(X11INCLUDE0)
CXXFLAGS = $(CXXOPT) -I. -I../include -I$(X11INCLUDE0)

MAINDIR	= ../$(TKDIR)/unix
MAIN	= tkAppInit

OBJS	= ds9.o $(MAIN).o

LIBS	= \
	../lib/libsaotk.a \
	../lib/libtkhtml.a \
	../lib/$(TKMPEGDIR)/libtkmpeg1.0.a \
	../lib/$(TCLXMLVER)/libTclxml3.2.a \
	../lib/$(TKTABLEVER)/libTktable2.10.a \
	../lib/$(TKIMGVER)/libtkimgpng1.4.a \
	../lib/$(TKIMGVER)/libpngtcl1.4.3.a \
	../lib/$(TKIMGVER)/libtkimgtiff1.4.a \
	../lib/$(TKIMGVER)/libtifftcl3.9.4.a \
	../lib/$(TKIMGVER)/libtkimgjpeg1.4.a \
	../lib/$(TKIMGVER)/libjpegtcl8.2.a \
	../lib/$(TKIMGVER)/libtkimggif1.4.a \
	../lib/$(TKIMGVER)/libtkimgwindow1.4.a \
	../lib/$(TKIMGVER)/libzlibtcl1.2.5.a \
	../lib/$(TKIMGVER)/libtkimg1.4.a \
	../lib/libtiff.a \
	../lib/libfuntools.a \
	../lib/librice.a \
	../lib/libhcomp.a \
	../lib/libplio.a \
	../lib/libast.a \
	../lib/libast_err.a \
	../lib/libast_pal.a \
	../lib/libwcs.a \
	../lib/libxpa.a \
	../lib/$(TCLIISDIR)/libtcliis1.0.a \
	../lib/$(TCLCHECKDNSDIR)/libtclcheckdns1.1.a \
	../lib/libsignal_ext.a \
	../lib/$(TKBLTDIR)/libtkblt3.0.a \
	../lib/$(TKMACOSXDIR)/libtkmacosx1.0.a \
	../lib/libtkstub8.6.a \
	../lib/libtclstub8.6.a

APPDIR	= ../bin/SAOImage\ DS9.app
SAOTKFW	= ../bin/SAOImage\ DS9.app/Contents/Frameworks/Saotk.framework
SAOTKRES= ../bin/SAOImage\ DS9.app/Contents/Frameworks/Saotk.framework/Resources
TCLFW	= ../bin/SAOImage\ DS9.app/Contents/Frameworks/Tcl.framework
TCLRES	= ../bin/SAOImage\ DS9.app/Contents/Frameworks/Tcl.framework/Resources
TKFW	= ../bin/SAOImage\ DS9.app/Contents/Frameworks/Tk.framework
TKRES	= ../bin/SAOImage\ DS9.app/Contents/Frameworks/Tk.framework/Resources

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

ds9	: exec appdir framework tcl tk tcllib tkcon xmlrpc tkblt xpa compiler
	cp -p ds9 $(APPDIR)/Contents/MacOS/.
	strip $(APPDIR)/Contents/MacOS/ds9
	install_name_tool -change \
	/Library/Frameworks/Tcl.framework/Versions/8.6/Tcl \
	@executable_path/../Frameworks/Tcl.framework/Tcl \
	$(APPDIR)/Contents/MacOS/ds9
	install_name_tool -change \
	/Library/Frameworks/Tk.framework/Versions/8.6/Tk \
	@executable_path/../Frameworks/Tk.framework/Tk \
	$(APPDIR)/Contents/MacOS/ds9
	cd ../bin; $(CODESIGN) --deep -f -s "SAOImage DS9" SAOImage\ DS9.app

debug	: exec appdir framework tcl_debug tk_debug tcllib tkcon xmlrpc tkblt xpa compiler
	cp -p ds9 $(APPDIR)/Contents/MacOS/.
	install_name_tool -change \
	/Library/Frameworks/Tcl.framework/Versions/8.6/Tcl \
	@executable_path/../Frameworks/Tcl.framework/Tcl \
	$(APPDIR)/Contents/MacOS/ds9
	install_name_tool -change \
	/Library/Frameworks/Tk.framework/Versions/8.6/Tk \
	@executable_path/../Frameworks/Tk.framework/Tk \
	$(APPDIR)/Contents/MacOS/ds9
	cd ../bin; $(CODESIGN) --deep -f -s "SAOImage DS9" SAOImage\ DS9.app

exec	: $(OBJS) $(LIBS)
	$(RM) ds9
	$(CXX) ${OPTS} \
	-o ds9 $(OBJS) $(LIBS) \
	-lxml2 \
	-lstdc++ -liconv \
	-F../build/tcl -framework Tcl \
	-F../build/tk -framework Tk \
	-framework CoreFoundation -framework Cocoa \
	-framework Carbon -framework IOKit

appdir	: FORCE
	rm -rf $(APPDIR)
	cp -rp macosx/SAOImage\ DS9.app ../bin/.

framework: FORCE
	cp -pr ../src $(SAOTKRES)/.
	cp -pr ../msgs $(SAOTKRES)/.
	cp -pr ../doc $(SAOTKRES)/.
	cp -pr ../cmaps $(SAOTKRES)/.
	cp -pr ../template $(SAOTKRES)/.

	rm $(SAOTKRES)/src/source.tcl
	cd $(APPDIR); find -d . -name CVS -exec rm -rf {} \;
	cd $(APPDIR); find . -name keepme -exec rm -rf {} \;
	echo "pkg_mkIndex $(SAOTKRES)/src *.tcl; exit" | ../bin/tclsh8.6

tcl	: FORCE
	cp -p ../build/tcl/Tcl.framework/Versions/8.6/Tcl $(TCLFW)/.
	cp -pr ../build/tcl/Tcl.framework/Versions/8.6/Resources/* $(TCLRES)/.

tcl_debug: FORCE
	cp -p ../build/tcl/Tcl.framework/Versions/8.6/Tcl_debug $(TCLFW)/Tcl
	cp -pr ../build/tcl/Tcl.framework/Versions/8.6/Resources/* $(TCLRES)/.

tk	: FORCE
	cp -p ../build/tk/Tk.framework/Versions/8.6/Tk $(TKFW)/.
	cp -p ../build/tk/Tk.framework/Versions/8.6/Resources/Info.plist \
	$(TKRES)/.
	cp -p ../build/tk/Tk.framework/Versions/8.6/Resources/license.terms \
	$(TKRES)/.
	cp -rp ../build/tk/Tk.framework/Versions/8.6/Resources/Scripts \
	$(TKRES)/.
tk_debug: FORCE
	cp -p ../build/tk/Tk.framework/Versions/8.6/Tk_debug $(TKFW)/Tk
	cp -p ../build/tk/Tk.framework/Versions/8.6/Resources/Info.plist \
	$(TKRES)/.
	cp -p ../build/tk/Tk.framework/Versions/8.6/Resources/license.terms \
	$(TKRES)/.
	cp -rp ../build/tk/Tk.framework/Versions/8.6/Resources/Scripts \
	$(TKRES)/.

tcllib	: FORCE
	mkdir $(SAOTKRES)/$(TCLLIBVER)
	cp -pr ../$(TCLLIBDIR)/modules/base64 $(SAOTKRES)/$(TCLLIBVER)/.
	cp -pr ../$(TCLLIBDIR)/modules/ftp $(SAOTKRES)/$(TCLLIBVER)/.
	cp -pr ../$(TCLLIBDIR)/modules/log $(SAOTKRES)/$(TCLLIBVER)/.
	cp -pr ../$(TCLLIBDIR)/modules/textutil $(SAOTKRES)/$(TCLLIBVER)/.
	cp -pr ../$(TCLLIBDIR)/modules/math $(SAOTKRES)/$(TCLLIBVER)/.

tkcon	: FORCE
	mkdir $(SAOTKRES)/$(TKCONVER)
	cp -p ../$(TKCONDIR)/*.tcl $(SAOTKRES)/$(TKCONVER)/.

xmlrpc	: FORCE
	mkdir $(SAOTKRES)/$(XMLRPCVER)
	cp -p ../$(XMLRPCDIR)/xmlrpc.tcl $(SAOTKRES)/$(XMLRPCVER)/.
	echo "pkg_mkIndex $(SAOTKRES)/$(XMLRPCVER) *.tcl; exit" | ../bin/tclsh8.6

tkblt	: FORCE
	mkdir $(SAOTKRES)/$(TKBLTDIR)
	cp -p ../$(TKBLTDIR)/library/graph.tcl $(SAOTKRES)/$(TKBLTDIR)/.
	echo "pkg_mkIndex $(SAOTKRES)/tkblt3.0 *.tcl; exit" | ../bin/tclsh8.6

xpa	: FORCE
	cp -pr ../bin/xpans $(SAOTKRES)/.

compiler: FORCE
	cp -p ../compilers/$(FILTERCOMPILER) $(SAOTKRES)/.

#--------------------------cleanup

distclean: FORCE
	$(RM) core *~ *# ds9Base ds9 ds9.C *.o


