include ../make.include
include ../make.pkgs

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

ZDIR	= zipdir/zvfsmntpt
FFILES	= $(ZDIR)/$(TCLVER) \
	$(ZDIR)/tcl8 \
	$(ZDIR)/$(TKVER) \
	$(ZDIR)/$(BLTVER) \
	$(ZDIR)/$(TCLLIBVER) \
	$(ZDIR)/$(TKCONVER) \
	$(ZDIR)/$(XMLRPCVER) \
	$(ZDIR)/src \
	$(ZDIR)/msgs \
	$(ZDIR)/doc \
	$(ZDIR)/cmaps \
	$(ZDIR)/template

ifdef FILTERCOMPILER
FILES	= $(FFILES) \
	$(ZDIR)/$(FILTERCOMPILER)
else
FILES	= $(FFILES)
endif

# for unix, macosx
MAINDIR = ../$(TKDIR)/unix
MAIN	= tkAppInit

LIBS	= \
	../lib/libsaotk.a \
	../lib/libtkhtml.a \
	../lib/libtkmpeg.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)/libtkimgppm1.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/libzip.a \
	../lib/libz.a \
	../lib/libxpa.a \
	../lib/libiis.a \
	../lib/libcheckdns.a \
	../lib/libsignal_ext.a \
	../lib/libxxlib.a \
	../lib/libBLTX30.a \
	../lib/libBLTCore30.a \
	../lib/libtk8.5.a \
	../lib/libtcl8.5.a

# if windows, redefine
ifeq ($(OS),windows)
  MAINDIR = ../$(TKDIR)/win
  MAIN	= winMain
  RES	= ds9.res.o

  LIBS	= \
	../lib/libsaotk.a \
	../lib/libtkhtml.a \
	../lib/libtkmpeg.a \
	../lib/$(TCLXMLVER)/Tclxml32.a \
	../lib/$(TKTABLEVER)/Tktable210.a \
	../lib/$(TKIMGVER)/tkimgpng14.a \
	../lib/$(TKIMGVER)/pngtcl143.a \
	../lib/$(TKIMGVER)/tkimgtiff14.a \
	../lib/$(TKIMGVER)/tifftcl394.a \
	../lib/$(TKIMGVER)/tkimgjpeg14.a \
	../lib/$(TKIMGVER)/jpegtcl82.a \
	../lib/$(TKIMGVER)/tkimggif14.a \
	../lib/$(TKIMGVER)/tkimgppm14.a \
	../lib/$(TKIMGVER)/tkimgwindow14.a \
	../lib/$(TKIMGVER)/zlibtcl125.a \
	../lib/$(TKIMGVER)/tkimg14.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/libzip.a \
	../lib/libz.a \
	../lib/libxpa.a \
	../lib/libiis.a \
	../lib/libcheckdns.a \
	../lib/libxxlib.a \
	../lib/libBLTX30.a \
	../lib/libBLTCore30.a \
	../lib/libtk85sg.a \
	../lib/libtcl85sg.a
endif

OBJS	= ds9.o $(MAIN).o $(RES)

#--------------------------targets

ifeq ($(OS),unix)
CFLAGS= $(CCOPT) -I. -I../include \
	-I../$(TKDIR)/generic -I../$(TKDIR)/unix \
	-I$(X11INCLUDE)

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

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

all: ds9

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

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

ds9.zip	: $(FILES)
	cd zipdir; ../../bin/zip -r9 ../ds9.zip *	

else

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

all: ds9

ds9	: null.zip ds9Base $(FILES)
	$(RM) $@
	cp ds9Base ds9.zip
	strip ds9.zip
	cat null.zip >> ds9.zip
	cd zipdir; ../../bin/zip -rA9 ../ds9.zip *
	mv ds9.zip ds9
	cp ds9 ../bin/.

debug	: null.zip ds9Base $(FILES)
	$(RM) $@
	cp ds9Base ds9.zip
	cat null.zip >> ds9.zip
	cd zipdir; ../../bin/zip -rA9 ../ds9.zip *
	mv ds9.zip ds9
endif

endif

ifeq ($(OS),windows)
CFLAGS= $(CCOPT) -I. -I../include \
	-I../$(TKDIR)/generic -I../$(TKDIR)/win \
	-I$(X11INCLUDE)

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

all: ds9.exe

ds9.exe	: ds9Base.exe ds9.zip ../bin/tcc
	$(RM) $@
	cp ds9Base.exe ds9.exe
	strip ds9.exe
	cp ds9.exe ../bin/.
	cp ds9.zip ../bin/.
	cp /bin/cygwin1.dll ../bin/.
	cp /bin/cygxml2-2.dll ../bin/.
	cp /bin/cygstdc++-6.dll ../bin/.
	cp /bin/cygiconv-2.dll ../bin/.
	cp /bin/cygz.dll ../bin/.
	cp /bin/cyggcc_s-1.dll ../bin/.
	cp /bin/cygjbig-2.dll ../bin/.
	cp /bin/cyglzma-5.dll ../bin/.

debug	: ds9Base.exe ds9.zip ../bin/tcc
	$(RM) $@
	cp ds9Base.exe ds9.exe

ds9app	: ds9.exe
	$(RM) -r ../bin/ds9app
	mkdir ../bin/ds9app
	cp ds9.exe ../bin/ds9app/.
	cp ds9.zip ../bin/ds9app/.
	cp /bin/cygwin1.dll ../bin/ds9app/.
	cp /bin/cygxml2-2.dll ../bin/ds9app/.
	cp /bin/cygstdc++-6.dll ../bin/ds9app/.
	cp /bin/cygiconv-2.dll ../bin/ds9app/.
	cp /bin/cygz.dll ../bin/ds9app/.
	cp /bin/cyggcc_s-1.dll ../bin/ds9app/.
	cp /bin/cygjbig-2.dll ../bin/ds9app/.
	cp /bin/cyglzma-5.dll ../bin/ds9app/.
	cp -rp ../bin/tcc ../bin/ds9app/.
	cp ../ds9/win/install.vbs ../bin/ds9app/.

ds9.zip	: $(FILES)
	cd zipdir; ../../bin/zip -r9 ../ds9.zip *	

endif

ifeq ($(OS),macosx)
CFLAGS= $(CCOPT) -I. -I../include \
	-I../$(TKDIR)/generic -I../$(TKDIR)/macosx \
	-I$(X11INCLUDE)

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

all: ds9

ds9	: ds9Base ds9.zip
	$(RM) $@
	cp ds9Base ds9
	strip ds9
	cp ds9 ../bin/.
	cp ds9.zip ../bin/.

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

ds9app	: ds9
	rm -rf ../bin/SAOImage\ DS9.app
	cp -rp macosx/SAOImage\ DS9.app ../bin/.
	cd ../bin/SAOImage\ DS9.app; find -d . -name CVS -exec rm -rf {} \;
	cp -p ../bin/ds9 ../bin/SAOImage\ DS9.app/Contents/MacOS/.
	cp -p ../bin/ds9.zip ../bin/SAOImage\ DS9.app/Contents/MacOS/.
	cd ../bin; $(CODESIGN) -s "SAOImage DS9" SAOImage\ DS9.app

ds9.zip	: $(FILES)
	cd zipdir; ../../bin/zip -r9 ../ds9.zip *	

endif

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

$(MAIN).c : $(MAINDIR)/$(MAIN).c
	cp $(MAINDIR)/$(MAIN).c .

null.zip:
	echo . | zip null.zip -
	zip null.zip -d -

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)/$(BLTVER) : zipdir
	$(RM) -r $@
	mkdir $(ZDIR)/$(BLTVER)
	mkdir $(ZDIR)/$(BLTVER)/afm
	cp ../$(BLTDIR)/library/graph.tcl $(ZDIR)/$(BLTVER)/.
	cp ../$(BLTDIR)/library/bltGraph.pro $(ZDIR)/$(BLTVER)/.
	cp ../$(BLTDIR)/library/tclIndex $(ZDIR)/$(BLTVER)/.
	cp ../$(BLTDIR)/library/afm/*.afm $(ZDIR)/$(BLTVER)/afm/.

$(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)

ifdef FILTERCOMPILER
$(ZDIR)/$(FILTERCOMPILER) : zipdir ../compilers/$(FILTERCOMPILER)
	$(RM) -r $@
	cp ../compilers/$(FILTERCOMPILER) $(ZDIR)/$(FILTERCOMPILER)
endif

#--------------------------solaris

ifeq ($(ARCH),solaris)
ds9Base	: $(OBJS) $(LIBS)
	$(RM) $@
	rm -f libstdc++.a
	ln -s `$(CXX) ${OPTS} -print-file-name=libstdc++.a` .
	$(CXX) -static-libgcc ${OPTS} \
	-o $@ $(OBJS) $(LIBS) \
	-Wl,-Bstatic -L. -lstdc++ \
	-Wl,-Bdynamic \
	-L$(X11LIB) -lX11 -lXext -lXft -lXrender -lfontconfig -lfreetype \
	-lxml2 \
	-lsocket -lnsl -ldl
	rm -f libstdc++.a
endif

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

ifneq (,$(findstring linux,$(ARCH)))
ds9Base	: $(OBJS) $(LIBS)
	$(RM) $@
	rm -f libstdc++.a
	ln -s `$(CXX) ${OPTS} -print-file-name=libstdc++.a` .
	$(CXX) ${OPTS} -static-libgcc -Wl,--export-dynamic \
	-o $@ $(OBJS) $(LIBS) \
	-Wl,-Bstatic -L. -lstdc++ \
	-Wl,-Bdynamic \
	-L$(X11LIB) -lX11 -lXext -lXft -lXrender -lXss \
	-lxml2 \
	-ldl -lpthread 
	rm -f libstdc++.a
endif

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

ifneq (,$(findstring darwin,$(ARCH)))
ds9Base	: $(OBJS) $(LIBS)
	$(RM) $@
	$(CXX) ${OPTS} \
	-o $@ $(OBJS) $(LIBS) \
	-L$(X11LIB) -lX11 -lXext -lXft -lXrender -lXss -lfontconfig -lfreetype \
	-lxml2 \
	-lstdc++ -liconv
endif

#--------------------------macosx

ifeq ($(OS),macosx)
ds9Base	: $(OBJS) $(LIBS)
	$(RM) $@
	$(CXX) ${OPTS} \
	-o $@ $(OBJS) $(LIBS) \
	-lxml2 \
	-lstdc++ -liconv \
	-sectcreate __TEXT __tk_rsrc ../$(TKDIR)/unix/tk8.5.rsrc \
	-framework CoreFoundation -framework Carbon
endif

#--------------------------windows

ifeq ($(OS),windows)
ds9Base.exe: $(OBJS) $(LIBS)
	$(RM) $@
	$(CXX) ${OPTS} \
	-o $@ $(OBJS) $(LIBS) \
	-lxml2 -ljbig \
	-lws2_32 -limm32 -lcomctl32 -mwindows 

$(RES)	: win/ds9.rc win/ds9.ico
	windres -o $@ --define STATIC_BUILD --include ../$(TKDIR)/generic \
	--include ../$(TCLDIR)/generic --include ../$(TKDIR)/win/rc \
	--include win win/ds9.rc

../bin/tcc: ../compilers/$(TCC)
	$(RM) -r $@
	cd ../bin; unzip ../compilers/$(TCC)
	chmod +x ../bin/tcc/tcc.exe
	touch $@

endif

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

clean	: FORCE
	$(RM) core *~ *# 

ifeq ($(OS),windows)
distclean: FORCE
	$(RM) core *~ *# ds9Base.exe ds9.exe *.zip *.o
	$(RM) -r zipdir bin/tcc
else
distclean: FORCE
	$(RM) core *~ *# ds9Base ds9 *.zip *.o
	$(RM) -r zipdir
endif

FORCE	:
