DS9CMD		= @DS9CMD@
DEBUGCMD	= @DEBUGCMD@

CC		= @CC@
CXX		= @CXX@
CFLAGS_DEFAULT	= @CFLAGS_DEFAULT@
CFLAGS_WARNING	= @CFLAGS_WARNING@
EXEEXT		= @EXEEXT@
OBJEXT		= @OBJEXT@
INCLUDES	= @PKG_INCLUDES@ @TCL_INCLUDES@ @TK_INCLUDES@ @TK_XINCLUDES@
PKG_CFLAGS	= @PKG_CFLAGS@
DEFS		= @DEFS@ $(PKG_CFLAGS)
CPPFLAGS	= @CPPFLAGS@
LIBS		= @PKG_LIBS@ @LIBS@
CFLAGS		= @CFLAGS@
COMPILE		= $(CC) $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CFLAGS)

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@

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

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

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

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

include ../../make.pkgs

ZDIR	= zipdir/zvfsmntpt
OBJS	= ds9.o tkAppInit.o

FILES	= \
	$(ZDIR)/$(TCLVER) \
	$(ZDIR)/tcl8 \
	$(ZDIR)/$(TKVER) \
	$(ZDIR)/$(TKBLTDIR) \
	$(ZDIR)/$(TCLLIBVER) \
	$(ZDIR)/$(TKCONVER) \
	$(ZDIR)/$(XMLRPCVER) \
	$(ZDIR)/library \
	$(ZDIR)/msgs \
	$(ZDIR)/doc \
	$(ZDIR)/cmaps \
	$(ZDIR)/template

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/$(TKSAODIR)/libtksao1.0.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/$(TCLZVFSDIR)/libtclzvfs1.0.a \
	../../lib/libxpa.a \
	../../lib/$(TCLIISDIR)/libtcliis1.0.a \
	../../lib/$(TCLCHECKDNSDIR)/libtclcheckdns1.1.a \
	../../lib/$(TCLSIGNALDIR)/libtclsignal1.4.a \
	../../lib/$(TKBLTDIR)/libtkblt3.0.a

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

all: $(DS9CMD)

debug: $(DEBUGCMD)

tkAppInit.o : tkAppInit.c
	$(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 .

ds9	: ds9Base ds9.zip
	$(RM) $@
	strip ds9Base
	cat ds9Base ds9.zip > ds9$(EXEEXT)
	zip -A ds9$(EXEEXT)
	chmod 755 ds9$(EXEEXT)
	cp ds9$(EXEEXT) ../../bin/.

dds9	: ds9Base ds9.zip
	$(RM) $@
	cat ds9Base ds9.zip > ds9$(EXEEXT)
	zip -A ds9$(EXEEXT)
	chmod 755 ds9$(EXEEXT)
	cp ds9$(EXEEXT) ../../bin/.

ds9zip	: ds9Base ds9.zip
	$(RM) $@
	strip ds9Base
	cp ds9Base ds9
	cp ds9 ../../bin/.
	cp ds9.zip ../../bin/.
	cd ../../bin; codesign -s "SAOImage DS9" ds9

dds9zip: ds9Base ds9.zip
	$(RM) $@
	cp ds9Base ds9
	cp ds9 ../../bin/.
	cp ds9.zip ../../bin/.
	cd ../../bin; codesign -s "SAOImage DS9" ds9

ds9Base	: $(OBJS) $(LLIBS)
	$(RM) $@
	$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ \
	$(OBJS) \
	$(LLIBS) \
	$(LIBS) \
	$(TK_STUB_LIB_SPEC) $(TK_LIB_SPEC) \
	$(TCL_STUB_LIB_SPEC) $(TCL_LIB_SPEC) \
	$(TK_LIBS)

ds9.zip	: $(FILES)
	cd zipdir; find . -depth -name "CVS" -exec rm -r {} \;
	cd zipdir; zip -r ../ds9.zip *	

zipdir	: 
	mkdir zipdir
	mkdir $(ZDIR)

$(ZDIR)/$(TCLVER) : zipdir ../../lib/$(TCLVER)
	$(RM) -r $@
	cp -r ../../lib/$(TCLVER) $(ZDIR)/.

$(ZDIR)/tcl8 : zipdir ../../lib/tcl8
	$(RM) -r $@
	cp -r ../../lib/tcl8 $(ZDIR)/.

$(ZDIR)/$(TKVER) : zipdir ../../lib/$(TKVER)
	$(RM) -r $@
	cp -r ../../lib/$(TKVER) $(ZDIR)/.
	rm -rf $(ZDIR)/$(TKVER)/images
	rm -rf $(ZDIR)/$(TKVER)/demos

$(ZDIR)/$(TKBLTDIR) : zipdir
	$(RM) -r $@
	mkdir $(ZDIR)/$(TKBLTDIR)
	cp ../../$(TKBLTDIR)/library/graph.tcl $(ZDIR)/$(TKBLTDIR)/.

$(ZDIR)/$(TCLLIBVER) : zipdir
	$(RM) -r $@
	mkdir $(ZDIR)/$(TCLLIBVER)
	cp -r ../../$(TCLLIBDIR)/modules/base64 $(ZDIR)/$(TCLLIBVER)/.
	cp -r ../../$(TCLLIBDIR)/modules/ftp $(ZDIR)/$(TCLLIBVER)/.
	cp -r ../../$(TCLLIBDIR)/modules/log $(ZDIR)/$(TCLLIBVER)/.
	cp -r ../../$(TCLLIBDIR)/modules/textutil $(ZDIR)/$(TCLLIBVER)/.
	cp -r ../../$(TCLLIBDIR)/modules/math $(ZDIR)/$(TCLLIBVER)/.

$(ZDIR)/$(TKCONVER) : zipdir ../../lib/$(TKCONVER)
	$(RM) -r $@
	cp -r ../../lib/$(TKCONVER) $@

$(ZDIR)/$(XMLRPCVER) : zipdir ../../lib/$(XMLRPCVER)
	$(RM) -r $@
	cp -r ../../lib/$(XMLRPCVER) $@

$(ZDIR)/library : zipdir ../library/*.tcl
	$(RM) -r $@
	cp -r ../library $(ZDIR)/.

$(ZDIR)/msgs : zipdir ../msgs/*
	$(RM) -r $@
	cp -r ../msgs $(ZDIR)/.

$(ZDIR)/doc : zipdir ../doc/* ../doc/ref/* ../doc/user/* ../doc/release/*
	$(RM) -r $@
	cd ..; find doc -name "*.html" | cpio -pdmuv unix/$(ZDIR)
	cd ..; find doc -name "*.gif" | cpio -pdmuv unix/$(ZDIR)
	cd ..; find doc -name "*.png" | cpio -pdmuv unix/$(ZDIR)

$(ZDIR)/cmaps : zipdir ../cmaps/*
	$(RM) -r $@
	cp -r ../cmaps $(ZDIR)/.

$(ZDIR)/template : zipdir ../template/*
	$(RM) -r $@
	cd ..; find template -name "*.tpl" | cpio -pdmuv unix/$(ZDIR)

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

distclean: clean
	-rm -f Makefile config.cache config.log config.status

clean	: FORCE
	cd ../library; $(MAKE) clean
	$(RM) core *~ *# ds9Base ds9 *.zip *.o
	$(RM) -r zipdir

FORCE	:

