srcdir                    := @srcdir@
prefix                    := @prefix@
package                   := lmod
ifeq ($(PATH_TO_SRC),@$(path_to_src)@)
  PATH_TO_SRC             := .
endif
PATH_TO_LUA               := @PATH_TO_LUA@
version                   := $(shell LUA_PATH="$(srcdir)/src/?.lua;" $(PATH_TO_LUA) -e "V=require('Version'); print(V.tag())")
SITE_CONTROLLED_PREFIX    := @SITE_CONTROLLED_PREFIX@
LMOD_ROOT                 := $(prefix)/$(package)
MY_PACKAGE                := $(prefix)/$(package)/$(version)
MY_PKG_PACKAGE            := $(prefix)/$(package)/$(package)
LMOD_CONFIG_DIR           := @LMOD_CONFIG_DIR@

ifneq ($(SITE_CONTROLLED_PREFIX),no)
  LMOD_ROOT               := $(prefix)/..
  MY_PACKAGE              := $(prefix)
  MY_PKG_PACKAGE          := $(prefix)
endif
CC                        := @CC@
PATH_TO_SRC               := @PATH_TO_SRC@
path_to_src               := PATH_TO_SRC


TOOL_SRC                  := $(wildcard $(srcdir)/tools/*.lua)
I18N_SRC                  := $(wildcard $(srcdir)/tools/i18n/*.lua)

CONF_PY                   := $(srcdir)/docs/source/conf.py
CONF_PY_PATTERN           := $(srcdir)/proj_mgmt/conf_py_update_patternA.lua
CURRENT_MK                := $(lastword $(MAKEFILE_LIST))
export IGNORE_DIRS        := $(shell $(PATH_TO_LUA) $(srcdir)/proj_mgmt/ignore_dirs_converter @IGNORE_DIRS@)
REDIRECT                  := @REDIRECT@
LMOD_SETTARG_FULL_SUPPORT := $(shell echo "@SETTARG@"       | tr '[:upper:]' '[:lower:]')
USE_DOT_FILES             := $(shell echo "@USE_DOT_FILES@" | tr '[:upper:]' '[:lower:]')
PREPEND_BLOCK             := $(shell echo "@PREPEND_BLOCK@" | tr '[:upper:]' '[:lower:]')
COLORIZE                  := $(shell echo "@COLORIZE@"      | tr '[:upper:]' '[:lower:]')
SETTARG_CMD               := settarg_cmd
MODULEPATH_INIT           := @MODULEPATH_INIT@
LMOD_AVAIL_EXTENSIONS     := @AVAIL_EXTENSIONS@
LMOD_HIDDEN_ITALIC        := @HIDDEN_ITALIC@
LMOD_OVERRIDE_LANG        := @LMOD_OVERRIDE_LANG@
LMOD_ALLOW_ROOT_USE       := @LMOD_ALLOW_ROOT_USE@
SITE_MSG_FILE             := @SITE_MSG_FILE@
SITE_NAME                 := @SITE_NAME@
SUPPORT_KSH               := @SUPPORT_KSH@
SYSHOST                   := @SYSHOST@
SILENCE_SHELL_DEBUGGING   := @SILENCE_SHELL_DEBUGGING@
SYS_LD_LIB_PATH           := @SYS_LD_LIB_PATH@
SYS_LD_PRELOAD            := @SYS_LD_PRELOAD@
SYS_LUA_PATH              := @SYS_LUA_PATH@
SYS_LUA_CPATH             := @SYS_LUA_CPATH@
CASE_INDEPENDENT_SORTING  := @CASE_INDEPENDENT_SORTING@
ZSH_SITE_FUNCTIONS_DIRS   := @ZSH_SITE_FUNCTIONS_DIRS@
SPIDER_CACHE_DESCRIPT_FN  := @SPIDER_CACHE_DESCRIPT_FN@
ANCIENT                   := @ANCIENT@
ALLOW_TCL_MFILES          := @ALLOW_TCL_MFILES@
MPATH_AVAIL               := @MPATH_AVAIL@
EXTENDED_DEFAULT          := @EXTENDED_DEFAULT@
TMOD_PATH_RULE            := @TMOD_PATH_RULE@
TMOD_FIND_FIRST           := @TMOD_FIND_FIRST@
CACHED_LOADS              := @CACHED_LOADS@
EXACT_MATCH               := @EXACT_MATCH@
DUPLICATE_PATHS           := @DUPLICATE_PATHS@
DISABLE_NAME_AUTOSWAP     := @DISABLE_NAME_AUTOSWAP@
SHORT_TIME                := @SHORT_TIME@
PIN_VERSIONS              := @PIN_VERSIONS@
AUTO_SWAP                 := @AUTO_SWAP@
SPIDER_CACHE_DIRS         := @SPIDER_CACHE_DIRS@
LEGACY_ORDERING           := @LEGACY_ORDERING@
EXPORT_MODULE             := @EXPORT_MODULE@
BASENAME                  := @BASENAME@
UPDATE_VERSION            := $(srcdir)/proj_mgmt/updateVersion
PS                        := @PS@
READLINK                  := @READLINK@
EXPR                      := @EXPR@
PATH_TO_HASHSUM           := @PATH_TO_HASHSUM@
PATH_TO_LUAC              := @PATH_TO_LUAC@
PATH_TO_PAGER             := @PATH_TO_PAGER@
PATH_TO_TCLSH             := @PATH_TO_TCLSH@
PATH_TO_LS                := @PATH_TO_LS@
MODULEPATH_ROOT           := @MODULEPATH_ROOT@
VERSION_SRC               := $(srcdir)/src/Version.lua
LUA_INCLUDE               := @LUA_INCLUDE@
UPDATE_SYSTEM_FN          := @UPDATE_SYSTEM_FN@
GIT_PROG                  := @PATH_TO_GIT@
GIT_VERSION               := $(shell if [ -n "$(GIT_PROG)" -a -d .git ]; then lmodV=`git describe --always`; echo "($$lmodV)"; else echo "($(version))"; fi)
PKG                       := $(MY_PKG_PACKAGE)
PKGV                      := $(MY_PACKAGE)
LIB                       := $(MY_PACKAGE)/lib
LIBEXEC                   := $(MY_PACKAGE)/libexec
SHELLS                    := $(MY_PACKAGE)/shells
TOOLS                     := $(MY_PACKAGE)/tools
I18N                      := $(MY_PACKAGE)/tools/i18n
SETTARG                   := $(MY_PACKAGE)/settarg
INIT                      := $(MY_PACKAGE)/init
INIT_KSH_FUNCS            := $(MY_PACKAGE)/init/ksh_funcs
FISH_TAB                  := $(MY_PACKAGE)/init/fish_tab_completion
MESSAGEDIR                := $(MY_PACKAGE)/messageDir
LMOD_MF                   := $(MY_PACKAGE)/modulefiles/Core
MAN_PAGES                 := $(MY_PACKAGE)/share/man/cat1
LMOD_MF_SOURCE            := $(patsubst %, $(srcdir)/%, MF/*.version.lua)
SETTARG_SOURCE            := $(patsubst %, $(srcdir)/%, settarg/*.lua  settarg/targ.in)
DATE_cmd                  := $(srcdir)/proj_mgmt/DATE_cmd.sh
UNAME_S                   := $(shell uname -s)
ifeq ($(UNAME_S),Darwin)
  SED      := gsed
else
  SED      := sed
endif

ifeq ($(LUA_INCLUDE),no)
   LUA_INCLUDE :=
else
   LUA_INCLUDE := -I$(LUA_INCLUDE)
endif



DIRLIST                   := $(DESTDIR)$(LIBEXEC) $(DESTDIR)$(TOOLS)      $(DESTDIR)$(SETTARG)        \
                             $(DESTDIR)$(SHELLS)  $(DESTDIR)$(INIT)       $(DESTDIR)$(LIB)            \
                             $(DESTDIR)$(LMOD_MF) $(DESTDIR)$(MAN_PAGES)  $(DESTDIR)$(MESSAGEDIR)     \
                             $(DESTDIR)$(I18N)    $(DESTDIR)$(FISH_TAB)   $(DESTDIR)$(INIT_KSH_FUNCS)

STANDALONE_PRGM           := src/lmod.in.lua src/addto.in.lua                       \
                             src/spider.in.lua src/clearLMOD_cmd.in.lua             \
                             src/ml_cmd.in.lua                                      \
                             src/spiderCacheSupport.in.lua                          \
                             src/sh_to_modulefile.in.lua                            \
                             src/update_lmod_system_cache_files.in                  \
                             src/check_module_tree_syntax.in.lua                    \
                             sh_src/print_os.sh.in 
STANDALONE_PRGM           := $(patsubst %, $(srcdir)/%, $(STANDALONE_PRGM))
SHELL_INIT                := bash.in cmake csh.in ksh.in tcsh.in zsh.in sh.in perl R \
                             env_modules_python.py.in lmod_bash_completions fish.in  \
                             lisp env_modules_ruby.rb.in rc.in
SHELL_INIT                := $(patsubst %, $(srcdir)/init/%, $(SHELL_INIT))
LMODRC_INIT               := $(patsubst %, $(srcdir)/init/%, lmodrc.lua)


ZSH_FUNCS                 := _ml _module
ZSH_FUNCS                 := $(patsubst %, $(srcdir)/init/zsh/%, $(ZSH_FUNCS))

FISH_FUNCS                := module.fish   
FISH_FUNCS                := $(patsubst %, $(srcdir)/init/fish/%, $(FISH_FUNCS))

KSH_FUNCS                 := clearLmod clearMT ml module settarg
KSH_FUNCS                 := $(patsubst %, $(srcdir)/init/ksh_funcs/%, $(KSH_FUNCS))

STARTUP                   := profile.in profile.fish.in profile.rc.in cshrc.in
STARTUP                   := $(patsubst %, $(srcdir)/init/%, $(STARTUP))

MSGFNs                    := $(wildcard $(srcdir)/messageDir/*.lua)

MAIN_DIR                  := Makefile.in INSTALL configure README_lua_modulefiles.txt \
                             README README.old License

CONTRIB_DIRS              :=  \
                              BuildSystemCacheFile              \
                              Deprecating_Modules               \
                              SitePackage                       \
                              TACC                              \
                              bash_patch                        \
                              converting_shell_to_module_files  \
                              hook                              \
                              parseVersions                     \
                              settarg                           \
                              tracking_module_usage             \
                              tricky_modulefiles

CONTRIB                   := $(patsubst %, $(srcdir)/contrib/%, $(CONTRIB_DIRS))
lua_code                  := $(filter-out %.in.lua, $(wildcard $(srcdir)/src/*.lua))   \
                             $(wildcard $(srcdir)/src/*.tcl)
VDATE                     := $(shell $(DATE_cmd) +'%F %H:%M %:z')

ComputeHashSum            := $(srcdir)/src/computeHashSum.in.lua
spiderCacheSupportCMD     := $(PATH_TO_LUA) $(DESTDIR)$(LIBEXEC)/spiderCacheSupport
export L_PATH              =  $(LUA_PATH)
export L_CPATH             =  $(LUA_CPATH)

HAVE_LUA_TERM             := @HAVE_LUA_TERM@
ifneq ($(HAVE_LUA_TERM),yes)
  PKGS := pkgs
endif
HAVE_LUAFILESYSTEM        := @HAVE_LUAFILESYSTEM@
ifneq ($(HAVE_LUAFILESYSTEM),yes)
  PKGS := $(PKGS) lfs
  PKG_LFS := lfs
endif
FAST_TCL_INTERP           := @FAST_TCL_INTERP@
TCL_INCLUDE     	  := @TCL_INCLUDE@
TCL_LIBS	          := @TCL_LIBS@
ifeq ($(FAST_TCL_INTERP),yes)
  PKGS    := $(PKGS) tcl2lua
  PKG_T2L := tcl2lua
endif

.PHONY: test pkgs man_pages

all:
	@echo done

uninstall:
	$(RM) -rf $(DESTDIR)$(MY_PKG_PACKAGE)
	@echo "If you added lmod files to /etc/profile.d please remove them."

pre-install: $(DIRLIST) lmod_install_targets

lmod_install_targets: $(PKGS) lmodrc_init shell_init startup libexec               \
                      Inst_Tools Inst_Shells Inst_Settarg Inst_Lmod_MF other_tools \
                      man_pages messageFns i18n spiderCacheSupport ksh_funcs

install: pre-install  zsh_tab_funcs fish_tab_funcs 
	$(MAKE) PKGV=$(PKG) shell_init startup
	@if test "$(SITE_CONTROLLED_PREFIX)" = "no" ; then                        \
	   echo $(RM) $(DESTDIR)$(PKG);                                           \
	   $(RM) $(DESTDIR)$(PKG);                                                \
	   echo ln -s $(version) $(DESTDIR)$(PKG);                                \
	   ln -s $(version) $(DESTDIR)$(PKG);                                     \
	else                                                                      \
	  echo '**************************************************************';  \
	  echo '';                                                                \
	  echo 'Warning: remember to update the Lmod scripts in /etc/profile.d';  \
	  echo '';                                                                \
	  echo '**************************************************************';  \
	fi;

echo:
	@echo Version: $(version)
	@echo IGNORE_DIRS: $(IGNORE_DIRS)
	@echo LUA_PATH: $$LUA_PATH
	@echo L_PATH: "$(L_PATH)"
	@echo VDATE: $(VDATE)
	@echo DIRLIST: $(DIRLIST)

man_pages:
	-$(PATH_TO_LUA) $(srcdir)/src/lmod.in.lua bash --help >/dev/null 2> $(DESTDIR)$(MAN_PAGES)/module.1

$(DIRLIST) :
	mkdir -p $@

__installMe:
	-for i in $(FILELIST); do                                                 \
	   bareN=$${i##*/};                                                       \
	   fn=$${bareN%%.in*};                                                    \
	   ext=$${bareN#*.};                                                      \
	   : echo "DIRLOC/fn: $(DIRLOC)/$$fn";                                    \
	   sed  -e 's|@PREFIX@|$(prefix)|g'                                       \
	        -e 's|@path_to_lua@|$(PATH_TO_LUA)|g'                             \
	        -e 's|@hashsum@|$(PATH_TO_HASHSUM)|g'                             \
	        -e 's|@pager@|$(PATH_TO_PAGER)|g'                                 \
	        -e 's|@case_independent_sorting@|$(CASE_INDEPENDENT_SORTING)|g'   \
	        -e 's|@lmod_settarg_full_support@|$(LMOD_SETTARG_FULL_SUPPORT)|g' \
	        -e 's|@use_dot_files@|$(USE_DOT_FILES)|g'                         \
	        -e 's|@lang@|$(LMOD_OVERRIDE_LANG)|g'                             \
	        -e 's|@site_msg_file@|$(SITE_MSG_FILE)|g'                         \
	        -e 's|@git@|$(GIT_VERSION)|g'                                     \
	        -e 's|--@ignore_dirs@--|$(IGNORE_DIRS)|g'                         \
	        -e 's|@sys_lua_path@|$(SYS_LUA_PATH)|g'                           \
	        -e 's|@hidden_italic@|$(LMOD_HIDDEN_ITALIC)|g'                    \
	        -e 's|@modulepath_init@|$(MODULEPATH_INIT)|g'                     \
	        -e 's|@sys_lua_cpath@|$(SYS_LUA_CPATH)|g'                         \
	        -e 's|@path_to_luac@|$(PATH_TO_LUAC)|g'                           \
	        -e 's|@path_to_lmod@|$(LIBEXEC)/lmod|g'                           \
	        -e 's|@have_lua_term@|$(HAVE_LUA_TERM)|g'                         \
	        -e 's|@silence_shell_debugging@|$(SILENCE_SHELL_DEBUGGING)|g'     \
	        -e 's|@fast_tcl_interp@|$(FAST_TCL_INTERP)|g'                     \
	        -e 's|@settarg_cmd@|$(SETTARG_CMD)|g'                             \
	        -e 's|@lmod_config_dir@|$(LMOD_CONFIG_DIR)|g'                     \
	        -e "s|@my_shell@|$$fn|g"                                          \
	        -e 's|@tclsh@|$(PATH_TO_TCLSH)|g'                                 \
	        -e 's|@ls@|$(PATH_TO_LS)|g'                                       \
	        -e 's|@disable_name_autoswap@|$(DISABLE_NAME_AUTOSWAP)|g'         \
	        -e 's|@tmod_path_rule@|$(TMOD_PATH_RULE)|g'                       \
	        -e 's|@tmod_find_first@|$(TMOD_FIND_FIRST)|g'                     \
	        -e 's|@redirect@|$(REDIRECT)|g'                                   \
	        -e 's|@basename@|$(BASENAME)|g'                                   \
	        -e 's|@support_ksh@|$(SUPPORT_KSH)|g'                             \
	        -e 's|@cached_loads@|$(CACHED_LOADS)|g'                           \
	        -e 's|@avail_extensions@|$(LMOD_AVAIL_EXTENSIONS)|g'              \
	        -e 's|@ps@|$(PS)|g'                                               \
	        -e 's|@readlink@|$(READLINK)|g'                                   \
	        -e 's|@site_name@|$(SITE_NAME)|g'                                 \
	        -e 's|@site_controlled_prefix@|$(SITE_CONTROLLED_PREFIX)|g'       \
	        -e 's|@syshost@|$(SYSHOST)|g'                                     \
	        -e 's|@expr@|$(EXPR)|g'                                           \
	        -e 's|@extended_default@|$(EXTENDED_DEFAULT)|g'                   \
	        -e 's|@libexec@|$(LIBEXEC)|g'                                     \
	        -e 's|@legacy_ordering@|$(LEGACY_ORDERING)|g'                     \
	        -e 's|@pin_versions@|$(PIN_VERSIONS)|g'                           \
	        -e 's|@auto_swap@|$(AUTO_SWAP)|g'                                 \
	        -e 's|@sys_ld_lib_path@|$(SYS_LD_LIB_PATH)|g'                     \
	        -e 's|@sys_ld_preload@|$(SYS_LD_PRELOAD)|g'                       \
	        -e 's|@export_module@|$(EXPORT_MODULE)|g'                         \
	        -e 's|@lmod_version@|$(version)|g'                                \
	        -e 's|@exact_match@|$(EXACT_MATCH)|g'                             \
	        -e 's|@ancient@|$(ANCIENT)|g'                                     \
	        -e 's|@prepend_block@|$(PREPEND_BLOCK)|g'                         \
	        -e 's|@colorize@|$(COLORIZE)|g'                                   \
	        -e 's|@duplicate_paths@|$(DUPLICATE_PATHS)|g'                     \
	        -e 's|@allow_tcl_mfiles@|$(ALLOW_TCL_MFILES)|g'                   \
	        -e 's|@mpath_avail@|$(MPATH_AVAIL)|g'                             \
	        -e 's|@short_time@|$(SHORT_TIME)|g'                               \
	        -e 's|@cacheDirs@|$(SPIDER_CACHE_DIRS)|g'                         \
	        -e 's|@updateSystemFn@|$(UPDATE_SYSTEM_FN)|g'                     \
	        -e 's|@modulepath_root@|$(MODULEPATH_ROOT)|g'                     \
	        -e 's|@lmod_root@|$(LMOD_ROOT)|g'                                 \
	        -e 's|@lmod_allow_root_use@|$(LMOD_ALLOW_ROOT_USE)|g'             \
	        -e 's|@PKGV@|$(PKGV)|g'                                           \
	        -e 's|@PKG@|$(PKG)|g'         < $$i > $(DIRLOC)/$$fn;             \
	    [ "$$ext" = "in.lua" -o "$$ext" = "tcl" -o "$$ext" = "in"             \
	       -o "$$ext" = "sh.in" ] &&                                          \
	       chmod +x $(DIRLOC)/$$fn;                                           \
	    if [ "$$ext" = "version.lua" ]; then                                  \
	       mname=$${bareN%%.*};                                               \
	       : echo "DIRLOC: $(DIRLOC)/$$mname";                                \
	       mv $(DIRLOC)/$$fn $(DIRLOC)/$$mname.lua;                           \
	    fi;                                                                   \
	done

generate_doc:
	ldoc .

shell_init: $(SHELL_INIT)
	$(MAKE) FILELIST="$^" DIRLOC=$(DESTDIR)$(INIT)    __installMe

lmodrc_init: $(LMODRC_INIT)
	$(MAKE) FILELIST="$^" DIRLOC=$(DESTDIR)$(INIT)    __installMe

messageFns: $(MSGFNs)
	$(MAKE) FILELIST="$^" DIRLOC=$(DESTDIR)$(MESSAGEDIR) __installMe

i18n: $(I18N_SRC)
	$(MAKE) FILELIST="$^" DIRLOC=$(DESTDIR)$(I18N) __installMe

startup: $(STARTUP)
	$(MAKE) FILELIST="$^" DIRLOC=$(DESTDIR)$(INIT)    __installMe

other_tools: $(ComputeHashSum) $(STANDALONE_PRGM)
	$(MAKE) FILELIST="$^" DIRLOC=$(DESTDIR)$(LIBEXEC) __installMe

spiderCacheSupport: $(PKG_LFS) $(PKG_T2L) Inst_Tools libexec
	$(spiderCacheSupportCMD) --cacheDirs "$(SPIDER_CACHE_DIRS)"          \
	                         --updateFn  "$(UPDATE_SYSTEM_FN)"           \
                                 --descriptFn "$(SPIDER_CACHE_DESCRIPT_FN)"  \
                                 >> $(DESTDIR)$(INIT)/lmodrc.lua;

src/computeHashSum: $(ComputeHashSum)
	$(MAKE) FILELIST="$^" DIRLOC="$(prefix)/src"                __installMe
	chmod +x $@

tcl2lua:
	if [ -d $(srcdir)/pkgs/tcl2lua ]; then                             \
           $(MAKE) -C $(srcdir)/pkgs/tcl2lua  LUA_INC=$(LUA_INCLUDE)       \
	        TCL_INCLUDE=$(TCL_INCLUDE) TCL_LIBS=$(TCL_LIBS)            \
                LIB=$(DESTDIR)$(LIB) LIBS=@LIBS@ CC="$(CC)"                \
                SHARE=$(DESTDIR)$(LIBEXEC)                                 \
                install;                                                   \
        fi

lfs:
	if [ -d $(srcdir)/pkgs/luafilesystem ]; then                       \
           $(MAKE) -C $(srcdir)/pkgs/luafilesystem  LUA_INC=$(LUA_INCLUDE) \
                LIB=$(DESTDIR)$(LIB) SHARE=$(DESTDIR)$(LIBEXEC) CC="$(CC)" \
                install;                                                   \
        fi
pkgs:
	if [ -d $(srcdir)/pkgs ]; then                                     \
           $(MAKE) -C $(srcdir)/pkgs LUA_INC=$(LUA_INCLUDE)                \
               LIB=$(DESTDIR)$(LIB) SHARE=$(DESTDIR)$(LIBEXEC) CC="$(CC)"  \
               install;                                                    \
        fi

zsh_tab_funcs: $(ZSH_FUNCS) $(KSH_FUNCS)
	-if [ -n "$(ZSH_SITE_FUNCTIONS_DIRS)" ]; then                       \
	  for zdir in `echo "$(ZSH_SITE_FUNCTIONS_DIRS)" | tr ':' ' '`; do  \
	    for i in $^; do                                                 \
	      bareN=$${i##*/};                                              \
	      fn=$${bareN%%.in*};                                           \
	      ext=$${bareN#*.};                                             \
	      cp $$i $(DESTDIR)$$zdir/$$fn  2> /dev/null;                   \
	      [ "$$ext" = "in" ] && chmod +x $(DESTDIR)$$zdir/$$fn;         \
	    done;                                                           \
	  done;                                                             \
	  true;                                                             \
	fi

ksh_funcs: $(KSH_FUNCS)
	$(MAKE) FILELIST="$^" DIRLOC=$(DESTDIR)$(INIT_KSH_FUNCS) __installMe

fish_tab_funcs: $(FISH_FUNCS)
	cp $^ $(DESTDIR)$(FISH_TAB) 2>/dev/null

makefile: $(srcdir)/Makefile.in ./config.status
	./config.status $@

config.status:
	./config.status --recheck

trailing_blanks_removed:
	find . -type d \( -path ./rt -o -path ./spec/Spider/h \) -prune -o -name '*.lua' -exec $(SED) -i -e 's/  *$$//' {} \;

dist:
	GIT_BRANCH=`git status | head -n 1 | sed -e 's/^[# ]*On branch //g' -e 's/^[# ]*HEAD detached at//g'`; \
	if [ "$$GIT_BRANCH" = "Not currently on any branch." ]; then GIT_BRANCH=master; fi; \
	echo "GIT_BRANCH: $$GIT_BRANCH"                                            ; \
	git archive --prefix=Lmod-$(version)/ $$GIT_BRANCH  > Lmod-$(version).tar  ; \
	echo "ls -l Lmod-$(version).tar"                                           ; \
	ls -l Lmod-$(version).tar                                                  ; \
	$(RM) -rf DIST                                                             ; \
	mkdir DIST                                                                 ; \
	cd DIST                                                                    ; \
	tar xf ../Lmod-$(version).tar                                              ; \
	$(RM) ../Lmod-$(version).tar                                               ; \
	echo "Lmod"-$(version) > .fname                                            ; \
	sed -i.bk 's/\@git\@/$(GIT_VERSION)/g' `cat .fname`/src/Version.lua        ; \
	rm `cat .fname`/src/Version.lua.bk                                         ; \
	tar chf `cat .fname`.tar `cat .fname`                                      ; \
	bzip2 `cat .fname`.tar                                                     ; \
	mv `cat .fname`.tar.bz2  ..                                                ; \
	cd ..; $(RM) -rf DIST

ml_dist:
	$(MAKE) DistD=DIST _ml_dist

_ml_dist: _distMkDir
	cp src/ml_cmd.in.lua ML_README.txt $(DistD)
	echo "ml"-$(version) > .fname;                                     \
	$(RM) -r `cat .fname` `cat .fname`.tar*;                           \
	mv ${DistD} `cat .fname`;                                          \
	tar chf `cat .fname`.tar `cat .fname`;                             \
	bzip2 `cat .fname`.tar;                                            \
	rm -rf `cat .fname` .fname;

test:
	cd rt; unset TMFuncPATH; tm .

tags: build_tags
	$(RM) file_list.*

build_tags:
	find . \( -regex '.*~$$\|.*/\.git\|.*/\.git/' -prune \)  \
	       -o -type f > file_list.1.txt
	sed -e 's|.*/.git.*||g'                                  \
	    -e 's|.*/embed/.*||g'                                \
	    -e 's|.*/rt/.*/t1/.*||g'                             \
	    -e 's|./rt/funny_files/mf/.*||g'                     \
	    -e 's|.*/html/.*$$||g'                               \
	    -e 's|./TAGS$$||g'                                   \
	    -e 's|./TAGS.world$$||g'                             \
	    -e 's|./makefile||g'                                 \
	    -e 's|./configure$$||g'                              \
	    -e 's|.*\.DS_Store$$||g'                             \
	    -e 's|.*\.tgz$$||g'                                  \
	    -e 's|.*\.tar\.gz$$||g'                              \
	    -e 's|.*\.tar\.bz2$$||g'                             \
	    -e 's|.*\.csv$$||g'                                  \
	    -e 's|.*\.aux$$||g'                                  \
	    -e 's|.*\.fdb_latexmk$$||g'                          \
	    -e 's|.*\.fls$$||g'                                  \
	    -e 's|.*\.nav$$||g'                                  \
	    -e 's|.*\.out$$||g'                                  \
	    -e 's|.*\.new.lua$$||g'                              \
	    -e 's|.*\.pdf$$||g'                                  \
	    -e 's|.*\.pyc$$||g'                                  \
	    -e 's|.*db\.conf$$||g'                               \
	    -e 's|.*\.o$$||g'                                    \
	    -e 's|.*\.so\.$$||g'                                 \
	    -e 's|.*\.so$$||g'                                   \
	    -e 's|.*\.pyo$$||g'                                  \
	    -e 's|.*\.snm$$||g'                                  \
	    -e 's|.*\.toc$$||g'                                  \
	    -e 's|.*\.sty$$||g'                                  \
	    -e 's|.*\.vrb$$||g'                                  \
	    -e 's|.*\.key$$||g'                                  \
	    -e 's|^#.*||g'                                       \
	    -e 's|.*/#.*||g'                                     \
	    -e 's|\.#.*||g'                                      \
	    -e 's|.*/\.#.*||g'                                   \
	    -e 's|.*\.pdf$$||g'                                  \
	    -e 's|.*\.used$$||g'                                 \
	    -e 's|./.*\.log$$||g'                                \
	    -e 's|./testreports/.*||g'                           \
	    -e 's|./docs/build/.*||g'                            \
	    -e 's|./config\.status$$||g'                         \
	    -e 's|.*\~$$||g'                                     \
	    -e 's|./lmod.check.txt*||g'                          \
	    -e 's|./file_list\..*||g'                            \
	    -e '/^\s*$$/d'                                       \
	       < file_list.1.txt > file_list.2.txt
	etags  -o TAGS.world `cat file_list.2.txt`
	sed -e 's|.*/rt/.*||g'                                   \
	    -e 's|.*/contrib/.*||g'                              \
	    -e 's|.*/cookbook/.*||g'                             \
	    -e 's|.*/docs/.*||g'                                 \
	    -e 's|.*/my_docs/.*||g'                              \
	    -e 's|.*/spec/.*/mf/.*||g'                           \
	    -e 's|.*/spec/.*/mf2/.*||g'                          \
	    -e 's|.*/spec/.*/nv/.*||g'                           \
	    -e 's|.*/spec/.*/nv2/.*||g'                          \
	    -e '/^\s*$$/d'                                       \
	       < file_list.2.txt > file_list.3.txt
	etags `cat file_list.3.txt`

busted:
        ifeq ($(TAG),)
	  @PROJDIR=$(CURDIR) busted            --verbose -m "$(CURDIR)/src/?.lua;$(CURDIR)/tools/?.lua;$(CURDIR)/tools/?/init.lua"
        else
	  @PROJDIR=$(CURDIR) busted  -t $(TAG) --verbose -m "$(CURDIR)/src/?.lua;$(CURDIR)/tools/?.lua;$(CURDIR)/tools/?/init.lua"
        endif

luachk:
	luacheck --exclude-files src/*.new.lua -- src shells tools settarg spec 2>&1 | tee lmod.check.txt; \
	if [ "$$?" -eq 0 ]; then rm lmod.check.txt; fi

libexec:  $(lua_code)
	$(MAKE) FILELIST="$^" DIRLOC=$(DESTDIR)$(LIBEXEC) __installMe

Inst_Tools: $(TOOL_SRC)
	$(MAKE) FILELIST="$^" DIRLOC=$(DESTDIR)$(TOOLS) __installMe

Inst_Shells: $(srcdir)/shells/*.lua
	$(MAKE) FILELIST="$^" DIRLOC=$(DESTDIR)$(SHELLS) __installMe

Inst_Settarg: $(SETTARG_SOURCE)
	$(MAKE) FILELIST="$^" DIRLOC=$(DESTDIR)$(SETTARG) __installMe


Inst_Lmod_MF: $(LMOD_MF_SOURCE)
	$(MAKE) FILELIST="$^" DIRLOC=$(DESTDIR)$(LMOD_MF) __installMe

clean:
	$(RM) config.log *.o
	cd $(srcdir)/pkgs; $(MAKE) LIB=$(DESTDIR)$(LIB) SHARE=$(DESTDIR)$(LIBEXEC) clean
	cd $(srcdir)/pkgs/luafilesystem; $(MAKE) LIB=$(DESTDIR)$(LIB) clean
	cd $(srcdir)/pkgs/tcl2lua; $(MAKE) LIB=$(DESTDIR)$(LIB) clean

clobber: clean

distclean: clobber
	$(RM) makefile config.status

world_update:
	@git status -s > /tmp/git_st_$$$$;                                             \
	if [ -s /tmp/git_st_$$$$ ]; then                                               \
	    echo "All files not checked in => try again";                              \
	elif [ $(srcdir)/configure -ot $(srcdir)/configure.ac ]; then                  \
	    echo "configure is out of date => try again";                              \
	else                                                                           \
	    branchName=`git status | head -n 1 | sed 's/^[# ]*On branch //g'`;         \
	    git push        github $$branchName;                                       \
	    git push --tags github $$branchName;                                       \
	    pid=$$$$;                                                                  \
	    echo '{'                                               >  /tmp/json_$$pid; \
	    echo "  \"tag_name\":         \"$(version)\","         >> /tmp/json_$$pid; \
	    echo "  \"target_commitish\": \"$$branchName\","       >> /tmp/json_$$pid; \
	    echo "  \"name\":             \"$(version)\","         >> /tmp/json_$$pid; \
	    echo "  \"body\":             \"\","                   >> /tmp/json_$$pid; \
	    echo "  \"draft\":            false,"                  >> /tmp/json_$$pid; \
	    echo "  \"prelease\":         false"                   >> /tmp/json_$$pid; \
	    echo '}'                                               >> /tmp/json_$$pid; \
	    URL=https://api.github.com/repositories/9381512/releases;                  \
	    TOKEN=$$(cat ~/c/lmod_github_token.txt);                                   \
	    curl --user rtmclay:token -H "Authorization: token $$TOKEN" -X POST        \
	         --data @/tmp/json_$$pid $$URL;                                        \
	fi;                                                                            \
	rm -f /tmp/json_$$pid /tmp/git_st_$$$$;

gittag:
        ifneq ($(TAG),)
	  @git status -s > /tmp/git_st_$$$$;                                                \
	  if [ -s /tmp/git_st_$$$$ ]; then                                                  \
	    echo "All files not checked in => try again";                                   \
	  elif [ $(srcdir)/configure -ot $(srcdir)/configure.ac ]; then                     \
	    echo "configure is out of date => try again";                                   \
	  else                                                                              \
	    $(RM)                                                           $(VERSION_SRC); \
	    echo 'local M={}'                                            >  $(VERSION_SRC); \
	    echo 'function M.tag()  return "$(TAG)"   end'               >> $(VERSION_SRC); \
	    echo 'function M.git()'                                      >> $(VERSION_SRC); \
	    echo '   local s = "@git@"'                                  >> $(VERSION_SRC); \
	    echo '   if (s == "@" .. "git@")     then s = "" end'        >> $(VERSION_SRC); \
	    echo '   if (s == "("..M.tag()..")") then s = "" end'        >> $(VERSION_SRC); \
	    echo '   return s'                                           >> $(VERSION_SRC); \
	    echo 'end'                                                   >> $(VERSION_SRC); \
	    echo 'function M.date() return "$(VDATE)" end'               >> $(VERSION_SRC); \
	    echo 'function M.name()'                                     >> $(VERSION_SRC); \
	    echo '  local a = {}'                                        >> $(VERSION_SRC); \
	    echo '  a[#a+1] = M.tag()'                                   >> $(VERSION_SRC); \
	    echo '  a[#a+1] = M.git()'                                   >> $(VERSION_SRC); \
	    echo '  a[#a+1] = M.date()'                                  >> $(VERSION_SRC); \
	    echo '  return table.concat(a," ")'                          >> $(VERSION_SRC); \
	    echo 'end'                                                   >> $(VERSION_SRC); \
	    echo 'return M'                                              >> $(VERSION_SRC); \
	    $(UPDATE_VERSION) -p $(CONF_PY_PATTERN)          --version $(TAG)   $(CONF_PY); \
	    git commit -m "moving to TAG_VERSION $(TAG)"         $(CONF_PY) $(VERSION_SRC); \
	    git tag -a $(TAG) -m 'Setting TAG_VERSION to $(TAG)'                          ; \
	    branchName=`git status | head -n 1 | sed 's/^[# ]*On branch //g'`             ; \
	    git push        origin $$branchName                                           ; \
	    git push --tags origin $$branchName                                           ; \
	  fi                                                                              ; \
	  rm -f /tmp/git_st_$$$$                                                          ;
        else
	  @echo "To git tag do: make gittag TAG=?"
        endif                                                                               
