#!/bin/bash
# Intended to be run on a replica machine to sync the checkpoints and or journals from its 
# corresponding master and then create a checkpoint from the existing offline database. Note
# that this script expects the offline database to already exist, so you need to prep that with
# a checkpoint taken on the master first.

export SDP_INSTANCE=${SDP_INSTANCE:-Undefined}
export SDP_INSTANCE=${1:-$SDP_INSTANCE}
if [[ $SDP_INSTANCE == Undefined ]]; then
   echo "Instance parameter not supplied."
   echo "You must supply the Perforce instance as a parameter to this script."
   exit 1
fi
# shellcheck disable=SC1091
source /p4/common/bin/p4_vars "$SDP_INSTANCE"
# shellcheck disable=SC1091
source /p4/common/bin/backup_functions.sh
# shellcheck disable=SC2034
LOGFILE=$LOGS/sync_replica.log

######### Start of Script ##########
check_vars
set_vars
get_journalnum
rotate_last_run_logs
log "Starting sync_replica_ckp.sh"

if [[ "${P4REPLICA}" == "FALSE" ]]; then
  die "Error: sync_replica can only be run a read only or forwarding replica."
fi

check_uid

# You must set up a public keypair using "ssh-keygen -t rsa" in order for this to work.
# You need to paste your CLIENT ~/.ssh/id_rsa.pub contents into the REMOTE ~/ssh/authorized_keys file.
if [[ "$SHAREDDATA" == "FALSE" ]]; then
  rsync -avz --delete ${RSYNCUSER}@${P4MASTERHOST}:$CHECKPOINTS/ $CHECKPOINTS >> $LOGFILE 2>&1
  rsync_exit_code=$?
  if [[ $rsync_exit_code -ne 0 ]]; then
    rsync -avz --delete ${RSYNCUSER}@${P4MASTERHOST}:$CHECKPOINTS/ $CHECKPOINTS >> $LOGFILE 2>&1
    rsync_exit_code=$?
    if [[ $rsync_exit_code -ne 0 ]]; then
      die "Error: Failed to pull $CHECKPOINTS from host $P4MASTERHOST.  The rsync exit code was: $rsync_exit_code.  Aborting."
    fi
  fi
fi

log "Start $P4SERVER Sync Checkpoint"
check_dirs
check_offline_db_usable
ckp_running
"$P4CBIN"/p4login
replay_journals_to_offline_db
# shellcheck disable=SC2034
ROOTDIR="$OFFLINE_DB"
get_offline_journal_num
dump_checkpoint

# You must set up a public keypair using "ssh-keygen -t rsa" in order for this to work.
# You need to paste your CLIENT ~/.ssh/id_rsa.pub contents into the REMOTE ~/ssh/authorized_keys file.
if [[ "$SHAREDDATA" == "FALSE" ]]; then
  rsync -avz --delete $CHECKPOINTS/*.ckp.* ${RSYNCUSER}@${P4MASTERHOST}:$CHECKPOINTS/ >> $LOGFILE 2>&1
  rsync_exit_code=$?
  if [[ $rsync_exit_code -ne 0 ]]; then
    rsync -avz --delete $CHECKPOINTS/*.ckp.* ${RSYNCUSER}@${P4MASTERHOST}:$CHECKPOINTS/ >> $LOGFILE 2>&1
    rsync_exit_code=$?
    if [[ $rsync_exit_code -ne 0 ]]; then
      die "Error: Failed to copy $CHECKPOINTS to host $P4MASTERHOST.  The rsync exit code was: $rsync_exit_code.  Aborting."
    fi
  fi
fi

recreate_offline_db_files
get_journalnum
get_offline_journal_num
replay_journals_to_offline_db
if [[ -d ${P4HOME}/journals.rep ]]; then
   cd ${P4HOME}/journals.rep
   rm $(ls -t | awk 'NR>1') > /dev/null 2>&1
fi
remove_old_logs
log "End $P4SERVER Sync Checkpoint"
set_counter
ckp_complete

get_journalnum
get_offline_journal_num

# LASTJOURNALNUM is the highest numbered journal number in the checkpoints directory.
if [[ $JOURNALNUM -lt $LASTJOURNALNUM ]]; then
  stop_p4d >> $LOGFILE 2>&1
  exit_code=$?

  if [[ $exit_code -ne 0 ]]; then
    die "Error: Server failed to shut down.  Aborting."
  fi

  log "Server should be down now."
  log "Moving offline db into root."
  replay_journals_to_offline_db
  switch_db_files

  log "Remove replication files to reset replication to beginning of current master journal."
  rm -f ${OFFLINE_DB}/state* >> $LOGFILE 2>&1
  rm -f ${OFFLINE_DB}/rdb.lbr >> $LOGFILE 2>&1
  rm -f ${P4ROOT}/state* >> $LOGFILE 2>&1
  rm -f ${P4ROOT}/rdb.lbr >> $LOGFILE 2>&1
  rm -f ${LOGS}/journal* >> $LOGFILE 2>&1

  start_p4d >> $LOGFILE 2>&1
  $P4CBIN/p4login

  log "Server should be back up now."

  $P4BIN pull -lj >> $LOGFILE
  rm -f ${OFFLINE_DB}/save/db.* >> $LOGFILE 2>&1
  rm -f ${P4ROOT}/save/db.* >> $LOGFILE 2>&1
  recreate_offline_db_files
  replay_journals_to_offline_db
fi

$P4CBIN/p4login
log "End $P4SERVER sync checkpoint replica"
