include ../make.include

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

vTCL	= 8.4.6

ifeq ($(ARCH),windows)
  # windows
  EXE = .exe

else
  # unix
  EXE =
endif

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

SCRIPTS = \
	analysis.tcl \
	analysisparam.tcl \
	annulus.tcl \
	bin.tcl \
	box.tcl \
	boxannulus.tcl \
	buttons.tcl \
	circle.tcl \
	colorbar.tcl \
	command.tcl \
	compass.tcl \
	contour.tcl \
	crosshair.tcl \
	cube.tcl \
	debug.tcl \
	dss.tcl \
	dialog.tcl \
	ellipse.tcl \
	ellipseannulus.tcl \
	examine.tcl \
	external.tcl \
	fits.tcl \
	frame.tcl \
	graph.tcl \
	grid.tcl \
	group.tcl \
	help.tcl \
	hv.tcl \
	hvform.tcl \
	hvsup.tcl \
	iis.tcl \
	info.tcl \
	line.tcl \
	magnifier.tcl \
	marker.tcl \
	menu.tcl \
	namesvr.tcl \
	panda.tcl \
	panner.tcl \
	panzoom.tcl \
	pixel.tcl \
	plot.tcl \
	plotsup.tcl \
	point.tcl \
	polygon.tcl \
	prefs.tcl \
	print.tcl \
	projection.tcl \
	rgb.tcl \
	ruler.tcl \
	saveas.tcl \
	scale.tcl \
	source.tcl \
	state.tcl \
	stdfbox.tcl \
	tkfbox.tcl \
	text.tcl \
	util.tcl \
	vo.tcl \
	wcs.tcl \
	xmfbox.tcl \
	xpa.tcl

FILES	= \
	doc/faq.html \
	doc/new.html \
	doc/issue.html \
	doc/helpdesk.html \
	doc/acknowledgment.html \
	doc/ref/analysis.html \
	doc/ref/color.html \
	doc/ref/command.html \
	doc/ref/contour.html \
	doc/ref/file.html \
	doc/ref/grid.html \
	doc/ref/gui.html \
	doc/ref/index.html \
	doc/ref/iraf.html \
	doc/ref/keyboard.html \
	doc/ref/manalysis.html \
	doc/ref/mbin.html \
	doc/ref/mcolor.html \
	doc/ref/medit.html \
	doc/ref/mfile.html \
	doc/ref/mframe.html \
	doc/ref/mhelp.html \
	doc/ref/mregion.html \
	doc/ref/mscale.html \
	doc/ref/mwcs.html \
	doc/ref/mzoom.html \
	doc/ref/prefs.html \
	doc/ref/print.html \
	doc/ref/region.html \
	doc/ref/sun.gif \
	doc/ref/xpa.html \
	doc/release/r3.0.html \
	doc/release/sun.gif \
	doc/sun.gif

INDEX	= pkgIndex.tcl

LIBS	= -L../lib -lsaotk -ltkhtml -ltkmpeg -L../lib/Img1.3 \
	-ltkimgwindow1.3 \
	-ltkimgjpeg1.3 -ljpegtcl1.0 \
	-ltkimgtiff1.3 -ltifftcl1.0 \
	-ltkimgpng1.3 -lpngtcl1.0 \
	-ltkimgppm1.3 \
	-ltkimggif1.3 \
	-ltkimgbmp1.3 \
	-ltkimgxbm1.3 \
	-lzlibtcl1.0 -ltkimg1.3 \
	-L../lib -lz -ltclxpa -liis -last -lcheckdns \
	-lBLT24 -ltk8.4 -ltcl8.4

LIBS.a	= ../lib/libsaotk.a \
	../lib/libtkhtml.a \
	../lib/libtkmpeg.a \
	../lib/Img1.3/libtkimgwindow1.3.a \
	../lib/Img1.3/libtkimgjpeg1.3.a ../lib/Img1.3/libjpegtcl1.0.a \
	../lib/Img1.3/libtkimgtiff1.3.a ../lib/Img1.3/libtifftcl1.0.a \
	../lib/Img1.3/libtkimgpng1.3.a ../lib/Img1.3/libpngtcl1.0.a \
	../lib/Img1.3/libtkimgppm1.3.a \
	../lib/Img1.3/libtkimggif1.3.a \
	../lib/Img1.3/libtkimgbmp1.3.a \
	../lib/Img1.3/libtkimgxbm1.3.a \
	../lib/Img1.3/libzlibtcl1.0.a ../lib/Img1.3/libtkimg1.3.a \
	../lib/libz.a \
	../lib/libtclxpa.a \
	../lib/libiis.a \
	../lib/libast.a \
	../lib/libcheckdns.a \
	../lib/libBLT24.a ../lib/libtk8.4.a ../lib/libtcl8.4.a \


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

ifeq ($(ARCH),solaris)
# problems with linking under solaris
# 1. there is no libdl.a, so we have to link against libdl.so
# 2. there seems to be a problem with libnsl.a or one of the libs it calls,
#    that causes a segv when printing. so we force use of some shared libs.
# 3. got to use -static-libgcc to force a static link to libgcc
# 4. got to use ln -s... to fool gcc to static link libstdc++

ds9	: ds9.o ds9tk.o FORCE
	$(RM) $@
	rm -f libstdc++.a
	ln -s `$(CXX) -print-file-name=libstdc++.a` .
	$(CXX) -static-libgcc ${OPTS} \
	-o $@ ds9.o ds9tk.o \
	-Wl,-Bstatic $(LIBS) \
	-L. -lstdc++ \
	-Wl,-Bdynamic -L$(X11LIB) -lX11 -lXext \
	/usr/lib/libsocket.so /usr/lib/libnsl.so /usr/lib/libdl.so
	rm -f libstdc++.a

ds9pure	: ds9.o ds9tk.o FORCE
	$(RM) $@
	/soft/purifyplus/releases/purify.sol.2003.06.00/purify \
	$(CXX) ${OPTS} -o $@ ds9.o ds9tk.o $(LIBS) -L$(X11LIB) -lX11 -lXext
endif

#--------------------------solaris64

ifeq ($(ARCH),solaris64)
ds9	: ds9.o ds9tk.o FORCE
	$(RM) $@
	rm -f libstdc++.a
	ln -s `$(CXX) -m64 -mcpu=v9 -print-file-name=libstdc++.a` .
	$(CXX) ${OPTS} -m64 -mcpu=v9 -static-libgcc \
	-o $@ ds9.o ds9tk.o -Wl,-Bstatic $(LIBS) \
	-L. -lstdc++ \
	-Wl,-Bdynamic -L$(X11LIB)/sparcv9 -lX11 -lXext \
	/usr/lib/sparcv9/libsocket.so /usr/lib/sparcv9/libnsl.so \
	/usr/lib/sparcv9/libdl.so
	rm -f libstdc++.a
endif

#--------------------------linux
# 
ifeq ($(ARCH),linux)
ds9	: ds9.o ds9tk.o FORCE
	$(RM) $@
	rm -f libstdc++.a
	ln -s `$(CXX) -print-file-name=libstdc++.a` .
	$(CXX) -static-libgcc $(OPTS) \
	-o $@ ds9.o ds9tk.o \
	-Wl,-Bstatic $(LIBS) \
	-L. -lstdc++ \
	-Wl,-Bdynamic  -L$(X11LIB) -lX11 \
	-ldl -lpthread
	rm -f libstdc++.a
endif

#--------------------------linux64
# 
ifeq ($(ARCH),linux64)
ds9	: ds9.o ds9tk.o FORCE
	$(RM) $@
	rm -f libstdc++.a
	ln -s `$(CXX) -m64 -print-file-name=libstdc++.a` .
	$(CXX) -static-libgcc $(OPTS) \
	-o $@ ds9.o ds9tk.o \
	-Wl,-Bstatic $(LIBS) \
	-L. -lstdc++ \
	-Wl,-Bdynamic  -L$(X11LIB) -lX11 \
	-ldl -lpthread
	rm -f libstdc++.a
endif

#--------------------------linuxppc
# yellow dog 3.0 has a bad version of libX11.a
# so we must link dynamic

ifeq ($(ARCH),linuxppc)
ds9	: ds9.o ds9tk.o FORCE
	$(RM) $@
	rm -f libstdc++.a
	ln -s `$(CXX) -print-file-name=libstdc++.a` .
	$(CXX) ${OPTS} \
	-o $@ ds9.o ds9tk.o \
	-Wl,-Bstatic $(LIBS) \
	-L. -lstdc++ \
	-Wl,-Bdynamic -L$(X11LIB) -lX11 \
	-ldl -lpthread
	rm -f libstdc++.a
endif

#--------------------------darwin
# darwin can't be linked static
# also, the libs always need ranlib

ifeq ($(ARCH),darwin)
ds9	: ds9.o ds9tk.o FORCE
	$(RM) $@
	$(RANLIB) ../lib/*.a ../lib/Img1.3/*.a
	$(CXX) ${OPTS} \
	-o $@ ds9.o ds9tk.o \
	$(LIBS.a) \
	-lstdc++ \
	$(X11LIB)/libX11.a \
	-framework CoreFoundation
endif

#--------------------------alpha

ifeq ($(ARCH),alpha)
ds9	: ds9.o ds9tk.o FORCE
	$(RM) $@
	$(CXX) -static -Wl,-S \
	-o $@ ds9.o ds9tk.o \
	$(LIBS) \
	-L$(X11LIB) -lX11 \
	-ldnet_stub
endif

#--------------------------sgi

ifeq ($(ARCH),sgi)
ds9	: ds9.o ds9tk.o FORCE
	$(RM) $@
	$(CXX) -static \
	-o $@ ds9.o ds9tk.o \
	$(LIBS) \
	-L$(X11LIB) -lX11 \
	-lm -lc
endif

#--------------------------hpux

ifeq ($(ARCH),hpux)
ds9	: ds9.o ds9tk.o FORCE
	$(RM) $@
	$(CXX) -static \
	-o $@ ds9.o ds9tk.o \
	$(LIBS) \
	-L$(X11LIB) -lX11 \
	/usr/lib/libdld.sl
endif

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

ifeq ($(ARCH),windows)
ds9	: ds9.o ds9tk.o FORCE
	$(RM) $@$(EXE)
	$(CXX) -O2 -Wl,-allow-multiple-definition -o $@ \
	-e _mainCRTStartup -mwin32 -mwindows \
	ds9.o ds9tk.o \
	../lib/libsaotk.a \
	../lib/libtkhtml.a \
	../lib/libtkmpeg.a \
	../lib/Img1.3/tkimgwindow13.a \
	../lib/Img1.3/tkimgjpeg13.a ../lib/Img1.3/jpegtcl10.a \
	../lib/Img1.3/tkimgtiff13.a ../lib/Img1.3/tifftcl10.a \
	../lib/Img1.3/tkimgpng13.a ../lib/Img1.3/pngtcl10.a \
	../lib/Img1.3/tkimgppm13.a \
	../lib/Img1.3/tkimggif13.a \
	../lib/Img1.3/tkimgbmp13.a \
	../lib/Img1.3/tkimgxbm13.a \
	../lib/Img1.3/zlibtcl10.a ../lib/Img1.3/tkimg13.a \
	../lib/libz.a \
	../lib/libtclxpa.a \
	../lib/libiis.a \
	../lib/libast.a \
	../lib/libcheckdns.a \
	../lib/libwin.a \
	../lib/libBLT24.a ../lib/libtk84s.a ../lib/libtcl84s.a \
	-limm32 -lcomctl32 -lwinspool ../tk$(vTCL)/win/wish.res.o

endif

#--------------------------items

doc	: FORCE
	@echo "Making Reference Manual psp..."
	cd doc/ref; html2ps -C fb -g -n -u -W b -x 1 index.html > ref.ps
	@echo "Making Reference Manual pdf
	cd doc/ref; ps2pdf ref.ps ref.pdf 

install	: FORCE
	cp ds9$(EXE) ../bin/.
	strip ../bin/ds9$(EXE)

index	: $(SCRIPTS)
	echo "pkg_mkIndex . $(SCRIPTS)" | ../bin/tclsh8.4$(EXE)

ds9.o : ds9.C ds9tk.h

ds9tk.o	: ds9tk.c ds9tk.h

ifeq ($(ARCH),windows)
ds9tk.c	: ds9.mta.windows ds9.C ds9.tcl $(SCRIPTS) $(FILES)
	../bin/mktclapp -f ds9.mta.windows > $@
else
ds9tk.c	: ds9.mta ds9.C ds9.tcl $(SCRIPTS) $(FILES)
	../bin/mktclapp -f ds9.mta > $@
endif

ds9tk.h	:
	../bin/mktclapp -header > $@

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

distclean : FORCE
	$(RM) core *~ *# ds9 ds9pure ds9tk.c ds9tk.h *.o
	$(RM) $(INDEX)

FORCE	:
