include ../../make.pkgs

CC		= @CC@
CXX		= @CXX@
LIBS		= @LIBS@
OBJEXT		= @OBJEXT@
EXEEXT		= @EXEEXT@

TCL_LIB_SPEC	= @TCL_LIB_SPEC@
TCL_STUB_LIB_SPEC= @TCL_STUB_LIB_SPEC@
TK_LIBS		= @TK_LIBS@
TK_LIB_SPEC	= @TK_LIB_SPEC@
TK_STUB_LIB_SPEC= @TK_STUB_LIB_SPEC@

PKG_CFLAGS	= @PKG_CFLAGS@
DEFS		= @DEFS@ $(PKG_CFLAGS)
CPPFLAGS	= @CPPFLAGS@
COMPILE		= $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CFLAGS)

CPPFLAGS	= @CPPFLAGS@
CFLAGS		= @CFLAGS@

INCLUDES	= @PKG_INCLUDES@ @TCL_INCLUDES@ @TK_INCLUDES@ @TK_XINCLUDES@

.SUFFIXES: .c .C .$(OBJEXT)

.c.@OBJEXT@:
	$(CC) $(COMPILE) -c $< -o $@

.C.@OBJEXT@:
	$(CXX) $(COMPILE) -c $< -o $@

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

OBJS	= ds9.o tkAppInit.o

LLIBS	= \
	../../lib/$(TKSAODIR)/libtksao1.0.a \
	../../lib/$(TKSAODIR)/libfuntools.a \
	../../lib/$(TKSAODIR)/libast.a \
	../../lib/$(TKSAODIR)/libast_err.a \
	../../lib/$(TKSAODIR)/libast_pal.a \
	../../lib/$(TKHTMLDIR)/libtkhtml1.0.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/libxpa.a \
	../../lib/$(TCLIISDIR)/libtcliis1.0.a \
	../../lib/$(TCLCHECKDNSDIR)/libtclcheckdns1.1.a \
	../../lib/$(TCLSIGNALDIR)/libtclsignal1.4.a \
	../../lib/$(TKBLTDIR)/libtkblt3.0.a \
	../../lib/$(TKMACOSXDIR)/libtkmacosx1.0.a

APPDIR	= ../../bin/SAOImage\ DS9.app
TKSAOFW	= ../../bin/SAOImage\ DS9.app/Contents/Frameworks/Tksao.framework
TKSAORES= ../../bin/SAOImage\ DS9.app/Contents/Frameworks/Tksao.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	: ds9base appdir framework tcl tk tcllib tkcon xmlrpc tkblt xpa compiler
	cp -p ds9base $(APPDIR)/Contents/MacOS/ds9
	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	: ds9base appdir framework tcl tk tcllib tkcon xmlrpc tkblt xpa compiler
	cp -p ds9base $(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

tkAppInit.o : tkAppInit.c
	$(CC) $(COMPILE) -DTK_LOCAL_APPINIT=SAOAppInit \
	-DTK_LOCAL_MAIN_HOOK=SAOLocalMainHook -c tkAppInit.c -o $@

tkAppInit.c : ../../$(TKDIR)/unix/tkAppInit.c
	cp ../../$(TKDIR)/unix/tkAppInit.c .

ds9base	: $(OBJS) $(LLIBS)
	$(RM) $@
	$(CXX) -o $@ $(OBJS) \
	$(LLIBS) \
	$(LIBS) \
	$(TK_STUB_LIB_SPEC) $(TK_LIB_SPEC) \
	$(TCL_STUB_LIB_SPEC) $(TCL_LIB_SPEC) \
	$(TK_LIBS)

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

framework: FORCE
	cp -pr ../library $(TKSAORES)/.
	cp -pr ../msgs $(TKSAORES)/.
	cp -pr ../doc $(TKSAORES)/.
	cp -pr ../cmaps $(TKSAORES)/.
	cp -pr ../template $(TKSAORES)/.

	rm $(TKSAORES)/library/source.tcl
	cd $(APPDIR); find -d . -name CVS -exec rm -rf {} \;
	cd $(APPDIR); find . -name keepme -exec rm -rf {} \;
	echo "pkg_mkIndex $(TKSAORES)/library *.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 $(TKSAORES)/$(TCLLIBVER)
	cp -pr ../../$(TCLLIBDIR)/modules/base64 $(TKSAORES)/$(TCLLIBVER)/.
	cp -pr ../../$(TCLLIBDIR)/modules/ftp $(TKSAORES)/$(TCLLIBVER)/.
	cp -pr ../../$(TCLLIBDIR)/modules/log $(TKSAORES)/$(TCLLIBVER)/.
	cp -pr ../../$(TCLLIBDIR)/modules/textutil $(TKSAORES)/$(TCLLIBVER)/.
	cp -pr ../../$(TCLLIBDIR)/modules/math $(TKSAORES)/$(TCLLIBVER)/.

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

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

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

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

compiler: FORCE
	cp -p ../../compilers/pcc-i386-snowleopard.tar.gz $(TKSAORES)/.

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

clean	: FORCE
	cd ../library; $(MAKE) clean
	$(RM) core *~ *# 

distclean: FORCE
	cd ../library; $(MAKE) distclean
	-rm -f Makefile
	-rm -f config.cache config.log config.status
	$(RM) core *~ *# ds9Base ds9 *.o

FORCE	:

