#!/bin/bash
#==============================================================================
# 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
#------------------------------------------------------------------------------

# This script is intended to be run on a standby replica.  It essentially just
# calls 'p4 admin checkpoint -Z' to reqeust a checkpoint and exits. The actual
# checkpoint is created on the next journal rotation on the master.

export SDP_INSTANCE="${SDP_INSTANCE:-Unset} "
export SDP_INSTANCE="${1:-$SDP_INSTANCE}"
if [[ "$SDP_INSTANCE" == Unset ]]; then 
   echo "Instance parameter not supplied." 
   echo "You must supply the Perforce instance as a parameter to this script." 
   exit 1 
fi 

source /p4/common/bin/p4_vars $SDP_INSTANCE
source /p4/common/bin/backup_functions.sh
LOGFILE=$LOGS/request_checkpoint.log

######### Start of Script ##########
check_vars
set_vars
rotate_last_run_logs
log "Start $P4SERVER Replica Request Checkpoint"
check_uid
check_dirs
$P4CBIN/p4login

log "$P4BIN admin checkpoint -Z"
$P4BIN admin checkpoint -Z >> "$LOGFILE" 2>&1 &

remove_old_checkpoints_and_journals
remove_old_logs
log "End $P4SERVER Replica Checkpoint"
mail_log_file "$HOSTNAME $P4SERVER replica request checkpoint log."
