#---------------------------------------------------------------------
# Makefile for Apache Cloudberry
#---------------------------------------------------------------------

# keep the default target above any targets from included makefiles
all : devel

#---------------------------------------------------------------------
# Declare the targets
#---------------------------------------------------------------------

# Targets for building GPDB with various options
# (development builds)
.PHONY : devel

# (release builds)
.PHONY : dist dist_prof

# Targets recognized by the PostgreSQL makefile (GNUmakefile)
.PHONY : all install installdirs uninstall distprep
.PHONY : clean distclean maintainer-clean

# The pvk target builds a subset of the GPDB package that includes just the
# tools used to verify the configuration of a set of GPDB servers.
.PHONY : pvk

# Targets for running autoconf and autoheader
.PHONY : autoconf

# Internal functions which are invoked by other rules within this makefile
.PHONY : mgmtcopy copylibs
.PHONY : RECONFIG HOMEDEP GPROOTDEP GPROOTDEP GPROOTFAIL
.PHONY : clients

#---------------------------------------------------------------------
# Make 3rd-party libraries and tools
#---------------------------------------------------------------------

# tell included makefiles where to find build-top-level resources
BLD_TOP:=$(shell sh -c pwd)
include Makefile.global
include Makefile.thirdparty

ifneq "$(BLD_BITS)" ""
export NO_M64=1
endif

#$(warning )
#$(warning PATH=$(PATH))
#$(warning LD_LIBRARY_PATH=$(LD_LIBRARY_PATH))
#$(warning )
#$(warning BLD_ARCH=$(BLD_ARCH))
#$(warning BLD_BITS=$(BLD_BITS))
#$(warning BLD_CFLAGS=$(BLD_CFLAGS))
#$(warning )

BLD_CXX_LIB_DIR=$($(BLD_ARCH)_CXX_LIB_DIR)

#---------------------------------------------------------------------
# Directory paths and file names
#---------------------------------------------------------------------

GPPGDIR=$(shell cd .. && pwd)
GPMGMT=../gpMgmt
BUILDDIR=$(GPPGDIR)

RELEASE=devel
ifeq "$(BUILD_TYPE)" "release"
GPDIR=cloudberry-db
CLIENTSDIR=cloudberry-clients
else
GPDIR=cloudberry-db-devel
CLIENTSDIR=cloudberry-clients-devel
endif

DISTPATH=$(GPROOT)/$(GPDIR)
CLIENTSDISTPATH=$(GPROOT)/$(CLIENTSDIR)

ifeq "$(DEVPATH)" ""
BLD_HOME=$(HOME)
DEVPATH=$(BLD_HOME)/$(GPDIR)
CLIENTSDEVPATH=$(BLD_HOME)/$(CLIENTSDIR)
else
# DEVPATH has been passed in as by the build scripts
GPROOT_DEV=$(dir $(DEVPATH))
CLIENTSDEVPATH=$(GPROOT_DEV)$(CLIENTSDIR)
endif

# files generated by 'configure'
ISCONFIG=$(GPPGDIR)/GNUmakefile


##
## On these platforms, we do the full build including the server. On other
## platforms, we do a client-only build.
##
SERVER_PLATFORMS=rhel7_x86_64 rhel6_x86_64 rhel8_x86_64 linux_x86_64

#---------------------------------------------------------------------
# Compiler options
#---------------------------------------------------------------------

OPTFLAGS="$(strip $(BLD_CFLAGS) -O3 -Wno-nonnull -fargument-noalias-global -fno-omit-frame-pointer -g)"
PROFFLAGS="$(strip $(BLD_CFLAGS) -O3 -Wno-nonnull -fargument-noalias-global -fno-omit-frame-pointer -g)"

ifeq (on, ${GPDBGOPT})
CFLAGS_OPT=-O1 -fno-omit-frame-pointer
else
CFLAGS_OPT=-O0
endif

ifeq (on, ${GPDBGCOV})
DEBUG_CFLAGS_OPT=
else
DEBUG_CFLAGS_OPT=-g3
endif

DEBUGFLAGS="$(strip $(BLD_CFLAGS) $(CFLAGS_OPT) $(DEBUG_CFLAGS_OPT))"
#---------------------------------------------------------------------
# configure
#---------------------------------------------------------------------

DEFPORT=5432

ORCA_CONFIG=--enable-orca

rhel6_x86_64_CONFIGFLAGS=--with-quicklz --with-lz4 --with-gssapi --enable-mapreduce --enable-orafce --enable-ic-proxy ${ORCA_CONFIG} --enable-gpcloud --with-libxml --with-ssl=openssl --with-pam --with-ldap --with-pythonsrc-ext
rhel7_x86_64_CONFIGFLAGS=--with-quicklz --with-lz4 --with-gssapi --enable-mapreduce --enable-orafce --enable-ic-proxy ${ORCA_CONFIG} --enable-gpcloud --with-libxml --with-ssl=openssl --with-pam --with-ldap --with-pythonsrc-ext
rhel8_x86_64_CONFIGFLAGS=--with-quicklz --with-lz4 --with-gssapi --enable-mapreduce --enable-orafce --enable-ic-proxy ${ORCA_CONFIG} --enable-gpcloud --with-libxml --with-ssl=openssl --with-pam --with-ldap --with-pythonsrc-ext
ubuntu18.04_x86_64_CONFIGFLAGS=--with-quicklz --with-lz4 --with-gssapi --enable-mapreduce --enable-orafce --enable-ic-proxy ${ORCA_CONFIG} --enable-gpcloud --with-libxml --with-ssl=openssl --with-pam --with-ldap --with-pythonsrc-ext
sles12_x86_64_CONFIGFLAGS=--with-quicklz --with-lz4 --with-gssapi --enable-mapreduce --enable-orafce --enable-ic-proxy ${ORCA_CONFIG} --enable-gpcloud --with-libxml --with-ssl=openssl --with-pam --with-ldap --with-pythonsrc-ext
rhel7_aarch64_CONFIGFLAGS=--with-quicklz --with-lz4 --with-gssapi --enable-mapreduce --enable-orafce --enable-ic-proxy ${ORCA_CONFIG} --enable-gpcloud --with-libxml --with-ssl=openssl --with-pam --with-ldap --with-pythonsrc-ext
common_CONFIGFLAGS=--with-quicklz --with-lz4 --with-gssapi --enable-mapreduce --enable-orafce --enable-ic-proxy ${ORCA_CONFIG} --enable-gpcloud --with-libxml --with-ssl=openssl --with-pam --with-ldap --with-pythonsrc-ext
#BLD_CONFIGFLAGS=$($(BLD_ARCH)_CONFIGFLAGS)
BLD_CONFIGFLAGS=$(common_CONFIGFLAGS)

CONFIGFLAGS=$(strip $(BLD_CONFIGFLAGS) --with-pgport=$(DEFPORT) $(BLD_DEPLOYMENT_SETTING))

ifdef CONFIGURE_FLAGS
CONFIGFLAGS+= $(CONFIGURE_FLAGS)
endif


ifneq (false, ${PG_LANG})
CONFIGFLAGS+= --with-perl --with-python PYTHON=python3
ifdef TCL_CFG
CONFIGFLAGS+= --with-tcl-config=${TCL_CFG}
endif
endif

# Configure in thirdparty libraries from the ext/ directory
CONFIG_INCLUDES=$(BLD_THIRDPARTY_INCLUDE_DIR) $(BLD_THIRDPARTY_INCLUDE_DIR)/libxml2
CONFIG_LIBRARIES=$(strip $(BLD_THIRDPARTY_LIB_DIR) $(BLD_CXX_LIB_DIR) $(BLD_PTHREADS_LIB_DIR))
CONFIGFLAGS+= --with-includes="$(CONFIG_INCLUDES)" --with-libraries="$(CONFIG_LIBRARIES)"


CONFIGFLAGS+= --disable-rpath
CONFIGFLAGS+= LDFLAGS_EX='-Wl,--enable-new-dtags -Wl,-rpath,\$$$$ORIGIN/../lib' LDFLAGS_SL='-Wl,--enable-new-dtags -Wl,-rpath,\$$$$ORIGIN:\$$$$ORIGIN/..'

RECONFIG :
	rm -f Debug/GNUmakefile
	rm -f Release/GNUmakefile
	rm -f $(GPPGDIR)/GNUmakefile

$(GPPGDIR)/GNUmakefile : $(GPPGDIR)/configure  env.sh
	rm -rf $(INSTLOC)
	mkdir -p $(GPPGDIR)
	echo "Running ./configure with CONFIGFLAGS=$(CONFIGFLAGS)\n"
	cd $(GPPGDIR) && \
                     CFLAGS=$(INSTCFLAGS) 					  \
                     ./configure $(CONFIGFLAGS)               \
                         --prefix=$(INSTLOC)                  \
                         --mandir=$(INSTLOC)/man

Debug/GNUmakefile : $(GPPGDIR)/configure  env.sh
	rm -rf $(INSTLOC)
	mkdir -p Debug
	echo "Running ./configure with CONFIGFLAGS=$(CONFIGFLAGS)\n"
	cd Debug && \
                CFLAGS=$(INSTCFLAGS) 						 \
                ./configure $(CONFIGFLAGS)                   \
                    --prefix=$(INSTLOC)                      \
                    --mandir=$(INSTLOC)/man

Release/GNUmakefile : $(GPPGDIR)/configure  env.sh
	rm -rf $(INSTLOC)
	mkdir -p Release
	echo "Running ./configure with CONFIGFLAGS=$(CONFIGFLAGS)\n"
	cd Release && \
                  CFLAGS=$(INSTCFLAGS) 					    \
                  ./configure $(CONFIGFLAGS)                \
                      --prefix=$(INSTLOC)                   \
                      --mandir=$(INSTLOC)/man

#---------------------------------------------------------------------
# autoconf
#---------------------------------------------------------------------
# Whenever there is a change to the configure.in file, autoconf and
# autoheader should be run to regenerate the files configure and
# src/include/pg_config.h.in.  Note that autoconf and autoheader
# don't update the timestamps of their output files unless there is an
# actual change in the contents; thus 'make' rules don't work well for
# rebuilding those files automatically.  Developers should rebuild them
# manually when needed, which can be done by invoking 'make autoconf'.

autoconf : RECONFIG
	cd $(GPPGDIR) && autoconf && autoheader

#---------------------------------------------------------------------
# standardized build rules
#---------------------------------------------------------------------

BLD_LD_LIBRARY_PATH:=$($(BLD_ARCH)_LD_LIBRARY_PATH):$($(BLD_WHERE_THE_LIBRARY_THINGS_ARE))
export $(BLD_WHERE_THE_LIBRARY_THINGS_ARE)=$(BLD_LD_LIBRARY_PATH)

perl_archlibexp:=$(shell perl -MConfig -e 'print $$Config{archlibexp}')

# set default build steps
define BUILD_STEPS
	@$(MAKE) -C $(BUILDDIR)/src/backend generated-headers
	cd $(BUILDDIR) && PYGRESQL_LDFLAGS=' -Wl,-rpath,\$$$$ORIGIN/.. -Wl,--enable-new-dtags ' $(MAKE) $(PARALLEL_MAKE_OPTS) install
	cd $(BUILDDIR)/src/pl/plpython && $(MAKE) clean && $(MAKE) $(PARALLEL_MAKE_OPTS) install && cd $(BUILDDIR)
	cd $(BUILDDIR)/src/pl/plperl && $(MAKE) clean && echo "LDFLAGS += -Wl,-rpath,$(perl_archlibexp)/CORE -Wl,--enable-new-dtags" >> GNUmakefile && echo "LDFLAGS_SL += -Wl,-rpath,$(perl_archlibexp)/CORE -Wl,--enable-new-dtags" >> GNUmakefile && $(MAKE) $(PARALLEL_MAKE_OPTS) install && cd $(BUILDDIR)
	#@$(MAKE) greenplum_path INSTLOC=$(INSTLOC)
	#@$(MAKE) mgmtcopy INSTLOC=$(INSTLOC)
	@$(MAKE) copylibs INSTLOC=$(INSTLOC)
	@$(MAKE) clients INSTLOC=$(INSTLOC) CLIENTSINSTLOC=$(CLIENTSINSTLOC)
	@$(MAKE) set_scripts_version INSTLOC=$(CLIENTSINSTLOC)
	@$(MAKE) copylicense INSTLOC=$(INSTLOC) \
                         CLIENTSINSTLOC=$(CLIENTSINSTLOC)
endef

ifeq "$(BLD_GPDB_BUILDSET)" "partial"
define BUILD_STEPS
	rm -rf $(INSTLOC)
	cd $(BUILDDIR)/gpMgmt/ && $(MAKE) generate_greenplum_path_file
	cd $(BUILDDIR)/src/backend/ && $(MAKE) ../../src/include/parser/gram.h
	cd $(BUILDDIR)/src/backend/ && $(MAKE) ../../src/include/utils/errcodes.h
	cd $(BUILDDIR)/src/backend/ && $(MAKE) ../../src/include/utils/fmgroids.h
	cd $(BUILDDIR)/src/port/ && $(MAKE) install
	cd $(BUILDDIR)/src/bin/scripts/ && $(MAKE) install
	cd $(BUILDDIR)/src/bin/psql/ && $(MAKE) install
	cd $(BUILDDIR)/src/bin/pg_config/ && $(MAKE) install
	cd $(BUILDDIR)/src/bin/pg_dump/ && $(MAKE) pg_dump
	cp -p $(BUILDDIR)/src/bin/pg_dump/pg_dump $(INSTLOC)/bin/
	cd $(BUILDDIR)/src/bin/pg_dump/ && $(MAKE) pg_dumpall
	cp -p $(BUILDDIR)/src/bin/pg_dump/pg_dumpall $(INSTLOC)/bin/
	cd $(BUILDDIR)/src/bin/pg_dump/ && $(MAKE) pg_restore
	cp -p $(BUILDDIR)/src/bin/pg_dump/pg_restore $(INSTLOC)/bin/
	cd $(BUILDDIR)/src/interfaces/ecpg/ && $(MAKE) install
	cd $(BUILDDIR)/src/include/ && $(MAKE) install
	cd $(BUILDDIR)/src/interfaces/libpq/ && $(MAKE) install
	cd $(BUILDDIR)/src/bin/gpfdist && $(MAKE) install
	cp -p $(GPMGMT)/bin/gpload $(INSTLOC)/bin/gpload
	cp -p $(GPMGMT)/bin/gpload.py $(INSTLOC)/bin/gpload.py
	$(MAKE) copylibs INSTLOC=$(INSTLOC)
	cd $(GPMGMT)/bin && $(MAKE) pygresql INSTLOC=$(INSTLOC)
	$(MAKE) clients INSTLOC=$(INSTLOC) CLIENTSINSTLOC=$(CLIENTSINSTLOC)
endef
endif

#---------------------------------------------------------------------
# environment for later builds inside the source tree
#
# run this to create an environment-loading script, source it,
# and be able to build inside the source tree with the same
# results as building from here at the top
#---------------------------------------------------------------------

env.sh:
	echo "export BLD_ARCH=$(BLD_ARCH)" > env.sh
	echo "export NO_M64=$(NO_M64)" >> env.sh
	echo "export PATH=$(PATH)" >> env.sh
	echo "export $(BLD_WHERE_THE_LIBRARY_THINGS_ARE)=$($(BLD_WHERE_THE_LIBRARY_THINGS_ARE))" >> env.sh
ifneq "$(PERL_DIR)" ""
	echo "export PERL5LIB=$(PERL_DIR)/lib/site_perl/5.12.4/x86_64-linux:$(PERL_DIR)/lib/site_perl/5.12.4:$(PERL_DIR)/lib/5.12.4/x86_64-linux:$(PERL_DIR)/lib/5.12.4" >> env.sh
endif

#---------------------------------------------------------------------
# Development builds
#---------------------------------------------------------------------

devel : INSTCFLAGS=$(DEBUGFLAGS)
devel : INSTLOC=$(DEVPATH)
devel : CLIENTSINSTLOC=$(CLIENTSDEVPATH)
devel : CONFIGFLAGS+= --enable-cassert --enable-debug --enable-depend
ifdef ENABLE_VPATH_BUILD
devel : BUILDDIR=Debug
devel : ISCONFIG=$(BUILDDIR)/GNUmakefile
devel : HOMEDEP Debug/GNUmakefile
else
devel : HOMEDEP $(ISCONFIG)
endif
	$(BUILD_STEPS)

#---------------------------------------------------------------------
# Release builds
#---------------------------------------------------------------------

dist : INSTCFLAGS=$(OPTFLAGS)
dist : INSTLOC=$(DISTPATH)
dist : CLIENTSINSTLOC=$(CLIENTSDISTPATH)
ifdef ENABLE_VPATH_BUILD
dist : BUILDDIR=Release
dist : ISCONFIG=$(BUILDDIR)/GNUmakefile
dist : GPROOTDEP RECONFIG Release/GNUmakefile
else
dist : GPROOTDEP RECONFIG $(ISCONFIG)
endif
	$(BUILD_STEPS)

dist_prof : INSTCFLAGS=$(PROFFLAGS)
dist_prof : INSTLOC=$(DISTPATH)
dist_prof : CLIENTSINSTLOC=$(CLIENTSDISTPATH)
ifdef ENABLE_VPATH_BUILD
dist_prof : BUILDDIR=Release
dist_prof : ISCONFIG=$(BUILDDIR)/GNUmakefile
dist_prof : GPROOTDEP RECONFIG $(ISCONFIG)
else
dist_prof : ISCONFIG=../GNUmakefile
dist_prof : GPROOTDEP RECONFIG $(ISCONFIG)
endif
	$(BUILD_STEPS)

dist_faultinj : CLIENTSINSTLOC=$(CLIENTSDISTPATH)
ifdef ENABLE_VPATH_BUILD
dist_faultinj : BUILDDIR=Release
dist_faultinj : ISCONFIG=$(BUILDDIR)/GNUmakefile
dist_faultinj : GPROOTDEP RECONFIG Release/GNUmakefile
else
dist_faultinj : ISCONFIG=../GNUmakefile
dist_faultinj : GPROOTDEP RECONFIG $(ISCONFIG)
endif
	$(BUILD_STEPS)

#---------------------------------------------------------------------
# clientTools
#---------------------------------------------------------------------
#
# The clients target builds GPDB [f]or a subset of files needed for client
# platforms.  It also builds clientTools-specific items like the ODBC drivers.
#

ifneq "$(GPROOT)" ""
CLIENTS_HOME_DIR=$(GPROOT)
else
CLIENTS_HOME_DIR=$(BLD_HOME)
endif
CLIENTSINSTLOC=$(CLIENTS_HOME_DIR)/cloudberry-clients
CLIENTSINSTLOC_BIN=$(CLIENTSINSTLOC)/bin
CLIENTSINSTLOC_BINEXT=$(CLIENTSINSTLOC)/bin/ext/
CLIENTSINSTLOC_EXT=$(CLIENTSINSTLOC)/ext
CLIENTSINSTLOC_LIB=$(CLIENTSINSTLOC)/lib
CLIENTSINSTLOC_LIB_PWARE=$(CLIENTSINSTLOC)/lib/pware
CLIENTSINSTLOC_DOCS=$(CLIENTSINSTLOC)/docs
CLIENTSINSTLOC_OPENSOURCE=$(CLIENTSINSTLOC)/thirdparty

DOC_TYPE=Unix
SCRIPT=.sh

define tmpCLIENTS_FILESET_BIN
	gpfdist
	gpload
	gpload.py
	pg_dump
	pg_dumpall
	psql
	createdb
	dropdb
	createuser
	dropuser
endef
CLIENTS_FILESET_BIN = $(strip $(tmpCLIENTS_FILESET_BIN))

# pg.py, pgdb.py, _pg.so are from pygresql which does not install into a single module
define tmpCLIENTS_FILESET_BINEXT
	pg.py
	pgdb.py
	_pg*.so
	yaml
endef
CLIENTS_FILESET_BINEXT = $(strip $(tmpCLIENTS_FILESET_BINEXT))

BLD_PYTHON_FILESET=.

BLD_OS:=$(shell uname -s)
Darwin_CLIENTS_LIBS=libcrypto.*.dylib libssl.*.dylib libpq.*.dylib* libkrb5.*.dylib libcom_err.*.dylib libldap_r-*.dylib libk5crypto.*.dylib libkrb5support.*.dylib liblber-*.dylib libyaml*.dylib
Linux_CLIENTS_LIBS=libpq.so*
define tmpCLIENTS_FILESET_LIB
	$($(BLD_OS)_CLIENTS_LIBS)
endef
CLIENTS_FILESET_LIB = $(strip $(tmpCLIENTS_FILESET_LIB))

ifeq "$(MPP_ARCH)" ""
rhel6_x86_64_MPP_ARCH=RHEL6-x86_64
rhel7_x86_64_MPP_ARCH=RHEL7-x86_64
rhel8_x86_64_MPP_ARCH=RHEL8-x86_64
ifneq "$($(BLD_ARCH)_MPP_ARCH)" ""
export MPP_ARCH=$($(BLD_ARCH)_MPP_ARCH)
else
export MPP_ARCH=$(BLD_ARCH)
endif
endif

ifeq "$(INSTLOC)" ""
INSTLOC=$(GPDIR)
endif

VERSION:=$(shell [ -f ../VERSION ] && perl -pe 's, ,-,g' ../VERSION)
VERSION_SHORT:=$(shell [ -f ../VERSION ] && perl -pe 's, .*,,g' ../VERSION)

clients :
	if [ ! -z "$(CLIENTSINSTLOC)" ] && [ "$(CLIENTSINSTLOC)" != "/" ]; then rm -rf $(CLIENTSINSTLOC); fi
ifeq "$(findstring clients,$(BLD_TARGETS))" ""
	# ---- build and packaging of the clients package is currently disabled;
	# ---- set BLD_TARGETS="clients" to enable them.
else
	# ---- copy GPDB fileset ----
	mkdir -p $(CLIENTSINSTLOC_BIN)
	(cd $(INSTLOC)/bin/ && $(TAR) cf - $(CLIENTS_FILESET_BIN)) | (cd $(CLIENTSINSTLOC_BIN)/ && $(TAR) xpf -)$(check_pipe_for_errors)
	mkdir -p $(CLIENTSINSTLOC_BINEXT)
	(cd $(GPMGMT)/bin/ext/ && $(TAR) cf - $(CLIENTS_FILESET_BINEXT)) | (cd $(CLIENTSINSTLOC_BINEXT)/ && $(TAR) xpf -)$(check_pipe_for_errors)
ifneq "$(PYTHONHOME)" ""
	mkdir -p $(CLIENTSINSTLOC_EXT)/python
	(cd $(PYTHONHOME) && $(TAR) cf - $(BLD_PYTHON_FILESET)) | (cd $(CLIENTSINSTLOC_EXT)/python/ && $(TAR) xpf -)$(check_pipe_for_errors)
endif
	mkdir -p $(CLIENTSINSTLOC_LIB)
 ifneq "$(CLIENTS_FILESET_LIB)" ""
	 (cd $(INSTLOC)/lib/ && $(TAR) cf - $(CLIENTS_FILESET_LIB)) | (cd $(CLIENTSINSTLOC_LIB)/ && $(TAR) xpf -)$(check_pipe_for_errors)
 endif
	# ---- copy scripts fileset ----
	mkdir -p $(CLIENTSINSTLOC)
	cp -f client/scripts/greenplum_$@_path$(SCRIPT) $(CLIENTSINSTLOC)/

	# ---- updating the version in required packages ----
	@$(MAKE) set_scripts_version INSTLOC=$(CLIENTSINSTLOC)
endif

#---------------------------------------------------------------------
# Platform Verification Kit (PVK)
#   formerly checkTools
#
# The pvk target builds a subset of the GPDB package that includes
# just the tools used to verify the configuration of a set of GPDB servers.
#---------------------------------------------------------------------

PVK_SCRIPTS = \
	bin/gpscp			\
	bin/gpssh			\
	bin/gpcheckperf			\
	$(NULL)

PVK_FILESET = \
	$(CHECKTOOLS_SCRIPTS)	\
	bin/lib/gplib.py	\
	bin/lib/pxssh.py	\
	bin/lib/pexpect.py	\
	bin/lib/multidd		\
	bin/lib/stream		\
	$(NULL)

pvk : INSTCFLAGS=$(OPTFLAGS)
pvk : INSTLOC=$(DISTPATH)
pvk : version GPROOTDEP
	cd $(GPMGMT)/bin && $(MAKE) stream
	mkdir -p $(INSTLOC)/bin/lib
	@for file in $(PVK_FILESET); do \
	    echo "cp -p $(GPMGMT)/$${file} $(INSTLOC)/$${file}"; \
	    cp -p $(GPMGMT)/$${file} $(INSTLOC)/$${file}; \
	done
	perl putversion $(foreach script,$(PVK_SCRIPTS),$(INSTLOC)/$(script))

#---------------------------------------------------------------------
# Set Version
#---------------------------------------------------------------------

SET_VERSION_SCRIPTS = \
	bin/gpactivatestandby \
	bin/gpaddmirrors \
	bin/gpcheckperf \
	bin/gpdeletesystem \
	bin/gpexpand \
	bin/gpfilespace \
	bin/gpinitstandby \
	bin/gpinitsystem \
	bin/gpload.py \
	bin/gplogfilter \
	bin/gpmovemirrors \
	bin/gprecoverseg \
	bin/gpreload \
	bin/gpscp \
	bin/gpssh \
	bin/gpssh-exkeys \
	bin/gpstart \
	bin/gpstate \
	bin/gpstop \
	bin/lib/gpcheckcat \
	sbin/gpconfig_helper.py \
	sbin/gpcleansegmentdir.py \
	sbin/gpgetstatususingtransition.py \
	sbin/gpsegstart.py \
	sbin/gpsegstop.py \
	sbin/gpupgrademirror.py \
	lib/python/gppylib/programs/clsAddMirrors.py \
	lib/python/gppylib/programs/clsHostCacheLookup.py \
	lib/python/gppylib/programs/clsRecoverSegment.py \
	lib/python/gppylib/programs/clsSystemState.py \
	lib/python/gppylib/programs/gppkg.py \
	lib/python/gppylib/programs/kill.py \
	lib/python/gppylib/mainUtils.py \
	$(NULL)

set_scripts_version :
	@for file in $(SET_VERSION_SCRIPTS); do \
	    if [ -f $(INSTLOC)/$${file} ]; then \
	        perl $(BLD_TOP)/../putversion $(INSTLOC)/$${file} ; \
	    fi ; \
	done

#---------------------------------------------------------------------
# Internal functions
#---------------------------------------------------------------------

mgmtcopy :
	#Copy the management utilities
	mkdir -p $(INSTLOC)/bin
	mkdir -p $(INSTLOC)/lib
	mkdir -p $(INSTLOC)/lib/python
	mkdir -p $(INSTLOC)/sbin
	#Setup /lib/python contents
	cp -rp $(GPMGMT)/bin/gppylib $(INSTLOC)/lib/python
	cp -rp $(GPMGMT)/bin/ext/* $(INSTLOC)/lib/python
	cp -rp $(GPMGMT)/bin $(INSTLOC)
	cp -rp $(GPMGMT)/sbin/* $(INSTLOC)/sbin/.
	if [ ! -d ${INSTLOC}/docs ] ; then mkdir ${INSTLOC}/docs ; fi
	if [ -d $(GPMGMT)/doc ]; then cp -rp $(GPMGMT)/doc $(INSTLOC)/docs/cli_help; fi
	if [ -d $(GPMGMT)/demo/gpfdist_transform ]; then \
	  mkdir -p $(INSTLOC)/demo; \
	  $(TAR) -C $(GPMGMT)/demo -czf $(INSTLOC)/demo/gpfdist_transform.tar.gz gpfdist_transform; \
	fi
	$(MAKE) set_scripts_version INSTLOC=$(INSTLOC)
	# Remove unwanted files.
	rm -rf $(INSTLOC)/bin/ext
	rm -rf $(INSTLOC)/bin/pythonSrc
	rm -rf $(INSTLOC)/bin/Makefile
	rm -rf $(INSTLOC)/bin/src
	rm -rf $(INSTLOC)/bin/gppylib
	find $(INSTLOC)/lib/python/gppylib -name test -type d | xargs rm -rf

libcopy=(cd $(1) && $(TAR) cf - $(2)) | (cd $(3) && $(TAR) xvf -)$(check_pipe_for_errors)

copylibs : thirdparty-dist
	mkdir -p $(INSTLOC)/etc
	mkdir -p $(INSTLOC)/include

copylicense:
	for proddir in $(INSTLOC) $(CLIENTSINSTLOC); do \
	   if [ -d $${proddir} ]; then \
	      echo cp -v ${BLD_TOP}/LICENSE $${proddir}; \
	      echo cp -v ${BLD_TOP}/NOTICE  $${proddir}; \
	   fi; \
	done

HOMEDEP :
	@ #If the HOME variable isn't set, then execute the usage() path
	@if [ x"$(HOME)" = x ]; then $(MAKE) HOMEFAIL; fi

GPROOTDEP :
	@ #If the GPROOT variable isn't set, then execute the usage() path
	@if [ x"$(GPROOT)" = x ]; then $(MAKE) GPROOTFAIL; fi

GPROOTFAIL :
	@echo
	@echo "	The GPROOT path variable is not set."
	@echo
	@echo "	You must decide where you will install to, and set GPROOT to that location, prior to"
	@echo "	installing the GP module with this Makefile."
	@echo
	@exit 1

HOMEFAIL :
	@echo
	@echo "	The HOME environment variable is not set. Please set it to continue."
	@echo
	@exit 1

#---------------------------------------------------------------------
# Cleanup
#---------------------------------------------------------------------

clean :
#	@if [ ! -f $(ISCONFIG) ]; then echo "GPDB root not configured, no need to clean it"; fi
	@if [ -d Debug -a -f Debug/GNUmakefile ]; then cd Debug; $(MAKE) clean; fi
	@if [ -d Release -a -f Release/GNUmakefile ]; then cd Release; $(MAKE) clean; fi
	@if [ -d $(GPPGDIR) -a -f $(GPPGDIR)/GNUmakefile ]; then cd $(GPPGDIR); $(MAKE) clean; fi
	@if [ -d $(GPMGMT)/bin ]; then cd $(GPMGMT)/bin; $(MAKE) clean; fi

distclean :
#	@if [ ! -f $(ISCONFIG) ]; then echo "GPDB root not configured, no need to clean it"; fi
	@if [ -d Debug -a -f Debug/GNUmakefile ]; then cd Debug; $(MAKE) distclean; fi
	@if [ -d Release -a -f Release/GNUmakefile ]; then cd Release; $(MAKE) distclean; fi
	@if [ -d $(GPPGDIR) -a -f $(GPPGDIR)/GNUmakefile ]; then cd $(GPPGDIR); $(MAKE) distclean; fi
	@if [ -d $(GPMGMT)/bin ]; then cd $(GPMGMT)/bin; $(MAKE) distclean; fi
	@rm -f VERSION
	@rm -f env.sh

distprep : $(ISCONFIG)
	@cd $(BUILDDIR) && $(MAKE) distprep

