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

CFLAGS= $(CCOPT) -I. -I../include -I$(X11INCLUDE)

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

ZDIR = zipdir/zvfsmntpt
OBJS	= ds9.o $(MAIN).o

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

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/libsaotk.a \
	../lib/libwcs.a \
	../lib/libzvfs.a \
	../lib/libxpa.a \
	../lib/$(TCLIISDIR)/libtcliis1.0.a \
	../lib/$(TCLCHECKDNSDIR)/libtclcheckdns1.1.a \
	../lib/libsignal_ext.a \
	../lib/$(TKBLTDIR)/libtkblt3.0.a \
	../lib/libtk8.6.a \
	../lib/libtkstub8.6.a \
	../lib/libtcl8.6.a \
	../lib/libtclstub8.6.a

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

all: ds9

#--------------------------linux

ifneq (,$(findstring linux,$(ARCH)))

CXXFLAGS = $(CXXOPT) -I. -I../include -I$(X11INCLUDE)

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

debug	: ds9Base ds9.zip
	$(RM) $@
	cat ds9Base ds9.zip > ds9
	zip -A ds9
	chmod 755 ds9

ds9Base	: $(OBJS) $(LIBS)
	$(RM) $@
	$(CXX) ${OPTS} \
	-o $@ $(OBJS) $(LIBS) \
	-L$(X11LIB) -lX11 -lXext -lXft -lXrender -lXss \
	-lxml2 \
	-lz -lpthread 

endif

#--------------------------darwin

ifneq (,$(findstring darwin,$(ARCH)))

CXXFLAGS = $(CXXOPT) -I. -I../include -I$(X11INCLUDE) -DZIPFILE

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

debug	: ds9Base ds9.zip
	$(RM) $@
	cp ds9Base ds9

ds9Base	: $(OBJS) $(LIBS)
	$(RM) $@
	$(CXX) ${OPTS} \
	-o $@ $(OBJS) $(LIBS) \
	-L$(X11LIB) -lX11 -lXext -lXft -lXrender -lXss -lfontconfig -lfreetype \
	-lxml2

endif

#--------------------------cygwin

ifneq (,$(findstring cygwin,$(ARCH)))

CXXFLAGS = $(CXXOPT) -I. -I../include -I$(X11INCLUDE)

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

debug	: ds9Base ds9.zip
	$(RM) $@
	cat ds9Base ds9.zip > ds9$(EXE)
	zip -A ds9$(EXE)
	chmod 755 ds9$(EXE)

ds9Base	: $(OBJS) $(LIBS)
	$(RM) $@
	$(CXX) ${OPTS} \
	-o $@ $(OBJS) $(LIBS) \
	-L$(X11LIB) -lX11 -lXext -lXft -lXrender -lXss -lfontconfig -lfreetype \
	-ljbig \
	-lxml2

endif

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)/src : zipdir ../src/*.tcl
	$(RM) -r $@
	cp -r ../src $(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 ds9/$(ZDIR)
	cd ..; find doc -name "*.gif" | cpio -pdmuv ds9/$(ZDIR)
	cd ..; find doc -name "*.png" | cpio -pdmuv ds9/$(ZDIR)

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

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

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

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