##############################################################################
# File: rules.std
# Compat for older (embedded) makefiles
##############################################################################
#

SCM_ROOT	= $(GBE_ROOT)
SCM_TYPE	= $(GBE_TYPE)
SCM_BIN 	= $(GBE_BIN)
SCM_PLATFORM	= $(GBE_PLATFORM)

#
###############################################################################
# Common definitions extracted from OPTIONS
##############################################################################
#
CC_PRE      := @
XX_PRE      := @
AA_PRE      := @

SHOWARGS    := NONE

USE_FILTER  := 1
LEAVETMP    :=
SHOWENV     :=

ifdef OPTIONS

ifneq "$(findstring args,$(OPTIONS))" ""        # Show arguments (default)
CC_PRE :=
XX_PRE :=
SHOWARGS := ARGS
endif

ifneq "$(findstring noargs,$(OPTIONS))" ""      # Show no arguments
CC_PRE := @
XX_PRE := @
SHOWARGS := NONE
endif

ifneq "$(findstring allargs,$(OPTIONS))" ""     #  Show ALL arguments
AA_PRE :=
SHOWARGS := ALL
endif

ifneq "$(findstring filter,$(OPTIONS))" ""      # Use filter (Default)
USE_FILTER := 1
endif

ifneq "$(findstring nofilter,$(OPTIONS))" ""    # Don't use filter
USE_FILTER  :=
endif

ifneq "$(findstring leavetmp,$(OPTIONS))" ""    # Leave temp files
LEAVETMP := 1
endif

ifneq "$(findstring noleavetmp,$(OPTIONS))" ""  # Don't leave temp files (Default)
LEAVETMP :=
endif

ifneq "$(findstring showenv,$(OPTIONS))" ""     # Display env before commands
SHOWENV := 1
endif

ifneq "$(findstring noshowenv,$(OPTIONS))" ""   # Don't display env before commands
SHOWENV :=
endif

endif

###############################################################################
# Some old VIX  uses have the following EnvVars set. These can confuse the
# gnu autoconfigure  process. Ensure that they are not set

unexport CC CXX CPP CPP_LINK CPPFLAGS
#unexport LD_LIBRARY_PATH

#
###############################################################################
# Brain hurts stuff... $(space) becomes a macro containing 1 space
# other aren't so bad.  These are used to make argument lists is a few 
# places where we have to translate from a space seperated list to a 
# something else seperated list (or vicky verka).
###############################################################################
#
comma		:= ,
plus		:= +
semicolon	:= ;
empty		:= 
space		:= $(empty) $(empty)
spacealt	:= %20

#
###############################################################################
# Macros to assist in the handling of paths with spaces
#	encodepath - Uses spacealt, uses /, space-sep-list
#		     Used to conatins lists of paths that may contain
#		     spaces. Used for INCLUDE and LIB paths
#
#	decodepath - Decodes an encoded path-list.
#		     User specifies the item prefix and suffix.
#		     Return list uses \ and may have spaces in pathnames
#	             Prefix items with 1st-arg
#                    Join items with 2nd arg
#
# Usage:
#	aa = $(call encodepath,c:\Program Files)
#	$(call decodepath,-I,;,$(aa))
#
###############################################################################
#
encodepath = $(subst //,/,$(subst \,/,$(subst $(space),$(spacealt),$(strip $(1)))))
decodepath = $(subst $(spacealt),$(space),$(subst $(space),$(empty),$(patsubst %,$(1)%$(2),$(subst /,\,$(3)))))

#
###############################################################################
#
#  Macros to convert relative to absolute paths
#
#   Work around for a bug in make 3.81 where abspath assumes that the input
#   is a relative path. The work around checks for paths that have a : or start
#   with a '/ - and assumes these are absolute.
#   It will only call makes 'abspath' for relative paths.
#
###############################################################################
#
myabspath = $(foreach dir,$1,$(call myabs1path,$(dir)))
myabs1path = $(if $(or $(findstring :,$1),$(filter /%,$1)),$1,$(abspath $1))

#
###############################################################################
# A little macro used within each of the rules for translating a file from 
# one type to another to print the environment if the variable SHOWENV has 
# been defined.
###############################################################################
#
ifdef SHOWENV
define show_environment
		$(printenv)
endef
else
define show_environment
endef
endif

#
################################################################################
# A macro to dump out command files if the ARGS or ALLARGS option is set
# Usage: $(call show_cmdfile,<PathOfCommandFile>)
################################################################################
#
ifeq ($(SHOWARGS),NONE)
show_cmdfile =
else
show_cmdfile = echo "Using command file: $(1)";cat $(1);echo "End of Command file"
endif

#
###############################################################################
# Support macros
###############################################################################
#

#..	Invoke RunTime support operations
#
#	Usage:
#		$(JatsRunTime) function_name -- args
#       Notes:
#		Don't use " to quote arguments as it will invoke a shell
#		under windows (and others). This can only slow it down.
#		Using ' is OK
#
define	JatsRunTime
    $(GBE_PERL) -Mjats_runtime -e
endef

#.. Remove files contained within the specified list
#   Used internally to delete generated files
#
#   Usage:      $(call RmFiles,VARIABLE_NAME)
#
define RmFiles
	$(AA_PRE)JatsFileUtil 'r0' '' $($1)
endef

#.. Install/uninstall the specified file
#
#   Usage:      $(call InstallFile,dest,file,fmode)
#               $(call UninstallFile,file)
#
define InstallFile
    $(call CopyFile,installing,$1,$2,$3,)
endef

define UninstallFile
    $(call UncopyFile,uninstalling,$1)
endef

#.. Package/Unpackage the specified file
#
#   Usage:      $(call PackageFile,dest,file,fmode)
#               $(call UnpackageFile,file)
#
define PackageFile
    $(call CopyFile,packaging,$1,$2,$3,)
endef

define UnpackageFile
    $(call UncopyFile,unpackaging,$1)
endef

#.. Generic Copy/Remove the specified file
#
#   Usage:      $(call CopyFile,Text,dest,file,fmode)
#               $(call UncopyFile,Text,file)
#
#   Notes:
#       dest, file and path
#	May have spaces in them
#		These will have been escaped with a '\' which will
#		need to be removed. The '\ ' is required to keep make
#		happy elsewhere.
#
#	Current implementation uses a dedicated JATS-internal program to perform
#	the operations. This is much better than the previous implementation
#	which was shell-based. Its problems included:
#		1) Very slow under Windows
#		2) Special handling on $
#		3) Windows Path length limitation of < ~260 characters
#
#	Implementation note:
#		1) Do not get the shell involved in invoking the command
#	           Quote args in '' not "" as "" will trigger shell usage
#		   This is good as we don't need/want shell expansion either
#		   Shell startup is slow under windows.
#		2) Paths use '/'. This is passed though to the utility
#
#	Macros
#	NiceName - convert a '\ ' pathname into an string with plain spaces
#		   Convert nasty characters into %nn to get past the shell
#		   Characters to convert are:
#		   	Percent(%)		- Its our escape lead-in
#						  Not sure make handles such file
#		   	singlequote(')		- It quotes our arguments
#		   	Ampersand(&)		- If found make uses shell
#
NiceName = $(subst \$(space), ,$(subst ',%27,$(subst &,%26,$(subst %,%25,$1))))

define CopyFile
	$(AA_PRE)JatsFileUtil 'c0' '$1' '$(call NiceName,$2)' '$(call NiceName,$3)' '$4'
endef

define UncopyFile
	$(AA_PRE)JatsFileUtil 'd0' '$1' '$(call NiceName,$2)'
endef

##
