VERSION = NEMO V4.2 13-mar-2020 PJT
################################################################################
# 	$(NEMO)/Makefile: top level Makefile for full NEMO installation
#       do not edit, it is derived from Makefile.in
################################################################################

#  useful for MacOSX since it can't distinguish case for most users (grmpfff)
.PHONY:  INSTALL install scripts


# The following subdirectories (1&2) are needed for installation, although only
# some may have been part of the tar file that was imported. 
# The 2nd set of files need to exist, and must have world write permission :-(
# They are always architecture dependant files, that never get exported and
# created during a new installation.

# these are all relative to $(NEMO)
CHECKDIR1 = data inc news tmp csh \
            man man/man1 man/man3 man/man5 man/man8 man/manl \
            demo etc adm bin lib obj \
	    local \
            adm/import adm/export adm/install adm/submit adm/purge

_ASCIIDIRS_  = text bugs man/doc  $(SRC) $(USR)       # check if ASCIIDIRS can go


# absolute (derived) directories & files
CHECKDIR2 = $(NEMOBIN) $(NEMOLIB) $(NEMOOBJ) \
            $(NEMOOBJ)/bodytrans $(NEMOOBJ)/potential $(NEMOOBJ)/acc $(NEMOOBJ)/manip $(NEMOOBJ)/fit \
            $(NEMO)/py $(NEMO)/py/modules \
	    opt opt/include opt/lib opt/bin

CHECKFIL2 = $(NEMO)/adm/Usage \
	    $(NEMO)/adm/install/mknemo.log \
	    $(NEMOOBJ)/bodytrans/BTNAMES

################################################################################
#  Top level sub-directories in NEMO that contain Makefiles for NEMO install.
#  Currently accepted are:
#	src 		- standard export (NEMO expects install targets)
#	usr		- user contributions (have their own install methods)
#MAKEDIRS = src usr
#MAKELIBS = corelibs dehnen
MAKEDIRS = src usr
MAKELIBS = corelibs dehnen
NEMOTARS = /home/teuben/NEMO/nemo/src/scripts/DEBIAN/astronemo_4.1.7_Ubuntu20.04_x86_64/opt/tar
#MKNEMOS  = hdf4 hdf5 cfitsio fftw wcslib gsl
MKNEMOS  = 

################################################################################
# The following files/directories will be saved to a tar file
# and used in incremental export tar files (Nightly/export.csh)
# SRC is to select subsections of the whole src tree
# To get all of NEMO, choose SRC=src usr (see e.g. tarkernel below)
# --deprecated--
#
SRC = src
USR =
#
ASCIIFILES = COPYING MANIFEST NEMORC PORTING README VERSION \
	     src/Makefile \
	     configure configure.in \
	     Makefile.in makedefs.in config.h.in \
	     nemo_start.csh.in nemo_start.sh.in \
             install-sh config.sub config.guess \
	     nemo.rc nemo_end \
	     man/tmac.an man/whatis man/Makefile 
ASCIIDIRS  = inc text data bugs csh demo \
             man/doc man/man? $(SRC) $(USR)

EXPORTA = $(ASCIIFILES) $(ASCIIDIRS)
EXPORTB = $(EXPORTA) $(NEMOBIN) $(NEMOLIB) $(NEMOOBJ)
CHKFILE = adm/chkfile

#	Some default macro names, they can be overridden by supplying them
#	on the commandline, e.g. "make tarfile /usr/moi/nemo.tar

FILE   = $(NEMO)/nemo.tar
BACKUP = $(NEMO)/nemo-fullbck.tar

#
MOTD = $(NEMO)/etc/motd
TIMESTAMP=$(NEMO)/adm/TIMESTAMP

# local NEMO enhanced installed manually by a NEMO administrator
NEMO_RC_CSH = $(NEMO)/nemo_local.csh
NEMO_RC_SH  = $(NEMO)/nemo_local.sh

#  Master site (Maryland) specific, and probably not used anywhere else
FTPDIR = /n/ftp/pub/nemo/
SFTPDIR = chara.astro.umd.edu:$(FTPDIR)
WWWDIR = $(NEMO)/local/www
################################################################################
# INSTALL:
#	----------Installation starts here------------
# make sure you're in $(NEMO) from here

SHELL = /bin/bash

# this should remain the first target in this Makefile, so a default "make" invocation goes here
help:   install lsb

install:
	@echo "There is no install because NEMO is build in place. The usual procedure is"
	@echo ""
	@echo "      ./configure  ....      -- run configure, with lots of options; try --help"
	@echo "      make build1            -- 1: postconfig  (build would do all build1..4)"
	@echo "      make build2            -- 2: libs + falcon"
	@echo "      make build3            -- 3: bins"
	@echo "      make build4            -- 4: mknemo build reconfirmation"
	@echo "      make check             -- the testsuite"
	@echo "      make bench             -- the benchmark"
	@echo ""
	@echo "After this, users will need to source the nemo_start.(c)sh file"
	@echo "There is also some new python support via nemo_start.py"

lsb:
	@lsb_release -a


help2:
	@echo ""
	@echo " ### NEMO installation help menu ###"
	@echo ""
	@echo Some important environment variables to be used are:
	@echo NEMO=$(NEMO)
	@echo NEMOSITE=$(NEMOSITE)
	@echo NEMOHOST=$(NEMOHOST)
	@echo SHELL=$(SHELL)
	@echo CC=$(CC)  CFLAGS=$(CFLAGS)
	@echo FC=$(FC)  FFLAGS=$(FFLAGS)
	@echo MAKE=$(MAKE) MFLAGS=$(MFLAGS)
	@echo MKNEMOS=$(MKNEMOS)
	@echo ""
	@echo "Additional help targets: helpfind, helptar"

# optional package installs, usually in $NEMO/opt via $NEMO/local during bootstrap
mknemos:
	@for mkn in $(MKNEMOS); do \
	mknemo $$mkn;\
	done

# make sure all directories and files are present before install can take place
dirs: 
	@echo Checking directory structure on `date`
	@echo By `whoami` on `hostname`
	-@for i in $(CHECKDIR1) $(ASCIIDIRS); do \
	(if [ ! -d $(NEMO)/$$i ]; then \
	    echo DIR $(NEMO)/$$i created; \
	    mkdir $(NEMO)/$$i; \
	    chmod a+w $(NEMO)/$$i; \
	fi); done
	-@for i in $(CHECKDIR2); do \
	(if [ ! -d $$i ]; then \
	    echo DIR $$i created; \
	    mkdir $$i; \
	    chmod a+w $$i; \
	fi); done
	-@for i in $(CHECKFIL2); do \
	(if [ ! -f $$i ]; then \
	    echo FILE $$i created; \
	    touch $$i; \
	    chmod a+w $$i; \
	fi); done
	@if [ -d $(NEMOTARS) ]; then \
	    echo 'Symlink tars'; \
	    rm tars; \
	    ln -s $(NEMOTARS) tars; \
	else \
	    echo 'No tar symlink'; \
	fi
	@if [ ! -d include ]; then \
	    ln -s inc include; \
	fi
	@if [ ! -f $(TIMESTAMP) ]; then \
            echo Updating $(TIMESTAMP) file on `date`; \
	    echo `date` `hostname` `whoami` > $(TIMESTAMP); \
            echo "`cat $(NEMO)/VERSION` `date +%m%d%H%M%y` `whoami` `date` (install)" >> $(CHKFILE); \
	else \
	    echo "TIMESTAMP exists:";\
	    ls -l $(TIMESTAMP);\
	fi
	@if [ ! -f $(MOTD) ]; then \
            echo Creating $(MOTD) file on `date`; \
            echo "***********************************************" > $(MOTD);\
            echo "Nemo Release 4 ($(VERSION)) installed: " >> $(MOTD);\
	    echo "  NEMOVER = $(NEMOVER)" >> $(MOTD); \
	    echo "  `date` on `whoami`@`hostname`" >> $(MOTD); \
            echo "***********************************************">> $(MOTD);\
	else \
	    echo "MOTD exists:";\
	    ls -l $(MOTD);\
	fi
	@if [ ! -f $(NEMO_RC_CSH) ]; then \
            echo Creating $(NEMO_RC_CSH) file on `date`; \
            echo "# place your local NEMO csh commands here" > $(NEMO_RC_CSH);\
	else \
	    echo "nemo_local.csh exists:";\
	    ls -l $(NEMO_RC_CSH);\
	fi
	@if [ ! -f $(NEMO_RC_SH) ]; then \
            echo Creating $(NEMO_RC_SH) file on `date`; \
            echo "# place your local NEMO sh commands here" > $(NEMO_RC_SH);\
	else \
	    echo "nemo_local.sh exists:";\
	    ls -l $(NEMO_RC_SH);\
	fi

	@echo "`cat VERSION` `date`" >> .version
	@if [ ! -f $(NEMOLIB)/maxsizes.h ]; then \
	    cp inc/max/maxsizes.h $(NEMOLIB)/maxsizes.h;\
	else \
	    echo "$(NEMOLIB)/maxsizes.h exists:";\
	    ls -l $(NEMOLIB)/maxsizes.h;\
	fi
	@echo "OK, all main directories and files should be there now"
	@echo Done at `date`

# @todo: deprecate?
pjt:
	@if [ ! -f $(NEMOLIB)/makedefs ]; then \
	    if [ -f $(NEMO)/makedefs ]; then \
              echo Copying $(NEMO)/makedefs $(NEMOLIB);\
	      cp $(NEMO)/makedefs $(NEMOLIB); \
            else \
              echo Creating dummy  $(NEMOLIB)/makedefs;\
              touch  $(NEMOLIB)/makedefs ;\
	    fi \
	else \
	    echo "$(NEMOLIB)/makedefs exists:";\
	    ls -l $(NEMOLIB)/makedefs;\
	fi


scripts:
	@echo Installing scripts on `date` 
	@echo By `whoami` on `hostname`
	(cd $(NEMO)/src/scripts; $(MAKE) install)

# The next four targets are not currently supported 
#all:	nemo_lib nemo_bin

nemo_lib:
	@echo Make all subdirectories on `date`
	@echo By `whoami` on `hostname`
	-@for i in ${MAKEDIRS}; do \
		(cd $$i; echo `date` MAKE NEMO_LIB in `pwd`; \
		$(MAKE) nemo_lib); done
	@echo NEMO is installed in $(NEMO)
	@echo all done `date`

nemo_bin:
	-@for i in ${MAKEDIRS}; do \
		(cd $$i; echo `date` MAKE NEMO_BIN in `pwd`; \
		$(MAKE) nemo_bin); done
	@echo all done `date`

nemo_src:
	-@for i in ${MAKEDIRS}; do \
	(cd $$i; $(MAKE) nemo_src); done


#
##############################################################################
# HTML stuff
#

html:  doxygen

doxygen:
	(cd text; doxygen nemo-doxygen.cfg)

README.html: README.md
	# markdown_py -x toc readme.md > readme.html
	pandoc README.md -o README.html


##############################################################################
# EXPORT

helptar:
	@echo "Install tar targets:"
	@echo "  % make targit"
	@echo "  % make targit0"
	@echo "  % make tarfile [FILE=$(FILE)]"
	@echo "  % make tarbackup [BACKUP=$(BACKUP)]"
	@echo "  % make purge"
	@echo "  % make ftp"
	@echo "  % make usrftp [USR=$(USR)]"
	@echo "  % make tarkernel"
	@echo "  % make tarbima"
	@echo "  % make tarorbit"
	@echo "  % make dist"
	@echo "  % make distu"
	@echo ""


# @todo warning:  VER vs. $NEMOVER
VER = `cat $(NEMO)/VERSION`
GIT1 = https://github.com/teuben/nemo/
GIT2 = https://github.com/teuben/pgplot/

targit:
	rm -rf nemo_$(VER)
	git clone $(GIT1) nemo_$(VER)
	mkdir nemo_$(VER)/local
	git clone $(GIT2) nemo_$(VER)/local/pgplot
	echo WARNING: snapshot gitless export `date` >> nemo_$(VER)/VERSION
	rm -rf nemo_$(VER)/.git nemo_$(VER)/local/pgplot/.git
	tar zcf nemo_$(VER).tar.gz nemo_$(VER)

targit0:
	rm -rf nemo_git
	git clone $(GIT1) nemo_git
	mkdir nemo_git/local
	git clone $(GIT2) nemo_git/local/pgplot
	tar zcf nemo_git.tar.gz nemo_git


# autoconf/GIT based export
#

DIST_DIR = nemo_$(NEMOVER)

dist:
	@if [ ! -d $(DIST_DIR) ]; \
	then \
	  echo 'TODO for a new DIST: -------------------------------------'; \
	  echo Date: `date`; \
	  echo 'Change this date in:  www/nemo/lastftp {FIX THIS}'; \
	  echo 'Change: docs/whatsnew.html'; \
	  echo 'Change: docs/index.html'; \
	  echo 'And run the following commands: '; \
	  echo 'make targit'; \
	  echo 'scp $(DIST_DIR).tar.gz $(SFTPDIR)'; \
	  echo "git tag `echo nemo_$(NEMOVER) | sed 's/\./_/g'`"; \
	  echo "git push origin `echo nemo_$(NEMOVER) | sed 's/\./_/g'`"; \
	  echo 'src/scripts/version inc'; \
	  echo 'git commit VERSION inc/version.h'; \
	  echo 'git push'; \
	  echo 'DONE: ----------------------------------------------------'; \
	else \
	  echo Directory $(DIST_DIR) exists; \
	fi;


# The following 'ftp' targets only supposed to work at the master site

COMPRESS = gzip -f
NEMOTAR  = nemo_`cat $(NEMO)/VERSION`.tar

export:
	@echo "Steps to update NEMO:"
	@echo "make dist                        GIT based distribution maker"
	@echo "make new                         to see what's new for ftp"
	@echo "make ftp                         full  compressed (src) export"
	@echo "src/scripts/version inc          increase patch level"
	@echo "  or:"
	@echo "src/scripts/version set MA.MI.PA set new Major/Minor/Patch"
	@echo "FTPDIR=$(FTPDIR)"
	@echo "SFTPDIR=$(SFTPDIR)"
	@echo "NEMOTAR=$(NEMOTAR)"


ftp:
	@echo Working from FTPDIR=$(FTPDIR) :;df -k $(FTPDIR)
	@$(MAKE) tarfile "SRC=$(SRC)" "USR=$(USR)" FILE=$(FTPDIR)/$(NEMOTAR)
	@echo Compressing
	@$(COMPRESS) $(FTPDIR)/$(NEMOTAR)
	@cp VERSION src/scripts/bootstrap README $(FTPDIR)
	@(cd $(FTPDIR); rm nemo.tar.gz; ln -s $(NEMOTAR).gz nemo.tar.gz)
	@ls -l $(FTPDIR)
	@echo "<A HREF=ftp://ftp.astro.umd.edu/pub/nemo/$(NEMOTAR).gz> $(NEMOTAR).gz </A>"  > $(WWWDIR)/lastftp
	@echo "(`date`)" >> $(WWWDIR)/lastftp
#	@(cd $(FTPDIR) ; ls -l $(NEMOTAR).gz > $(WWWDIR)/lastftp)
#	@echo `ls -l $(FTPDIR)/$(NEMOTAR).gz` > $(WWWDIR)/lastftp
#	@echo "Last updated on `date` by `whoami`" > $(WWWDIR)/lastftp

#		only do this when you really are done with "ftp"
stamp:
	@echo "`cat $(NEMO)/VERSION` `date +%m%d%H%M%y` `whoami` `date`" >> $(CHKFILE)
	@tail -1 $(CHKFILE)
	@echo You should probably now increment the version:
	@echo "   src/scripts/version inc"

new_time:
	@echo "`cat $(NEMO)/VERSION` `date +%m%d%H%M%y` `whoami` `date`" >> $(CHKFILE)
	@tail -1 $(CHKFILE)

test2:
	@echo "<A HREF=ftp://ftp.astro.umd.edu/pub/nemo/$(NEMOTAR).gz> $(NEMOTAR).gz </A>"  > $(WWWDIR)/lastftp
	@echo "(`date`)" >> $(WWWDIR)/lastftp


##############################################################################
#	Some helpful non-NEMO targets

# deprecated: needs doc files
miriad:
	@echo "Installing miriad shells..."
	(cd $(NEMO)/src/tools/miriad/miriad;\
	 $(MAKE) miriad ; mv miriad $(NEMOBIN); $(MAKE) clean)
	(cd $(NEMO)/src/tools/miriad/mirtool;\
	 $(MAKE) nemotool ; mv nemotool $(NEMOBIN); $(MAKE) clean)

# deprecated: makeindex comes with latex
makeindex:
	@echo "Installing makeindex utility for LaTeX manuals"	(cd $(NEMO)/src/tools/makeindex;\
	 $(MAKE);mv indexsrc/index $(NEMOBIN)/makeindex;$(MAKE) clean)

#	internal PGPLOT install (not recommended anymore)
pgplot:
	src/scripts/pgplot.install png=$(PNG) gif=$(GIF)

#       some of the essentials that can be installed in $NEMO/opt
cfitsio:
	(source nemo_start.sh ; src/scripts/mknemo.d/cfitsio)

hdf4:
	(source nemo_start.sh ; src/scripts/mknemo.d/hdf4)

hdf5:
	(source nemo_start.sh ; src/scripts/mknemo.d/hdf5)


# deprecated: superseded by glnemo2
vogl:
	src/scripts/nemo.vogl



build:   build1 build2 build3 build4

build1:
	(source nemo_start.sh ; $(MAKE) postconfig mknemos)
	@echo 'Continue with "make build2"'

build2:
	(source nemo_start.sh ; $(MAKE) libs)
	@echo 'Continue with "make build3"'

build3:
	(source nemo_start.sh ; $(MAKE) bins)
	@echo 'Continue with "make build4"'

BINS4 = tsf tabhist tsd orboom sdinfo
build4:
	(source nemo_start.sh ; mknemo  $(BINS4))
	@echo 'Continue with "make check"'


# the exact python method under development

python: anaconda3

python_setup: nemo_start.py
	python -c 'import sys; print(sys.path)'

anaconda3:
	src/scripts/install_anaconda3



helpconfig:
	@echo config, configure, config_extra, config_clean

postconfig:	dirs config_extra scripts

config:	configure
	./configure

configure:	configure.ac
	autoconf

config_extra:
	cp config.h makedefs $(NEMOLIB)

DIFF = diff

diff_new:
	-$(DIFF) config.h $(NEMOLIB)
	-$(DIFF) makedefs $(NEMOLIB)

#	useful if you want to start with a clean slate
config_clean:
	rm -f $(CONFIG_CLEAN)

CLEAN = bin lib obj adm/TIMESTAMP etc/motd $(CONFIG_CLEAN)

CONFIG_CLEAN = config.h makedefs config.cache config.log config.status \
	nemo_start.sh nemo_start.csh

clean:
	@echo There is no single clean, there is cleanall, config_clean and distclean:
	@echo ""
	@echo '"make config_clean" would remove' 
	@echo ""
	@echo $(CONFIG_CLEAN)
	@echo ""
	@echo '2) "make cleanall" would remove' 
	@echo ""
	@echo $(CLEAN)
	@echo ""
	@echo '3) "make cleansrc" would clean the src tree (of .o files and such)'
	@echo ""
	@echo '"make distclean" would remove NEMORC.local as well as'
	@echo ""
	@echo $(CHEDKDIR2)

distclean: config_clean cleansrc
	@echo Cleaning as much as one can to simulate a distribution
	rm -rf $(CHECKDIR2)

cleanall:
	rm -rf $(CLEAN)

cleansrc:
	(cd src; $(MAKE) clean)

# via configure it will select:    MAKELIBS = corelibs dehnen     
libs:	$(MAKELIBS)
	@echo Done at `date`

# gyrfalcON
dehnen:
	-(cd usr/dehnen; $(MAKE) -i clean all) >> install.log 2>&1 

corelibs:
	@echo Starting at `date`, tail -f $(NEMO)/install.log
	-(cd src;$(MAKE) -i clean install) > install.log 2>&1 

slib:
	(cd $(NEMOLIB); \
	ld -shared -o libnemo.so --whole-archive libnemo.a)


bins:
	@echo Starting at `date`, tail -f $(NEMO)/install.log	
	cd src;$(MAKE) -i bins >> ../install.log 2>&1 
	cd usr;$(MAKE) -i bins >> ../install.log 2>&1 
	@echo Done at `date`

missingbins:
	ls $(NEMO)/bin > bins.list
	-$(DIFF) bins.list src/scripts
	@echo This NEMO has `wc -l bins.list`, the benchmark is `wc -l src/scripts/bins.list`

test:
	@echo ISSUE the following command:
	@echo 'src/scripts/testsuite -b'

testbins:
	src/scripts/testsuite -b

testsuite:
	@echo ISSUE the following command:
	@echo 'src/scripts/testsuite -b'

check:
	(source nemo_start.sh; src/scripts/testsuite)
	@echo 'Continue with "make checklog" (if you see any FAILS)'

# show the latest testsuite build log
checklog:
	cat `ls -tr tmp/test*/testsuite.log | tail -1`

TIME = /usr/bin/time

bench:
	(source nemo_start.sh; $(TIME) src/scripts/nemo.bench bsf=1)

#
TEST=""
reinstall:
	@echo 'It is adviced to "git update" before this step'
	docs/test_a_new_nemo_git reuse=1 nemo=. $(TEST)

sure:	libs bins

.makedefs:
	$(MAKE) -f src/Makefile makedefs_help
