include ../make.include

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

vTCL	= 8.4.7

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

else
  # unix
  EXE =
endif

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

SCRIPTS = \
	2mass.tcl \
	analysis.tcl \
	analysisparam.tcl \
	annulus.tcl \
	ar.tcl \
	bin.tcl \
	box.tcl \
	boxannulus.tcl \
	bpanda.tcl \
	buttons.tcl \
	cat.tcl \
	catflt.tcl \
	catreg.tcl \
	catsrch.tcl \
	catsym.tcl \
	circle.tcl \
	colorbar.tcl \
	command.tcl \
	compass.tcl \
	composite.tcl \
	contour.tcl \
	convert.tcl \
	cpanda.tcl \
	crosshair.tcl \
	cube.tcl \
	debug.tcl \
	dss.tcl \
	dialog.tcl \
	ellipse.tcl \
	ellipseannulus.tcl \
	epanda.tcl \
	examine.tcl \
	external.tcl \
	fits.tcl \
	frame.tcl \
	graph.tcl \
	grid.tcl \
	group.tcl \
	help.tcl \
	hv.tcl \
	hvform.tcl \
	hvsup.tcl \
	imgsvr.tcl \
	iis.tcl \
	info.tcl \
	layout.tcl \
	line.tcl \
	magnifier.tcl \
	marker.tcl \
	markerbase.tcl \
	markerbaseannulus.tcl \
	markerbaseannulusrect.tcl \
	markerbasecenter.tcl \
	markerbaseline.tcl \
	markerbasepanda.tcl \
	markerbasepandarect.tcl \
	markerbaserect.tcl \
	menu.tcl \
	nameres.tcl \
	nsvr.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 \
	smooth.tcl \
	source.tcl \
	starbase.tcl \
	state.tcl \
	stdfbox.tcl \
	template.tcl \
	text.tcl \
	util.tcl \
	vector.tcl \
	vo.tcl \
	wcs.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/mview.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/r4.0.html \
	doc/release/sun.gif \
	doc/sun.gif \
	\
	template/chandra/acis/acis-i.tpl \
	template/chandra/acis/acis-i-letg.tpl \
	template/chandra/acis/acis-i-hetg.tpl \
	template/chandra/acis/acis-s.tpl \
	template/chandra/acis/acis-s-letg.tpl \
	template/chandra/acis/acis-s-hetg.tpl \
	template/chandra/acis/acis-is.tpl \
	template/chandra/acis/acis-is-letg.tpl \
	template/chandra/acis/acis-is-hetg.tpl \
	template/chandra/acis/acis-si.tpl \
	template/chandra/acis/acis-si-letg.tpl \
	template/chandra/acis/acis-si-hetg.tpl \
	template/chandra/acis/acis-is2.tpl \
	template/chandra/acis/acis-is2-letg.tpl \
	template/chandra/acis/acis-is2-hetg.tpl \
	template/chandra/hrc/hrc-i.tpl \
	template/chandra/hrc/hrc-i-letg.tpl \
	template/chandra/hrc/hrc-i-hetg.tpl \
	template/chandra/hrc/hrc-s.tpl \
	template/chandra/hrc/hrc-s-letg.tpl \
	template/chandra/hrc/hrc-s-hetg.tpl \
	\
	template/sao/megacam-amp.tpl \
	template/sao/megacam-chip.tpl \
	template/sao/swirc.tpl \
	template/sao/hectospec.tpl \
	template/sao/hectochelle.tpl \
	\
	template/xmm/epicmos1.tpl \
	template/xmm/epicmos2.tpl \
	template/xmm/epicpn.tpl

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/Tktable2.9 -lTktable2.9 \
	-L../lib -lz -lxpa -liis -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/Tktable2.9/libTktable2.9.a \
	../lib/libz.a \
	../lib/libxpa.a \
	../lib/libiis.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
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) \
	-Wl,--export-dynamic \
	-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) \
	-Wl,--export-dynamic \
	-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} \
	-Wl,--export-dynamic \
	-o $@ ds9.o ds9tk.o \
	-Wl,-Bstatic $(LIBS) \
	-L. -lstdc++ \
	-Wl,-Bdynamic -L$(X11LIB) -lX11 \
	-ldl -lpthread
	rm -f libstdc++.a
endif

#--------------------------FreeBSD

ifeq ($(ARCH),freebsd)
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 \
	-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) ${OPTS} -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/Tktable2.9/Tktable29.a \
	../lib/libz.a \
	../lib/libxpa.a \
	../lib/libiis.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 ds9tk.c ds9tk.h *.o
	$(RM) $(INDEX)

FORCE	:
