#------------------------------------------------------------------------------
# Instance specific variables are set or calculated in this file. This file
# is not intended to be sourced directly. It is sourced when the main SDP
# environment file, /p4/common/bin/p4_vars, is sourced. This p4_N.vars file
# is sourced as in the following example, where N is the SDP instance name,
# with a value of 1 by default:
#
#   source /p4/common/bin/p4_vars N
#
# A line like the above can optionally be added in the ~/.bashrc file
# of the perforce user on the Linux system. This is recommended so that
# the SDP Environment is loaded properly during initial login.
#
#------------------------------------------------------------------------------
# When editing values in this file keep in mind the following:
#
# * Do not change values for which the comments say 'DO NOT CHANGE'.
# * Do not add code or logic of any kind.
# * Limit changes strictly to change the right-side of assignments (i.e. text
# to the right of the '=' sign).
# * Be careful not to introduce whitespace characters on either side of
# the '=' sign.
#
# If custom logic is needed, e.g. to add host-specific behaviors or configure
# multiple active brokers with different ports, add that custom logic in custom
# local files that appear in the /p4/common/site directory.  Files in the
# 'site' directory will not be not affected by SDP upgrades. Also, files in the
# 'site' directory are inherently custom and not officially supported by
# Perforce Technical Support.
#
# If needed, custom logic goes into these files:
# * /p4/common/site/config/p4_vars.local    - Global/Common
# * /p4/common/site/config/p4_N.vars.local  - Instance-specifc (N=instance).
#
# This file contains comments of the form: # shellcheck disable=SCNNNN.
# DO NOT CHANGE OR MOVE THESE COMMENT LINES.
#------------------------------------------------------------------------------
# Disable shellcheck requirement for shebang line, as this file is sourced
# in (from p4_vars) rather than executed as a script.
# shellcheck disable=SC2148
#------------------------------------------------------------------------------
# shellcheck disable=SC2034
# SDP_INSTANCE_VARS_FORMAT defines the version of the format of this file,
# used when the SDP is upgraded. DO NOT CHANGE SDP_INSTANCE_VARS_FORMAT.
SDP_INSTANCE_VARS_FORMAT=1.14

# This file is processed by mkdirs.sh to update REPL_* values as appropriate.
# It can subsequently be edited if required.

# Various scripts send email to $MAILTO.  This can contain comma-delimited list
# of email addresses (no spaces).  The addresses specified here are generally
# those of individual Perforce Helix administrators, and/or to an email
# distribution list of administrators.
export MAILTO=REPL_MAILTO

# Specify the email address from which emails are sent in $MAILFROM.
# This must be exactly one email address.  The value typically looks something
# like P4Admin@example.com.
# NOTE: Many modern email services, such as Office365 and Google Workspaces,
# may require that the domain name in the email address from which emails are
# sent be the correct domain for your company.  For example, if your company's
# domain for email is example.com, the email server may require that any emails
# sent from that domain also end in @example.com.
export MAILFROM=REPL_MAILFROM

# This is the P4USER that is the super user. This user must have a unlimited
# ticket valid on the master Perforce Helix server machine.
export P4USER=REPL_ADMINUSER

# Service user definition. DO NOT CHANGE P4SERVICEUSER.
export P4SERVICEUSER=svc_${SERVERID}

# P4MASTER_ID is server.id of Master for this instance
export P4MASTER_ID=REPL_MASTER_ID

# If this instance uses SSL, set SSL_PREFIX to ssl:, ssl4:, ssl6:,
# ssl46:, or ssl64:. See: https://portal.perforce.com/s/article/1180
# The trailing colon is required.
export SSL_PREFIX=REPL_SSLPREFIX

# Set the numeric portion of the P4PORT value used for p4d.
export P4PORTNUM=REPL_P4PORT

# The P4PORT value as defined here is only for use on the server machine. For
# safety and clarity, it intentionally excludes a hostname component, and
# looks like 'ssl:1999' or just '1999', but not something like
# 'ssl:some_host:1999'.  The intent is to ensure that the P4PORT value cannot
# accidentally target another machine due to DNS and/or /etc/host hacks.
# This P4PORT value is calculated. DO NOT CHANGE P4PORT.
export P4PORT=${SSL_PREFIX}${P4PORTNUM}

# Set the value for P4BROKERPORT to Unset if the broker is not
# used.
export P4BROKERPORTNUM=REPL_P4BROKERPORT

# This is the broker port. To test broker connections, you
# can use a command like: p4 -p $P4BROKERPORT info
# DO NOT CHANGE P4BROKERPORT.
export P4BROKERPORT=${SSL_PREFIX}${P4BROKERPORTNUM}

# These $P4D_FLAGS show when using 'ps' to scan the process table.
# Other environment variables and 'p4 configure' settings can affect
# p4d even if not listed on the command line.
# DO NOT CHANGE P4D_FLAGS.
export P4D_FLAGS="-p $P4PORT -r $P4ROOT -J $P4JOURNAL -L $P4LOG -q -d --pid-file"

# Default values of the following - may be overwritten below

# P4MASTERHOST=DNS Name (or IP address) of the commit server
# for this instance. If DNS is not available, /etc/hosts
# entries may be used to simulate DNS.
export P4MASTERHOST=REPL_P4MASTERHOST

# The P4MASTERPORT is the P4PORT value of the commit server, used
# by replicas, proxies, and standalone brokers. This incorporates
# teh DNS name of the commit server.
# DO NOT CHANGE P4MASTERPORT.
export P4MASTERPORT=${SSL_PREFIX}${P4MASTERHOST}:${P4PORTNUM}

# P4REPLICA is calculated. DO NOT CHANGE P4REPLICA.
export P4REPLICA=FALSE
# SHAREDDATA defaults FALSE, but is set to TRUE on replica servers that share their /hxdepots
# volume with their P4TARGET server - as per lbr.replication configurable
# DO NOT CHANGE SHAREDDATA.
export SHAREDDATA=FALSE

# SERVERID is defined in /p4/N/root/server.id.  DO NOT CHANGE SERVERID.
if [[ -n "$SERVERID" && "$SERVERID" != "$P4MASTER_ID" ]]; then
   export P4REPLICA=TRUE

   TmpP4ROOT=
   if [[ -f "$P4ROOT/db.config" && ! -f "$P4ROOT/P4ROOT_not_usable.txt" ]]; then
      TmpP4ROOT=$(mktemp -d "$P4TMP/tmp.p4root.XXXXXX")
      cp "$P4ROOT/db.config" "$TmpP4ROOT/."
      
      # Use egrep because it works where we need it to. Shellcheck suggests
      # using 'grep -E' instead.
      # shellcheck disable=SC2196
      targetServerPort=$("$P4DBIN" -r "$TmpP4ROOT" -cshow | egrep "^$SERVERID: P4TARGET = ")
      targetServerPort=${targetServerPort##* = }
      # Use 'echo' w/o quotes to remove leading/trailing whitespace.
      # shellcheck disable=SC2116 disable=SC2086
      targetServerPort=$(echo $targetServerPort)
   else
      targetServerPort=
   fi

   if [[ -n "$targetServerPort" ]]; then
      export P4MASTERPORT="$targetServerPort"
      if [[ "$targetServerPort" == *":"* ]]; then
         # strip off any trailing ':.....' element (e.g., ':1666')
         export P4MASTERHOST="${targetServerPort%:*}"
         # if P4TARGET includes ssl, also need to strip off ssl: from front
         export P4MASTERHOST="${P4MASTERHOST#*:}"
      fi
   fi

   repStyle="readonly"
   if [[ -f "$P4ROOT/db.config" && ! -f "$P4ROOT/P4ROOT_not_usable.txt" ]]; then
      # If this P4D data set is case-insensitive, check the configurables with a
      # case-insenstive match. If the P4D init script is a symlink, then the P4D
      # is case-sensitive, otherwise it is case-insensitive.
      if [[ -L "${P4DBIN}_init" ]]; then
         repStyle=$("$P4DBIN" -r "$P4ROOT" -cshow|"$GREP" "^$SERVERID: lbr.replication = ")
      else
         repStyle=$("$P4DBIN" -r "$P4ROOT" -cshow|"$GREP" -i "^$SERVERID: lbr.replication = " | head -1)
      fi

      repStyle=${repStyle##* = }
      # Use 'echo' w/o quotes to remove leading/trailing whitespace.
      # shellcheck disable=SC2116 disable=SC2086
      repStyle=$(echo $repStyle)
   fi

   if [[ $repStyle == "shared" ]]; then
      # Used by p4verify.sh among others
      export SHAREDDATA=TRUE
   fi
   # shellcheck disable=SC2115
   [[ -n "$TmpP4ROOT" ]] && rm -rf "$TmpP4ROOT/"
fi

#------------------------------------------------------------------------------
# Proxy settings. These PROXY_* settings can be left as they appear, or changed
# as desired. The usage of SSL_PREFIX does not need to be consistent in the
# PROXY_TARGET and PROXY_PORT (local listen port).  It is possible to have
# either, neither, or both PROXY_TARGET and PROXY_PORT using the SSL_PREFIX.
# The usage of the SSL for the PROXY_TARGET port value must reflect was is used
# by the target p4d server (a commit or edge).
export PROXY_TARGET=${SSL_PREFIX}${P4MASTERHOST}:${P4BROKERPORTNUM}
export PROXY_PORT=${SSL_PREFIX}$((P4BROKERPORTNUM+1))
# Set Proxy monitoring level to 3 in order to monitor all traffic for all operations
export PROXY_MON_LEVEL=3

# Add '-u $P4SERVICEUSER' to PROXY_V_FLAGS if required, i.e. if security >= 5.
export PROXY_V_FLAGS="-v server=1 -v track=1 -v proxy.monitor.level=${PROXY_MON_LEVEL:-3} -v net.autotune=1 -e 3072"

#------------------------------------------------------------------------------
# Perforce Defect Tracking Gateway (P4DTG) Settings:
export P4DTG_CFG=$P4SERVER

#------------------------------------------------------------------------------
# If SNAPSHOT_SCRIPT is defined and set to the path of an executable
# script, daily_checkpoint.sh will call the referenced scrip after
# checkpoints are created. It is expected that the called custom script
# will create a snapshot or perform some other backup activity.

# The logic below allows for the called SNAPSHOT_SCRIPT to run only
# on the master server.
#
# A sample script to enable snapshots on an AWS master
# server is provided as: /p4/common/cloud/aws/bin/snapshot.sh
#
if [[ -n "$SERVERID" && "$SERVERID" == "$P4MASTER_ID" ]]; then
   # This snapshot script is exectued only on the master server.
   export SNAPSHOT_SCRIPT=
else
   # Leave this value empty.
   export SNAPSHOT_SCRIPT=
fi

#------------------------------------------------------------------------------
# Set VERIFY_SDP_SKIP_TEST_LIST as an alternative to passing in a lists of tests
# using the '-skip' flag to the verify_sdp.sh script. The command line flag to
# the script is more appropriate if a test is to be skipped temporarily, while
# setting the VERIFY_SDP_SKIP_TEST_LIST variable is better for making a permanent
# exception (e.g. skipping crontab checks if some other scheduler is used).
#
# The value is a comma-delimited list of tests to skip, as defined in the
# documentation for verify_sdp.sh.  For more info, run: verify_sdp.sh -man
#
# Valid test names are:

#	* cron|crontab: Skip crontab check. Use this if you do not expect crontab to
#	be configured, perhaps if you use a different scheduler.
#	* excess: Skip checks for excess copies of p4d/p4p/p4broker in PATH.
#	* init: Skip compare of init scripts w/templates in /p4/common/etc/init.d
#	* license: Skip license related checks.
#	* commitid: Skip check ensuring ServerID of commit starts with 'commit' or 'master'.
#	* masterid: Synonym for commitid.
#	* offline_db: Skip checks that require a healthy offline_db.
#	* p4root: Skip checks that require healthy P4ROOT db files.
#	* p4t_files: Skip checks for existence of P4TICKETS and P4TRUST files.
#	* passwd|password: Skip SDP password checks.
#	* version: Skip version checks.
export VERIFY_SDP_SKIP_TEST_LIST=

#------------------------------------------------------------------------------
# The SDP_ALWAYS_LOGIN should be set to 0 except in certain rare situations
# with older p4d servers.  With the value set to 0, the p4login script does
# a 'p4 login -s' first to see if a ticket is valid, and logs in again only
# if required. Generally the P4USER in this script will have a non-expiring
# ticket.
export SDP_ALWAYS_LOGIN=0

#------------------------------------------------------------------------------
# The SDP_MAX_START_DELAY_* settings are numeric values that define the maximum
# number of second to wait for services to start before considering the startup
# a failure in various SDP scripts. Successful start is indicated by being
# responsive to a 'p4 info' command and the appropriate port for each service.
export SDP_MAX_START_DELAY_P4D=120
export SDP_MAX_START_DELAY_P4BROKER=60
export SDP_MAX_START_DELAY_P4P=60

# The SDP_MAX_STOP_DELAY_* settings are numeric values that define the maximum
# number of second to wait for services to stop before considering the shutdown
# a failure in various SDP scripts. Successful stop is indicated by removal of
# the server.pid file for p4d, and lack of active pids for p4broker/p4p. These
# values are typically never used. Note that 'p4d' does not have a max delay.
export SDP_MAX_STOP_DELAY_P4BROKER=600
export SDP_MAX_STOP_DELAY_P4P=600

#------------------------------------------------------------------------------
# Set SDP_AUTOMATION_USERS to a comma-delimited list of Perforce user accounts
# to login when the 'p4login' script is used with the '-automation' flag. This
# will normally be a list of users that operate trigger scripts, such as the
# 'swarm' user if Helix Swarm is used. Typically such users will have long-term
# tickets.  For more info, run: p4login -man
export SDP_AUTOMATION_USERS="swarm"

#------------------------------------------------------------------------------
# Set DO_PARALLEL_CHECKPOINTS=0 to disable parallel checkpoints.
#
# Set DO_PARALLEL_CHECKPOINTS=N to enable parallel checkpoints, where N
# is a positive integer indicating the number of parallel threads to use.
# As a special case, setting DO_PARALLEL_CHECKPOINTS=1 is the same as
# setting DO_PARALLEL_CHECKPOINTS=4.  The N value is passed to the p4d
# with the '-N' parameter when doing checkpoint create, dump, and
# replay options with 'p4d -jcp', 'p4d -jdp', and 'p4d -jrp',
# respectively.
#
# If parallel checkpoints are enabled:
# * The live_checkpoint.sh will create live checkpoints using 'p4d -jcp', and
#   replay using 'p4d -jrp'.
# * The daily_checkpoint.sh will create offline checkpoints using 'p4d -jdp',
#   and replay using 'p4d -jrp'.
# * The recover_offline_db.sh will look for checkpoint directories rather
#   than singular checkpoint files, and replay with 'p4d -jrp'.
# * The refresh_P4ROOT_from_offline_db.sh replays using 'p4d -jrp'.
#
# The db.checkpoint.threads configurable is ignored with the scripts, as
# the '-N' parameter overrides configurable.
#
# Parallel checkpoints became available in p4d 2022.2. This setting is
# ignored if the server version is lower than that.
export DO_PARALLEL_CHECKPOINTS=0

#------------------------------------------------------------------------------
# The umask affects default permissions on created files and directories.
umask 0026

#------------------------------------------------------------------------------
# Custom Local Changes
#------------------------------------------------------------------------------
# Any instance-specific local customizations 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 "/p4/common/site/config/$P4SERVER.vars.local.d" ]]; then
   # shellcheck disable=SC1090
   for f in "/p4/common/site/config/$P4SERVER".vars.local.d/*; do source "$f"; done
fi

if [[ -r "/p4/common/site/config/$P4SERVER.vars.local" ]]; then
   # shellcheck disable=SC1090
   source "/p4/common/site/config/$P4SERVER.vars.local"
fi