#==============================================================================
# Copyright and license info is available in the LICENSE file included with
# the Server Deployment Package (SDP), and also available online:
# https://swarm.workshop.perforce.com/projects/perforce-software-sdp/view/main/LICENSE
#------------------------------------------------------------------------------
# shellcheck disable=SC2148

# This defines the version of the format of this file, used when the SDP is
# upgraded.

# shellcheck disable=SC2034
# SDP_P4_VARS_FORMAT defines the version of the format of this file,
# used when the SDP is upgraded. DO NOT CHANGE SDP_P4_VARS_FORMAT.
SDP_P4_VARS_FORMAT=1.7

# This p4_vars file sets the standard SDP bash shell environment.  It contains
# global and default settings for all instances. Source this file with a
# parameter, the SDP instance name. For example, to set the shell environment
# for an instance named acme, do:
#
# $ source /p4/common/bin/p4_vars acme
#
# Note that a '.' (dot) command can be used in place of the world 'source'.
#
# Sourcing this p4_vars file causes the instance-specific settings file to
# be sourced if it exists.  Instance-specific settings files are in the
# /p4/common/config directory.  For the acme instance, the instance-specific
# settings file would be p4_acme.vars. 
#
# If the instance parameter is omitted, an instance name of '1' (the digit one)
# is assumed.

# This file should not be modified. If site-specific overrides or additional
# settings are desired, please add this syntax (like this file):
# /p4/common/config/site_global_vars
#
# If that file exists, it will be sourced in by this file after the settings
# in this file are read in, and before any instance-specific overrides are
# set.

# Save SHELLOPTS
_shellopts=$SHELLOPTS

# Trap unbound (undefined) variables, treating them as an error.
set -u

export OSUSER=REPL_OSUSER
export SDP_VERSION="REPL_SDPVERSION"

export AWK=awk
export CUT=cut
export DF="df -h"
export GREP=grep
export ID=id
export SDPMAIL=mail
export PS=ps

OS=$(uname)
if [ "${OS}" = "SunOS" ] ; then
   export AWK=/usr/xpg4/bin/awk
   export ID=/usr/xpg4/bin/id
   export SDPMAIL=mailx
elif [ "${OS}" = "AIX" ] ; then
   export DF="df -m"
fi

# Which perforce instance are we configuring? Defaults to 1.
export SDP_INSTANCE=${1:-1}

# The SDP_ROOT has a fixed value of /p4 for production environments. The
# variable SDP_ROOT should not be defined outside of this file.
export SDP_ROOT=${SDP_ROOT:-/p4}

# The P4INSTANCE variable is deprecated, and is maintained here for backward
# compatibility.  The SDP_INSTANCE variable should be referenced instead, as
# P4INSTANCE may go away in a future release.
export P4INSTANCE=$SDP_INSTANCE
export P4SERVER="p4_${SDP_INSTANCE}"
export P4SSLDIR="${SDP_ROOT}/ssl"
export P4HOME="${SDP_ROOT}/${SDP_INSTANCE}"
export TMP=${P4HOME}/tmp
export P4TMP=${P4HOME}/tmp

# Explicitly unset environment variables that affect p4d server if they
# are defined at startup, excluding those that are always defined in this
# file, like P4ROOT, P4PORT, P4LOG, and P4JOURNAL.  This prevents cross-
# instance shell environment contamination if p4_vars is sourced multiple
# times for different instances.
unset P4AUTH P4CHANGE P4DEBUG P4AUDIT P4TARGET

export P4ROOT=${P4HOME}/root
export P4DTG_ROOT=/p4/common/dtg
export P4JOURNAL=${P4HOME}/logs/journal
export P4CONFIG=${P4HOME}/.p4config
export P4TICKETS=${P4HOME}/.p4tickets
export P4TRUST=${P4HOME}/.p4trust
export P4ENVIRO=/dev/null/.p4enviro
export DEPOTS=${P4HOME}/depots
export CHECKPOINTS=${P4HOME}/checkpoints
export LOGS=${P4HOME}/logs
export P4LOG=${LOGS}/log
export P4LOGS=$P4HOME/logs
export P4COMMON=${SDP_ROOT}/common
export P4CBIN=$P4COMMON/bin
export P4CSITE=${P4COMMON}/site
export P4CSBIN=${P4CSITE}/bin
export P4CCFG=${P4COMMON}/config
export P4CLIB=${P4COMMON}/lib
export PERLHOME=${P4COMMON}/perl
export PERL5LIB=$PERLHOME/lib:$P4CLIB:${PERL5LIB:-}
export PYHOME=${P4COMMON}/python
export RBHOME=${P4COMMON}/ruby
export HMS_HOME=${P4CSITE}/hms
export PATH=$P4HOME/bin:$P4CBIN:$PYHOME/bin:$PERLHOME/site/bin:$PERLHOME/bin:$RBHOME/bin:$P4CSBIN:$PATH:.
export MANPATH=$PERLHOME/site/man:$PERLHOME/man:${MANPATH:-}
export SERVERID=

if [[ -r "${P4ROOT}/server.id" ]]; then
   SERVERID=$(head -1 "${P4ROOT}/server.id")
else
   # If a real ServerID is not set via a server.id file, use the short
   # hostname as a default.  This is not appropriate for a real p4d,
   # but is fine for SDP-managed machines that use just a proxy or broker.
   # In those cases, SERVERID is used to define the service user, prefixed
   # with 'svc_'. This works well if reasonable host names are assigned.
   SERVERID=${HOSTNAME%%.*}
fi

# P4Proxy Settings
export P4PCACHE="$P4HOME/cache"

# Set KEEPCKPS to the number of checkpoint & journal files and their
# corresponding log files to keep.  Checkpoints and their log file are
# numbered with the same journal counter number.  Checkpoints/journals
# and their logs are removed when daily_checkpoint.sh, live_checkpoint.sh,
# or recreate_db_checkpoint.sh are run.
#  Set KEEPCKPS=0 to avoid automated cleanup of checkpoints and logs.
export KEEPCKPS=21

# KEEPJNLS allows you to keep a different number of journals than checkpoints
# in case you rotate the journal more frequently than you run checkpoints.
export KEEPJNLS=21

# Set KEEPLOGS to the number of server logs to keep, counting back from
# the most recent.  Server logs are numbered with their corresponding
# journal counter number, since server logs are rotated when checkpoint
# logs are rotated.  Keep in mind that if scripts are set to run
# more than once per day, this will not correspond to the number
# of days, just the number of iterations of script calls.
# KEEPLOGS affects all log files other than checkpoint logs, which are
# governed by KEEPCKPS.
# Set KEEPLOGS=0 to avoid automated cleanup of old server logs.
export KEEPLOGS=21

# Don't change this; change or comment the above as needed
export P4BIN=${P4HOME}/bin/p4_${SDP_INSTANCE}
export P4DBIN=${P4HOME}/bin/p4d_${SDP_INSTANCE}
export P4PBIN=${P4HOME}/bin/p4p_${SDP_INSTANCE}
export P4BROKERBIN=${P4HOME}/bin/p4broker_${SDP_INSTANCE}
export P4DTGBIN=${P4HOME}/bin/p4dtg_${SDP_INSTANCE}
export P4_VERSION P4D_VERSION P4BROKER_VERSION P4P_VERSION

export SDP_ADMIN_PASSWORD_FILE="$P4CCFG/.p4passwd.${P4SERVER}.admin"

# Define *_VERSION values.  These will look like "2014.1.899321"
# or "2015.1_BETA.903064".  For example, to specify behavior specific
# to 2014.1 or later P4D, test with [[ "$P4D_VERSION" > "2014.1" ]].
# That expression will be true 2014.1 and any subsequent release.  Since
# the values contain the patch number, all releases of "2014.1.*" will
# be greater than the string "2014.1", so "greater than or equal to" is
# implied.
export P4_VERSION P4D_VERSION P4P_VERSION P4BROKER_VERSION

[[ -x $P4BIN ]] && P4_VERSION=$($P4BIN -V|$GREP ^Rev.|$CUT -d '/' -f 3).$($P4BIN -V|$GREP ^Rev.|$CUT -d '/' -f 4|$CUT -d ' ' -f 1)
[[ -x $P4DBIN ]] && P4D_VERSION=$($P4DBIN -V 2>/dev/null|$GREP ^Rev.|$CUT -d '/' -f 3).$($P4DBIN -V 2>/dev/null|$GREP ^Rev.|$CUT -d '/' -f 4|$CUT -d ' ' -f 1)

# Set version values for optional executables.
[[ -x $P4PBIN ]] && P4P_VERSION=$($P4PBIN -V|$GREP ^Rev.|$CUT -d '/' -f 3).$($P4PBIN -V|$GREP ^Rev.|$CUT -d '/' -f 4|$CUT -d ' ' -f 1)
[[ -x $P4BROKERBIN ]] && P4BROKER_VERSION=$($P4BROKERBIN -V|$GREP ^Rev.|$CUT -d '/' -f 3).$($P4BROKERBIN -V|$GREP ^Rev.|$CUT -d '/' -f 4|$CUT -d ' ' -f 1)

# Number of seconds to delay service start for p4d/p4broker/p4p.  This helps
# prevent issues with a too-fast service restart when the port may still not
# be cleared, system resources may not be available during the boot process,
# or similar timing issues. Delaying on start is also a workaround for issues
# where the license file references a not-quite-static reserved IP address
# that may not be be accessible early in the machine reboot process; for
# this purpose a value as high as 20 may be needed.  This can be set
# to a value of 0 if no delay is desired.
export SDP_START_DELAY=2

# If a site global settings file exits, source it here.
if [[ -f "$P4CCFG/site_global_vars" ]]; then
   # shellcheck disable=SC1091
   source "$P4CCFG/site_global_vars"
fi

#------------------------------------------------------------------------------
# Custom Local Changes
#------------------------------------------------------------------------------
# Any local customizations that apply to all instances should appear in the
# file sourced below in the /p4/common/site structure, which contains site-
# specific that are not part of the SDP package.

if [[ -d "${P4CSITE}/config/p4_vars.local.d" ]]; then
   # shellcheck disable=SC1090
   for f in "${P4CSITE}"/config/p4_vars.local.d/*; do source "$f"; done
fi

if [[ -r "${P4CSITE}/config/p4_vars.local" ]]; then
   # shellcheck disable=SC1091
   source "${P4CSITE}/config/p4_vars.local"
fi

# Sets instance-specific vars.  Last so that the caller sees the error.
if [[ -f "${P4CCFG}/${P4SERVER}.vars" ]]; then
   # shellcheck disable=SC1090
   source "${P4CCFG}/${P4SERVER}.vars"
fi

# If 'nounset' was not in SHELLOPTS when we started (i.e. 'set -u' mode), call
# 'set +u'.  This is a workaround to a Linux bug:
# https://bugzilla.redhat.com/show_bug.cgi?id=1055784
# 
# The goal: We want it so p4_vars can be sourced safely in scripts and in user
# shell environments.  In scripts, we want 'set -u' to be set, as a bash
# programming best practice disallowing reference of undefined variables
# ("unbound variables" in bash parlance).  However, in some versions of
# Linux and more specifically bash, the tab command completion is broken
# if 'set -u' is set, such that when you hit the 'tab' key interactively,
# instead of getting completion, you get a hideous error.  This workaround
# gives you optimal behavior in scripts that have already called 'set -u'
# before sourcing the SDP environment file, as well as interactive shells
# (where the 'set +u' workaround applies).
if [[ "$_shellopts" != *"nounset"* ]]; then
    set +u
fi