#
# This file is a Makefile for JS9.  If it has the name "Makefile.in"
# then it is a template for a Makefile;  to generate the actual Makefile,
# run "./configure", which is a configuration script generated by the
# "autoconf" program (constructs like "@foo@" will get replaced in the
# actual Makefile.
#

PACKAGE =		@PACKAGE_NAME@
VERSION =		@PACKAGE_VERSION@
DISTNAME =		${PACKAGE}-${VERSION}
DDISTNAME =		${PACKAGE}-data

DISTDIR	=		../export
FTPDIR	=		../ftp
BSCR	=		build
SCR	=		scripts
ALLIN1	=	 	js9-allinone

#----------------------------------------------------------------
# Things you can change to personalize the Makefile for your own
# site (you can make these changes in either Makefile.in or
# Makefile, but changes to Makefile will get lost if you re-run
# the configuration script).
#----------------------------------------------------------------

# Default top-level directories in which to install architecture-
# specific files (exec_prefix) and machine-independent files such
# as scripts (prefix).  The values specified here may be overridden
# at configure-time with the --exec-prefix and --prefix options
# to the "configure" script.

prefix =		@prefix@
exec_prefix =		@exec_prefix@

# The following definition can be set to non-null for special systems
# like AFS with replication.  It allows the pathnames used for installation
# to be different than those used for actually reference files at
# run-time.  INSTALL_ROOT is prepended to $prefix and $exec_prefix
# when installing files.
INSTALL_ROOT =

# Directory in which to install the .a or .so binaries
LIB_INSTALL_DIR =	$(INSTALL_ROOT)$(exec_prefix)/lib

# Directory in which to install the program wish:
BIN_INSTALL_DIR =	$(INSTALL_ROOT)$(exec_prefix)/bin

# Directory in which to install the include files
INCLUDE_INSTALL_DIR =	$(INSTALL_ROOT)$(prefix)/include

# Top-level directory for manual entries:
MAN_INSTALL_DIR =	$(INSTALL_ROOT)$(prefix)/man

# Directory in which to install manual entries
MAN1_INSTALL_DIR =	$(MAN_INSTALL_DIR)/man1

# Directory in which to install manual entries
# procedures:
MAN3_INSTALL_DIR =	$(MAN_INSTALL_DIR)/man3

# Some versions of make, like SGI's, use the following variable to
# determine which shell to use for executing commands:
SHELL =			/bin/sh

# There are just too many different versions of "install" around;
# better to use the install-sh script that comes with the distribution,
# which is slower but guaranteed to work.

INSTALL = 		@srcdir@/build/install-sh -c
INSTALL_PROGRAM =	${INSTALL}
INSTALL_DATA =		${INSTALL} -m 644

#----------------------------------------------------------------
# The information below should be usable as is.  The configure
# script won't modify it and you shouldn't need to modify it
# either.
#----------------------------------------------------------------
RM =		rm -rf

CP =		cp -p

# type of backend server (or none)
HELPER =	@HELPER@
USEHELPER =	@USEHELPER@

# which library
FITSLIB =	@FITSLIB@

# where the web files go on your site
WEBDIR =   	@WEBDIR@
CGIDIR =   	@CGIDIR@
CGIURL =   	@CGIURL@
CGIXPATH =   	@CGIXPATH@
# this is for SAO install only ... I think!
SAODIR =	@SAODIR@

WEBDIRS	=	plugins params js css font images

BEDIRS =	node_modules analysis-plugins analysis-wrappers

OTHERDIRS =	astroem src scripts build demos tests sao

WEBFILES =	js9.js js9.min.js js9worker.js \
		js9support.js js9support.min.js js9support.css js9plugins.js \
		js9support.txt js9.css $(ALLIN1).js $(ALLIN1).css \
		js9Msg.js js9PostMessage.js js9Helper.js js9Regions.js \
		js9Electron.js js9ElectronPreload.js js9ElectronMainMenu.js \
		astroem.js astroemw.js astroemw.wasm \
		favicon.ico js9.html

WEBHELP	=	help

PREFFILES =	js9Prefs.json js9prefs.js

MFILES =	Makefile Makefile.in configure configure.ac \
		js9 js9load js9wait js9msg README.md

# clean up from previous versions
RMFILES =	analysis-plugins/imageSection.json \
		js9Prefs-default.json js9prefs-default.js \
		js9allinone.html js9analysis.html js9basics.html \
		js9bespoke.html js9bitpix.html js9blend.html \
		js9cat.html js9cmaps.html js9create.html \
		js9dysel.html js9imexam.html js9large.html \
		js9masks.html js9menustyles.html js9multi.html \
		js9onchange.html js9panzoom.html js9plugins.html \
		js9preload.html js9rgb.html \
		js9sizes.html js9super.html js9sync.html \
		js9_center.html js9_center_top.html js9cookie.html \
		js9debug.html js9memleak.html js9iframe.html \
		js9usermenus.js js9usermenus.css js9debugextras \
		js9Test.html js9Test.js js9Tests fitsy.js fitsy.min.js

# sao: files that we gzip to make transfer faster
GZFILES =	js9.js js9.min.js js9support.js js9support.min.js \
		js9plugins.js js9-allinone.js \
		astroem.js astroemw.js astroemw.wasm

SAOWEBFILES =	sao/index.html

SAOCGIFILES =	sao/FITS-proxy.cgi \
		plugins/archive/CORS-proxy.cgi \
		plugins/archive/simbad-proxy.cgi

SAOPREFSFILES =	sao/js9Prefs.json sao/js9prefs.js

CSSFILES =	css/jquery.contextMenu.css css/dhtmlwindow.css  \
		css/tabcontent.css css/spectrum.css

PLCSSFILES =	plugins/core/blend.css plugins/core/blink.css \
		plugins/core/cmaps.css plugins/core/colorcontrols.css \
		plugins/core/colorbar.css plugins/core/cube.css \
		plugins/core/divs.css plugins/core/filters.css \
		plugins/core/imarith.css plugins/core/keyboard.css \
		plugins/core/layers.css plugins/core/mef.css \
		plugins/core/mousetouch.css plugins/core/scalecontrols.css \
		plugins/core/separate.css plugins/core/statusbar.css \
		plugins/core/syncui.css plugins/core/toolbar.css \
		plugins/core/zoomcontrols.css

JSFILES =	 js/winmod.js js/jquery.min.js js/jquery-ui.min.js js/jquery.contextMenu.min.js js/jquery.flot.min.js js/jquery.flot.errorbars.min.js js/jquery.flot.navigate.min.js js/jquery.flot.resize.min.js js/jquery.flot.selection.min.js js/flot-zoom.min.js js/sprintf.min.js js/dhtmlwindow.min.js js/dhtmlwindow_blurb.js js/fabric.min.js js/pako_inflate.min.js js/FileSaver.min.js js/canvas-toBlob.js js/tabcontent.js js/arrive.min.js js/jquery.doubletap.min.js js/jquery.flot.axislabels.js js/spin.js js/ElementQueries.js js/ResizeSensor.js js/gaussblur.js js/imagefilters.js js/jquery.ui.touch-punch.js js/js9inline.js js/spectrum.min.js js/tinycolor.min.js js/jquery.mark.es6.min.js js/jquery.caret.min.js js/regSelect.js

JSLINT =	js9.js js9worker.js js9Helper.js js9Msg.js \
		js9Electron.js js9ElectronPreload.js js9ElectronMainMenu.js \
		js9PostMessage.js js9Regions.js astroem/post.js

PLUGINFILES =	plugins/archive/archive.js \
		plugins/fitsy/binning.js \
		plugins/core/blend.js \
		plugins/core/blink.js \
		plugins/core/cmaps.js \
		plugins/core/colorbar.js \
		plugins/core/colorcontrols.js \
		plugins/core/console.js \
		plugins/core/cube.js \
		plugins/core/divs.js \
		plugins/core/filters.js \
		plugins/core/imarith.js \
		plugins/core/info.js \
		plugins/core/keyboard.js \
		plugins/core/layers.js \
		plugins/core/magnifier.js \
		plugins/core/mef.js \
		plugins/core/menubar.js \
		plugins/core/panner.js \
		plugins/core/zoomcontrols.js \
		plugins/core/prefs.js \
		plugins/core/scalecontrols.js \
		plugins/core/separate.js \
		plugins/core/statusbar.js \
		plugins/core/sync.js \
		plugins/core/syncui.js \
		plugins/core/toolbar.js \
		plugins/imexam/imexam.js \
		plugins/imexam/encircled.js \
		plugins/imexam/pixtable.js \
		plugins/imexam/radproj.js \
		plugins/imexam/reghist.js \
		plugins/imexam/regstat.js \
		plugins/imexam/xyproj.js \
		plugins/imexam/3dplot.js \
		plugins/imexam/contour.js

CGIFILES =	js9Helper.cgi

WEBDATA	=	blend casa fits kes75 large m13 orion reproj ses

PROGS =		js9helper

ALLPROGS =	$(PROGS)

INCLUDES =	js9helper.h

SCRIPTS =	scripts/funhist2flot scripts/funcnts2flot \
		scripts/js9mosaic

SCRIPTS2 =	js9 js9msg js9load js9wait

UTILLIB	=	libutil.a

ifeq ($(FITSLIB), cfitsio)
HELPEROBJS =	js9helper.o jsfitsio.o listhdu.o healpix.o
else
HELPEROBJS =	js9helper.o
endif

UTILOBJS = 	$(wildcard util/*.o)

all:		progs helper

progs:		FORCE
		@(echo "Entering src directory to build progs ..."; \
		  cd src && $(MAKE) all)

helper:		FORCE
		@($(BSCR)/mkhelper "$(HELPER)" \
		  "$(CGIDIR)" "$(CGIURL)" "$(CGIXPATH)"; \
		  if [ x"$(USEHELPER)" = x"yes" ]; then \
		    $(BSCR)/mkjs9 "$(WEBDIR)"; \
		  fi;)

regSelect:	FORCE
		@(echo "remaking js9 region selection parser ..."; \
		  jison src/regSelect.jison && mv regSelect.js js/.; \
		  $(MAKE) js9support;)

js9min:		FORCE
		@(echo "remaking js9.min.js ..."; \
		  touch js9.js; build/minify js9.js; \
		  echo "remaking all in one ..."; \
		  $(BSCR)/mkallinone $(ALLIN1) $(VERSION))

js9support:	FORCE
		@(echo "remaking js9support ..."; \
		  echo "css files in js9support.css: " > js9support.txt; \
		  echo $(CSSFILES) >>  js9support.txt; \
		  cat $(CSSFILES)  >  js9support.css;  \
		  echo $(PLCSSFILES) >>  js9support.txt; \
		  cat $(PLCSSFILES)  >>  js9support.css;  \
		  echo "js files in js9support.js: " >> js9support.txt; \
		  echo $(JSFILES)  >> js9support.txt; \
		  cat $(JSFILES)   >  js9support.min.js; \
		  X=`echo "$(JSFILES)" | sed 's/\.min//g'`; \
		  cat $$X  > js9support.js; \
		  echo "plugin files in js9plugins.js: " >> js9support.txt; \
		  echo $(PLUGINFILES)  >> js9support.txt; \
		  cat $(PLUGINFILES)   >  js9plugins.js; \
		  echo "remaking all in one ..."; \
		  $(BSCR)/mkallinone $(ALLIN1) $(VERSION);)

prefs:		FORCE
		@(echo "copying default preferences to js9Prefs.json"; \
		  cp -p build/js9Prefs-default.json js9Prefs.json; \
		  cp -p build/js9prefs-default.js js9prefs.js;)

saoprefs:	FORCE
		@(echo "editing js9Prefs for SAO installations ..."; \
	        sed -f $(BSCR)/saoprefs.sed < js9Prefs.json > sao/js9Prefs.json; \
	        sed -f $(BSCR)/saoprefs.sed < js9prefs.js > sao/js9prefs.js; \
		echo "editing helper port for SAO installations ..."; \
		$(BSCR)/saoport; \
		echo "editing helper file for SAO installations ..."; \
		$(BSCR)/mkhelper "$(HELPER)" \
		"$(CGIDIR)" "$(CGIURL)" "$(CGIXPATH)" sao/js9Prefs.json;)

js9:		FORCE
		@(if [ x"$(USEHELPER)" = x"yes" ]; then \
		    $(BSCR)/mkjs9 "$(WEBDIR)"; \
		  fi;)

inline:		FORCE
		@(echo "remaking js9 inline support ..."; \
		  $(BSCR)/mkinline)

eslint:		FORCE
		@(for file in $(JSLINT); do      \
		      echo $$file;               \
		      eslint $$file;             \
		done)

eslint2:	FORCE
		@(for file in `ls plugins/core/*.js plugins/fitsy/binning.js`; do      \
		      echo $$file;               \
		      eslint $$file;             \
		done)

tar:		FORCE
		($(RM) config.cache; \
		cd ..; \
		export COPYFILE_DISABLE=true; \
		tar cf - $(DISTNAME) | gzip -9 -c > $(DISTNAME).tgz)

install:	install-binaries install-scripts install-web

install-binaries: FORCE
	@(echo "entering src directory to install progs ..."; \
	  cd src && $(MAKE) install)

install-scripts: $(SCRIPTS) $(SCRIPTS2)
	@(if [ x$(USEHELPER) = xyes ]; then \
	  echo "Installing scripts ..."; \
	  for i in $(BIN_INSTALL_DIR) ; \
	    do \
	    if [ ! -d $$i ] ; then \
		echo "making directory $$i"; \
		mkdir -p $$i; \
		chmod 755 $$i; \
		else true; \
		fi; \
	    done; \
	 for i in $(SCRIPTS) $(SCRIPTS2) ; \
	    do \
		if [ -f $$i ] ; then \
		    echo "installing $$i" ; \
		    j=`basename $$i`; \
		    $(INSTALL_PROGRAM) $$i $(BIN_INSTALL_DIR)/$$j ; \
		fi; \
	    done; \
	    echo "Resetting js9 script to default after install ..."; \
	    make scriptclean; \
	  else \
	    echo "nothing to install for 'scripts' (no helper configured)"; \
	  fi;)

install-web:		install-webdirs install-webfiles install-webcgi

install-webdirs:	FORCE
	@(if [ x$(WEBDIR) != x ]; then \
	 echo "Installing web directories ..."; \
	 for i in $(WEBDIRS) $(WEBHELP); do \
	    if [ x$(WEBDIR) != x ]; then \
		mkdir -p $(WEBDIR); \
		if [ -d $$i ] ; then \
		    echo "installing web dir: $$i" ; \
		    tar cf - $$i | (cd $(WEBDIR); tar xf -); \
		fi; \
	    fi; \
         done; \
	 if [ x$(USEHELPER) = xyes ]; then \
	   if [ -d $(WEBDIR)/node_modules ]; then \
	     echo "moving web dir: node_modules (to avoid conflicts)"; \
	     rm -rf $(WEBDIR)/onode_modules; \
	     mv $(WEBDIR)/{,o}node_modules; \
	   fi; \
	   echo "Installing backend helper dirs ..."; \
	   for i in $(BEDIRS); do \
		if [ -d $$i ] ; then \
		    echo "installing backend dir: $$i" ; \
		    tar cf - $$i | (cd $(WEBDIR); tar xf -); \
		fi; \
           done; \
	 fi; \
         else \
	    echo "nothing to install for web directories ..." ; \
         fi;)

install-webfiles:	FORCE
	@(if [ x$(WEBDIR) != x ]; then \
	    echo "Installing web files ..."; \
	    mkdir -p $(WEBDIR); \
	    for i in $(WEBFILES); do\
		if [ -d $$i ] ; then \
		    echo "installing web directory: $$i" ; \
		elif [ -f $$i ] ; then \
		    echo "installing web file: $$i" ; \
		fi; \
		tar cf - $$i | (cd $(WEBDIR); tar xf -); \
            done; \
	    for i in $(PREFFILES); do\
		if [ ! -f $(WEBDIR)/$$i ] ; then \
		    echo "installing web file: $$i" ; \
		    tar cf - $$i | (cd $(WEBDIR); tar xf -); \
		else \
		    if ! diff -q $$i $(WEBDIR)/$$i &>/dev/null; then \
			echo "not installing $$i to avoid overwriting existing (and different) file"; echo "         override with 'make install-prefs'" ; \
		    fi; \
		fi; \
	    done; \
	    for i in $(RMFILES); do\
	        if [ -r $(WEBDIR)/$$i ]; then \
	            echo "removing $$i file ..."; \
	            rm -rf $(WEBDIR)/$$i; \
	        fi; \
	    done; \
        else \
          echo "nothing to install for web files ..." ; \
        fi;)

install-webcgi:	FORCE
	@(if [ -d "$(CGIDIR)" ]; then \
	    echo "Installing web CGI files ..."; \
	    for i in $(CGIFILES) ; do\
	        if [ -f $$i ]; then \
		    if [ -d $$i ] ; then \
		        echo "installing cgi directory: $$i" ; \
		    elif [ -f $$i ] ; then \
		        echo "installing cgi file: $$i" ; \
		    fi; \
		    tar cf - $$i | (cd $(CGIDIR); tar xf -); \
	        else \
		    echo "skipping cgi install: $$i"; \
	        fi; \
	    done; \
	else \
	    echo "nothing to install in cgi"; \
	fi;)

install-demos:	FORCE
	@(if [ -d demos ]; then \
	    echo "Installing web demos ..."; \
	    mkdir -p $(WEBDIR); \
            tar cf - demos | (cd $(WEBDIR); tar xf -); \
	fi)

install-tests:	FORCE
	@(if [ -d tests ]; then \
	    echo "Installing web tests ..."; \
	    mkdir -p $(WEBDIR); \
            tar cf - tests | (cd $(WEBDIR); tar xf -); \
	fi)

install-webdata:	FORCE
	@(for i in data/$(WEBDATA) ; do\
	    if [ x$(WEBDIR) != x ]; then \
	        mkdir -p $(WEBDIR)/data; \
		if [ -d $$i ] ; then \
		    echo "installing $$i" ; \
		    tar cf - $$i | (cd $(WEBDIR)/data; tar xf -); \
		fi; \
	    fi; \
         done;)

install-prefs:	FORCE
	@(for i in $(PREFFILES) ; do\
	    if [ x$(WEBDIR) != x ]; then \
	        mkdir -p $(WEBDIR); \
		if [ -f $$i ] ; then \
		    echo "installing $$i" ; \
		    tar cf - $$i | (cd $(WEBDIR); tar xf -); \
		fi; \
	    fi; \
         done;)

install-gzip:	FORCE
	@(for i in $(GZFILES) ; do\
	    if [ -f $(WEBDIR)/$$i ] ; then \
	        echo "gzip'ing $$i" ; \
	        gzip -c $(WEBDIR)/$$i > $(WEBDIR)/$$i.gz; \
	    fi; \
         done;)

install-sao:	install-gzip saoprefs install-webcgi install-demos install-tests
	@(if [ -d "$(SAODIR)" ]; then \
	    for i in $(SAOWEBFILES) ; \
	    do \
		if [ -f $$i ] ; then \
		    echo "installing sao web file: $$i" ; \
		    j=`basename $$i`; \
	            $(CP) $$i $(SAODIR)/$$j; \
		fi; \
	    done; \
	    for i in $(SAOCGIFILES) ; \
	    do \
		if [ -f $$i ] ; then \
		    echo "installing sao cgi file: $$i" ; \
		    j=`basename $$i`; \
	            $(CP) $$i $(SAODIR)/cgi-bin/$$j; \
		fi; \
	    done; \
	    for i in $(SAOPREFSFILES) ; \
	    do \
		if [ -f $$i ] ; then \
		    echo "installing sao prefs file: $$i" ; \
		    j=`basename $$i`; \
	            $(CP) $$i $(WEBDIR)/$$j; \
		    rm -r $$i; \
		fi; \
	    done; \
	  fi; \
	  echo "create temp directory"; \
	  mkdir -p $(WEBDIR)/tmp;)

allinone:	FORCE
		@($(BSCR)/mkallinone $(ALLIN1) $(VERSION))

release:	clean
		(mkdir -p $(DISTDIR)/$(DISTNAME); \
	        touch $(DISTDIR)/foo; \
		export COPYFILE_DISABLE=true; \
		tar cf - \
			$(WEBDIRS) $(BEDIRS) $(OTHERDIRS) \
			$(WEBFILES) $(WEBHELP) $(PREFFILES) \
			$(MFILES) |\
		(cd $(DISTDIR)/$(DISTNAME) && tar xf -); \
		cd $(DISTDIR) ; \
		tar cf - $(DISTNAME) | gzip -c > $(DISTNAME).tgz; \
		rm -rf foo $(DISTNAME))

release-data:	FORCE
		tar cf - data | gzip -c > $(DISTDIR)/$(DDISTNAME).tgz;

spell:		FORCE
		@(for f in $(SAOWEBFILES) $(WEBHELP)/*.html; do \
			echo "spellcheck $$f ..."; \
			aspell -c $$f; \
		  done)

js9helper:	FORCE
		@(build/nnode)

smoke:		FORCE
		@(X="`js9 targets`"; 					\
	          echo "$$X" | egrep -q -s '^ERROR|^0$$|^$$';		\
		  if [ $$? = 0 ]; then					\
			js9 -a -v --webpage tests/smoke.html --width 1110 --height 700 & \
			while [ x"`js9 targets`" != x1 ]; do 		\
			  printf "."; 					\
			  sleep 1; 					\
			done; 						\
		  fi;							\
		  python3 tests/smoke.py)

smoke2:		FORCE
		@(X="`js9 targets`"; 					\
	          echo "$$X" | egrep -q -s '^ERROR|^0$$|^$$';		\
		  if [ $$? = 0 ]; then					\
			js9 -a -v --webpage tests/smoke.html --width 1060 --height 670 & \
			while [ x"`js9 targets`" != x1 ]; do 		\
			  printf "."; 					\
			  sleep 1; 					\
			done; 						\
		  fi;							\
		  python3 tests/smoke2.py)

smoke3:		FORCE
		@(X="`js9 targets`"; 					\
	          echo "$$X" | egrep -q -s '^ERROR|^0$$|^$$';		\
		  if [ $$? = 0 ]; then					\
			js9 -a -v --webpage tests/smoke.html --savedir `pwd` --width 1060 --height 670 & \
			while [ x"`js9 targets`" != x1 ]; do 		\
			  printf "."; 					\
			  sleep 1; 					\
			done; 						\
		  fi;							\
		  python3 tests/smoke3.py)

smoke4:		FORCE
		@(echo "testing js9 shell scripting ...";               \
		  tests/ebands;                                         \
		  echo "testing hostfs support ...";                    \
		  js9 -a -v --hostfs true --cmdfile tests/ecnts.js;     \
		  printf "diff countsInRegions.log:\n";			\
		  diff countsInRegions.log tests/.; 			\
		  rm -f countsInRegions.log;				\
		  echo "\nall done!\n";					\
)

smokeall:	smoke smoke2 smoke3 smoke4

threeways:	FORCE
		@(cd tests/threeways && threeloop)

viewreadme:	FORCE
		@(grip)

clean:		FORCE
		@($(RM) *.a *.so *.dylib *.o *.exe core core.* errs *pure* \
			foo* *~ \#* TAGS *.E a.out errors 		\
			$(UTILLIB) $(ALLPROGS) $(CGIFILES) 		\
			gmon.out *.pg *.bak  				\
			*js9Prefs.json-orig *js9prefs.js-orig 		\
			config.info config.log ltest 			\
			autom4te.cache a.out.dSYM 			\
			*/*~ */*.bak plugins/*/*~ demos/debug*.html; 	\
		        $(MAKE) scriptclean; 				\
			cd src && $(MAKE) clean && cd ..; 		\
			cd astroem && $(MAKE) clean && cd ..)

scriptclean:	FORCE
		@($(CP) build/js9.in js9 && chmod +x js9)

distclean:	clean
		$(RM) Makefile config.status config.cache config.log 	\
		$(PREFFILES)

maintainer-clean:: clean
		$(RM) config.status config.cache config.log		\
		$(PREFFILES)

Makefile:	Makefile.in
		@(if [ -f config.status ] ; then 			\
			$(SHELL) config.status;      			\
		fi)

astroem:	FORCE
		@(make clean; cd astroem && make && make install && make clean)

errcheck:	FORCE
		@(egrep '[^x]error|warning|ld:|collect2:|make:' foo | \
		  egrep -v "^lex.*but not used"; true)

electron-cache:	FORCE
		@(rm -rf "$$HOME/Library/Application Support/Electron/Cache")

valgrind:	FORCE
		@(echo "see: https://developer.mozilla.org/en-US/docs/Mozilla/Testing/Valgrind"; echo "for Macs: valgrind --dsymutil=yes --show-mismatched-frees=no --leak-check=yes --track-origins=yes [prog] [args]";)

FORCE:

# DO NOT DELETE THIS LINE -- make depend depends on it.
