# $Rev:: 8434          $:  Revision of last commit
# $Date:: 2021-12-29 1#$:  Date of last commit

#
# =====  release description  ====
DESCRIPTION = ORAC, JCC version
STATUS = beta version 

RELEASE=$(shell cat RELEASE)
RELEASE:=.$(subst .,_,$(RELEASE))
SVN_REV=$(shell svnversion)
XXX=$(shell svnversion | sed 's/[0-9]//g')
SVN_PATH_TAG=$(shell svn info | awk '/^URL:/ {URL=$$2};/^Repository Root:/ {Root=$$3};END {a=substr(URL,length(Root)+7);gsub("/",".",a);print a}')
START_PATH:=$(realpath .)
SVN_TAG:=r$(subst :,-,$(SVN_REV))
DIR_NAME=orac$(RELEASE)
TAR_NAME=orac$(RELEASE).$(SVN_PATH_TAG).$(SVN_TAG).tar.gz
TAR_NAME_NO_SVN=orac$(RELEASE).tar.gz
CURRENT_URL=$(shell svn info | grep ^URL | cut -d" " -f2)

THIS_MAKEFILE := $(lastword $(MAKEFILE_LIST))

# =====  release description  ====

# GNU tar options: 
TAR_OPTS=--dereference --exclude-vcs --exclude "*.develop"  --exclude "lib/etc" 

.DEFAULT: 
	$(MAKE) -C src $@

help:
	$(MAKE) -C src help

#
# create a distribution and pack it
#   

# the distribution is made from a fresh copy of the 
# 

#    works for any workingdir/version, not only the HEAD of the trunk
#      e.g. a particular release
#      always call make with THIS Makefile (or a later version)
#      `make -f <this_makefile_path> distro'

#         note that in recursive calls also 
#         (see below, `$(MAKE) -f $(THIS_MAKEFILE) tar`)
#         you want to use THIS Makefile -not the one you find in the
#         working dir you are making a distro of, which may have a
#         different Makefile:

distro: manual
	svn update 

# test the value of XXX here, not the real revision, since the value
#   which is in XXX is the one used for naming the tar file
	@if expr "$(XXX)" = ""; then \
	$(MAKE) -f $(THIS_MAKEFILE) tar; \
	echo " *************************************************************";\
	echo "  Created new distribution file ../"$(TAR_NAME); \
	echo " *************************************************************";\
        else \
	echo " ***************************************************************";\
        echo " * WARNING: this dir has mixed SVN versions, or modified files:";\
        echo " * svnversion = " `svnversion` "; used in tar name= $(SVN_REV) "; \
        echo " * update and/or commit changes before making a distribution. ";\
        echo " * (if you just committed, try typing 'make distro' again) ";\
	echo " ***************************************************************";\
        fi

#
tar:
	cd ..; \
	rm -rf $(DIR_NAME); \
	mkdir $(DIR_NAME); \
	svn checkout $(CURRENT_URL) $(DIR_NAME)/ ; \
	cp $(START_PATH)/doc/orac-manual.pdf $(DIR_NAME)/doc ; \
	tar -zcvf $(TAR_NAME_NO_SVN) $(TAR_OPTS) $(DIR_NAME); \
	rm -rf $(DIR_NAME); 

#	BASE=`basename $$PWD`; \

#	rm $(DIR_NAME); \
#	touch $${BASE}; \
#	ln -sf $${BASE} $(DIR_NAME) ; \



# create PDF version of the manual
manual:
#	$(MAKE) -C doc/etc/manual clean
	$(MAKE) -C doc/etc/manual 
	$(MAKE) -C doc/etc/manual publish

clean:
	-rm -rf *~ *# 

# recursively 'clean' directories
#distclean: clean
#	-for dir in `find * -type d` ; do $(MAKE) -C $$dir distclean > /dev/null 2>&1 ; done 

cleanall:
	-for dir in `find * -type d` ; do $(MAKE) -C $$dir clean > /dev/null 2>&1 ; done 


test:
	@echo  MAKEFILE_LIST= $(MAKEFILE_LIST)
	@echo  THIS_MAKEFILE= $(THIS_MAKEFILE)
	@echo  RELEASE= $(RELEASE)
	@echo  SVN_PATH= $(SVN_PATH)
	@echo  SVN_PATH_TAG= $(SVN_PATH_TAG)
	@echo  SVN_TAG= $(SVN_TAG)
	@echo  TAR_NAME= $(TAR_NAME)
	@echo  START_PATH= $(START_PATH)
	@echo  cp $(SVN_PATH)/doc/orac-manual.pdf $(DIR_NAME)/doc 

