# Various scripts send email to $MAILTO.  This can contain comma-delimited list
# of email addresses (no spaces).
export MAILTO=REPL_MAILTO

# Specify the email adddress from which emails are sent in $MAILFROM.
# This must be exactly one email address.
export MAILFROM=REPL_MAILFROM

export P4USER=REPL_ADMINUSER
export P4SERVICEUSER=svc_${SERVERID}

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

# The setting below should be ssl: if this instance uses ssl.
export SSL_PREFIX=REPL_SSLPREFIX

export P4PORTNUM=REPL_P4PORT
export P4PORT=${SSL_PREFIX}${P4PORTNUM}

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

# These $P4D_FLAGS show when using 'ps' to scan the process table.
# Other settings affect p4d.
export P4D_FLAGS="-p $P4PORT -r $P4ROOT -J $P4JOURNAL -L $P4LOG -q -d --pid-file"

# SERVERID is defined in /p4/n/root/server.id. 
# Override P4TICKETS and P4TRUST for replicas.
if [[ -n "$SERVERID" && "$SERVERID" != "$P4MASTER_ID" ]]; then
   export P4REPLICA=TRUE

   export P4USER=REPL_ADMINUSER
   if [[ -f $P4ROOT/db.domain ]]; then
      targetServerPort=$($P4DBIN -r $P4ROOT -cshow | egrep "^$SERVERID: P4TARGET = ")
      targetServerPort=${targetServerPort##* = }
      targetServerPort=$(echo $targetServerPort)
   else
      targetServerPort=$P4PORTNUM
   fi

   if [[ -n "$targetServerPort" ]]; then
      export P4MASTERPORT=$targetServerPort

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


   # Set SHAREDDATA=TRUE on replica servers that share their /hxdepots
   # volume with their P4TARGET server.
   if [[ -f $P4ROOT/db.domain ]]; then
      repStyle=$($P4DBIN -r $P4ROOT -cshow|$GREP "^$SERVERID: lbr.replication = ")
      repStyle=${repStyle##* = }
      repStyle=$(echo $repStyle)
   else
      repStyle=readonly
   fi

   if [[ $repStyle == "shared" ]]; then
      export SHAREDDATA=TRUE
   else
      export SHAREDDATA=FALSE
   fi
else
   export P4REPLICA=FALSE

   # This is the DNS name of the master server:
   export P4MASTER=REPL_DNSNAME
   export SHAREDDATA=FALSE
   export P4MASTERPORT=${SSL_PREFIX}${P4MASTER}:${P4PORTNUM}
fi

# Proxy settings
export PROXY_TARGET=${SSL_PREFIX}${P4MASTER}:${REPL_P4P_TARGET_PORT:-$P4PORTNUM}
export PROXY_PORT=$P4PORT

# P4DTG Settings:
export P4DTG_CFG=$P4SERVER

export SDP_ALWAYS_LOGIN=0

umask 0026