#
SHELL=/bin/sh
# 
########################################################################

TCLVERS    =  tcl8.3.2
TKVERS     =  tk8.3.2
ITCLVERS   =  itcl3.2.0
EXPVERS    =  expect-5.28
TCLSHVERS  =  8.3

FTOOLS     = $(FV)

########################################################################

COMPLIST	=	fitsTcl $(ITCLVERS) pow \
			$(TCLVERS)/unix $(TKVERS)/unix

CONFIG_OPTION	=	--enable-shared

#####################################
#                                   #
#  Top level rules for building fv  #
#                                   #
#####################################

all: tcl8 tk8 itcl fitstcl1 pow1

install: install-compiled fv1


#####################
# Package compiles
#####################

tcl8:
	if [ -d $(TCLVERS)/unix ]; then \
		cd $(TCLVERS)/unix; \
		rm -f config.cache; \
		./configure --prefix=$(FTOOLS) ${CONFIG_OPTION} ; \
		${MAKE}; \
	fi

tk8:
	if [ -d $(TKVERS)/unix ]; then \
		cd $(TKVERS)/unix; \
		rm -f config.cache; \
		./configure --prefix=$(FTOOLS) ${CONFIG_OPTION} ; \
		${MAKE}; \
	fi

expect:
	if [ -d $(EXPVERS) ]; then \
		cd $(EXPVERS); \
		rm -f config.cache; \
		./configure --prefix=$(FTOOLS) ${CONFIG_OPTION} \
		--with-tclinclude=`pwd`/../$(TCLVERS)/generic \
		--with-tkinclude=`pwd`/../$(TKVERS)/generic \
		--with-tclconfig=`pwd`/../$(TCLVERS)/unix \
		--with-tkconfig=`pwd`/../$(TKVERS)/unix; \
		${MAKE}; \
	fi


pow1:
	if [ -d pow ]; then \
		cd pow; \
		./configure --prefix=$(FTOOLS) ${CONFIG_OPTION}\
			    --with-tclinclude=`pwd`/../$(TCLVERS)/generic \
			    --with-tkinclude=`pwd`/../$(TKVERS)/generic; \
		 $(MAKE) TARGETNAME=libpow; \
	fi



itcl:
	if [ -d $(ITCLVERS) ]; then \
		cd $(ITCLVERS); \
		./configure --prefix=$(FTOOLS) ${CONFIG_OPTION} \
		    --with-tcl=`pwd`/../$(TCLVERS)/unix/ \
		    --with-tk=`pwd`/../$(TKVERS)/unix/; \
		$(MAKE); \
	fi

fitstcl1:
	if [ -d fitsTcl ]; then \
		cd fitsTcl; \
		./configure --prefix=$(FTOOLS) ${CONFIG_OPTION} \
		    --with-tclinclude=`pwd`/../$(TCLVERS)/generic; \
		$(MAKE); \
	fi

fv1:
	-mkdir ${FTOOLS}/lib/fv
	if [ -d fv ]; then \
		cd fv;\
		cp -p  fv ${FTOOLS}/bin/fv;\
		cp -pr doc ${FTOOLS}/lib/fv;\
		cp -pr class ${FTOOLS}/lib/fv;\
		cp -p  fvInit.tcl ${FTOOLS}/lib/fv; \
	fi


###################
#  Installation Rules
###################

install-compiled: install-tcl8 install-tk8 install-itcl install-pow install-fitstcl

install-tcl8:
	if [ -f $(TCLVERS)/unix/Makefile ]; then \
		cd $(TCLVERS)/unix; \
		${MAKE} install; \
	fi
	@-cd ${FTOOLS}/bin; ln -s tclsh$(TCLSHVERS) tclsh 2> /dev/null

install-tk8:
	if [ -f $(TKVERS)/unix/Makefile ]; then \
		cd $(TKVERS)/unix; \
		${MAKE} install; \
	fi
	@-cd ${FTOOLS}/bin; ln -s wish$(TCLSHVERS) wish 2> /dev/null

install-expect:
	if [ -f $(EXPVERS)/Makefile ]; then \
		cd $(EXPVERS); \
		${MAKE} install; \
	fi

install-pow:
	if [ -f pow/Makefile ]; then \
		cd pow; \
		$(MAKE) TARGETNAME=libpow install; \
	fi

install-itcl:
	if [ -f $(ITCLVERS)/Makefile ]; then \
		cd $(ITCLVERS); $(MAKE)  install; \
	fi


install-fitstcl:
	if [ -f fitsTcl/Makefile ]; then \
		cd fitsTcl; $(MAKE) FTOOLS=$(FTOOLS) install; \
	fi

##################
#  Cleaning rules
##################

cleanup: clean
	rm -rf local/man

distclean:
	@-for dir in ${COMPLIST}; do \
		if [ -f $$dir/Makefile ]; then \
			(cd $$dir; ${MAKE} distclean); status=$$?; \
		fi; \
	done
	rm -rf local/*

clean: clean-itcl clean-tcltk8 clean-fitstcl clean-pow

clean-tcltk8:
	if [ -f $(TCLVERS)/unix/Makefile ]; then \
		cd $(TCLVERS)/unix; $(MAKE) clean; rm -f config.cache; \
	fi
	if [ -f $(TKVERS)/unix/Makefile ]; then \
		cd $(TKVERS)/unix; $(MAKE) clean; rm -f config.cache; \
	fi

clean-pow:
	if [ -f pow/Makefile ]; then \
		cd pow; $(MAKE)  clean; rm -f config.cache; \
	fi

clean-itcl:
	if [ -f $(ITCLVERS)/Makefile ]; then \
		cd $(ITCLVERS); $(MAKE)  clean; rm -f config.cache; \
	fi

clean-fitstcl:
	if [ -f fitsTcl/Makefile ]; then \
		cd fitsTcl; $(MAKE)  clean; rm -f config.cache; \
	fi


dist-clean: clean

