prefix		= @prefix@
exec_prefix	= @exec_prefix@

bindir		= @bindir@
libdir		= $(exec_prefix)/lib
#libdir		= @libdir@
includedir	= @includedir@
datarootdir	= @datarootdir@
datadir		= @datadir@
mandir		= @mandir@

TCLSH_PROG	= @TCLSH_PROG@

TCL_VERSION	= @TCL_VERSION@
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@

tifftcl_LIB_SPEC= @tifftcl_LIB_SPEC@
pngtcl_LIB_SPEC	= @pngtcl_LIB_SPEC@
jpegtcl_LIB_SPEC= @jpegtcl_LIB_SPEC@
zlibtcl_LIB_SPEC= @zlibtcl_LIB_SPEC@

tkimg_LIB_SPEC	= @tkimg_LIB_SPEC@
tkimggif_LIB_SPEC= @tkimggif_LIB_SPEC@
tkimgjpeg_LIB_SPEC= @tkimgjpeg_LIB_SPEC@
tkimgpng_LIB_SPEC= @tkimgpng_LIB_SPEC@
tkimgtiff_LIB_SPEC= @tkimgtiff_LIB_SPEC@
tkimgwindow_LIB_SPEC= @tkimgwindow_LIB_SPEC@

tcliis_LIB_SPEC	= @tcliis_LIB_SPEC@
tclsignal_LIB_SPEC= @tclsignal_LIB_SPEC@
tclxml_LIB_SPEC	= @tclxml_LIB_SPEC@
tclzvfs_LIB_SPEC= @tclzvfs_LIB_SPEC@
tls_LIB_SPEC	=@tls_LIB_SPEC@

tkblt_LIB_SPEC	= @tkblt_LIB_SPEC@
tkhtml1_LIB_SPEC= @tkhtml1_LIB_SPEC@
tkmpeg_LIB_SPEC	= @tkmpeg_LIB_SPEC@
tksao_LIB_SPEC	= @tksao_LIB_SPEC@
tktable_LIB_SPEC= @tktable_LIB_SPEC@

CC		= @CC@
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@ @SSLLIBS@
CFLAGS		= @CFLAGS@
COMPILE		= $(CC) $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CFLAGS)

system		= @system@

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

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

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

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

APPDIR	= zvfsmntpt
LIBDIR= $(APPDIR)

ID	= $(shell finger $(shell whoami) | egrep -o 'Name: [a-zA-Z0-9 ]{1,}' | cut -d ':' -f 2 | head -n 1| xargs echo)

OBJS	= ds9.o tkAppInit.o

empty:=
space:=$(empty) $(empty)

SPECS	= \
	$(jpegtcl_LIB_SPEC) \
	$(pngtcl_LIB_SPEC) \
	$(tifftcl_LIB_SPEC) \
	$(zlibtcl_LIB_SPEC) \
	$(tcliis_LIB_SPEC) \
	$(tclsignal_LIB_SPEC) \
	$(tclxml_LIB_SPEC) \
	$(tclzvfs_LIB_SPEC) \
	$(tls_LIB_SPEC) \
	$(tkblt_LIB_SPEC) \
	$(tkhtml1_LIB_SPEC) \
	$(tkimg_LIB_SPEC) \
	$(tkimggif_LIB_SPEC) \
	$(tkimgjpeg_LIB_SPEC) \
	$(tkimgpng_LIB_SPEC) \
	$(tkimgtiff_LIB_SPEC) \
	$(tkimgwindow_LIB_SPEC) \
	$(tkmpeg_LIB_SPEC) \
	$(tksao_LIB_SPEC) \
	$(tktable_LIB_SPEC) \
	$(TK_STUB_LIB_SPEC) \
	$(TK_LIB_SPEC) \
	$(TCL_STUB_LIB_SPEC) \
	$(TCL_LIB_SPEC)

LLIBS	= \
	$(subst /$(space),/,$(patsubst -L%,%/,$(patsubst -l%,lib%.a,$(tksao_LIB_SPEC)))) \
	$(libdir)/libfuntools.a \
	$(libdir)/libast.a \
	$(libdir)/libast_err.a \
	$(libdir)/libast_pal.a \
	$(libdir)/libxpa.a \
	$(subst /$(space),/,$(patsubst -L%,%/,$(patsubst -l%,lib%.a,$(SPECS))))

PREQS	= \
	$(APPDIR) \
	$(LIBDIR)/tcl$(TCL_VERSION) \
	$(LIBDIR)/tcl8 \
	$(LIBDIR)/tk$(TCL_VERSION) \
	$(LIBDIR)/library \
	$(LIBDIR)/msgs \
	$(LIBDIR)/doc \
	$(LIBDIR)/cmaps \
	$(LIBDIR)/template \
	$(LIBDIR)/tls \
	$(LIBDIR)/tkblt \
	$(LIBDIR)/tkcon \
	$(LIBDIR)/base64 \
	$(LIBDIR)/ftp \
	$(LIBDIR)/log \
	$(LIBDIR)/textutil \
	$(LIBDIR)/math

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

.NOTPARALLEL :
.PHONY	: debug

ifeq (,$(findstring Darwin,$(system)))

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

debug	: ds9base ds9.zip
	$(RM) ds9
	cat ds9base ds9.zip > ds9
	zip -A ds9
	chmod 755 ds9
	cp ds9 $(prefix)/bin/.

else

ds9	: ds9base ds9.zip
	$(RM) ds9
	strip ds9base
	cp ds9base ds9
	cp ds9 $(prefix)/bin/.
	cp ds9.zip $(prefix)/bin/.
#	cd $(prefix)/bin; codesign -s "$(ID)" ds9

debug: ds9base ds9.zip
	$(RM) ds9
	cp ds9base ds9
	cp ds9 $(prefix)/bin/.
	cp ds9.zip $(prefix)/bin/.
#	cd $(prefix)/bin; codesign -s "$(ID)" ds9

endif

ds9base	: $(OBJS) $(LLIBS)
	$(RM) $@
	$(CC) $(CFLAGS) -o $@ \
	$(OBJS) \
	$(LLIBS) \
	$(TK_LIBS) \
	$(LIBS)

ds9.zip	: $(PREQS)
	zip -ru ds9.zip $(APPDIR)

tkAppInit.o : tkAppInit.c
	$(COMPILE) -DTK_LOCAL_APPINIT=SAOAppInit \
	-DTK_LOCAL_MAIN_HOOK=SAOLocalMainHook -c $? -o $@

tkAppInit.c : $(prefix)/tk$(TCL_VERSION)/unix/tkAppInit.c
	cp $? .

$(APPDIR) : 
	mkdir -p $@

$(LIBDIR)/tcl$(TCL_VERSION) :
	cp -prf $(prefix)/lib/tcl$(TCL_VERSION) $(LIBDIR)/.

$(LIBDIR)/tcl8 :
	cp -prf $(prefix)/lib/tcl8 $(LIBDIR)/.

$(LIBDIR)/tk$(TCL_VERSION) :
	cp -prf $(prefix)/lib/tk$(TCL_VERSION) $(LIBDIR)/.
	rm -rf $(APPDIR)/tk$(TCL_VERSION)/images
	rm -rf $(APPDIR)/tk$(TCL_VERSION)/demos

#--------------------------clean

.PHONY	: clean

clean	:
	$(RM) core *~ *# ds9base ds9 *.zip *.o
	$(RM) -rf $(APPDIR)

include ../make.include