#!/bin/bash
# Intended to be run on a replica machine to sync replica from its corresponding master
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 

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

######### Start of Script ##########
check_vars
set_vars
get_journalnum
ckp_running
rotate_last_run_logs
log "Starting sync_replica.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

if [[ "$SHAREDDATA" == "FALSE" ]]; then
  rsync -avz --delete ${RSYNCUSER}@${P4MASTERHOST}:$JOURNALS/ $JOURNALS >> $LOGFILE 2>&1
  rsync_exit_code=$?
  if [[ $rsync_exit_code -ne 0 ]]; then
    rsync -avz --delete ${RSYNCUSER}@${P4MASTERHOST}:$JOURNALS/ $JOURNALS >> $LOGFILE 2>&1
    rsync_exit_code=$?
    if [[ $rsync_exit_code -ne 0 ]]; then
      die "Error: Failed to pull $JOURNALS from host $P4MASTERHOST.  The rsync exit code was: $rsync_exit_code.  Aborting."
    fi
  fi
fi

recreate_offline_db_files
get_journalnum
# Reduce the journal number by 1 since the rotated journals in offline will be 1 less than
# the active journal number.
JOURNALNUM=$((JOURNALNUM - 1))
get_offline_journal_num
replay_journals_to_offline_db
CHECKPOINTS_ORIG=${CHECKPOINTS}
CHECKPOINTS=${P4HOME}/journals.rep
replay_journals_to_offline_db
CHECKPOINTS=$CHECKPOINTS_ORIG
get_journalnum

# 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."
  get_journalnum
  get_offline_journal_num
  replay_journals_to_offline_db
  CHECKPOINTS_ORIG=${CHECKPOINTS}
  CHECKPOINTS=${P4HOME}/journals.rep
  replay_journals_to_offline_db
  CHECKPOINTS=$CHECKPOINTS_ORIG
  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
fi

$P4CBIN/p4login
remove_old_logs
remove_old_checkpoints_and_journals
if [[ -d ${P4HOME}/journals.rep ]]; then
   cd ${P4HOME}/journals.rep
   rm $(ls -t | awk 'NR>3') > /dev/null 2>&1
fi
ckp_complete
log "End $P4SERVER sync replica"

