#!/bin/bash
# Intended to be run on a replica machine to sync replica from its corresponding master
# Like sync_replica.sh but for use in a regular crontab
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
rotate_last_run_logs
log "Starting weekly_sync_replica.sh"

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

check_uid

$P4CBIN/p4login_master
MASTERJOURNALNUM=$($P4BIN -u $P4USER -p ${P4MASTERPORT} counter journal)

if [[ "$MASTERJOURNALNUM" == "" ]]; then
   die "Error:  Couldn't get journal number from master.  Aborting."
fi

# We set JOURNALNUM to one less than the master since we are not truncating the journal
# and replay_journals_to_offline_db assumes that truncate_journal has been run.

JOURNALNUM=$(($MASTERJOURNALNUM-1))

# 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 ${OSUSER}@${P4MASTER}:$CHECKPOINTS/ $CHECKPOINTS > $LOGFILE 2>&1
   rsync_exit_code=$?

   if [[ $rsync_exit_code -ne 0 ]]; then
      die "Error: Failed to pull $CHECKPOINTS from host $P4MASTER.  The rsync exit code was: $rsync_exit_code.  Aborting."
   fi
fi

recreate_offline_db_files
get_offline_journal_num
replay_journals_to_offline_db

/p4/${SDP_INSTANCE}/bin/p4d_${SDP_INSTANCE}_init stop >> $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."
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 ${P4HOME}/logs/journal >> $LOGFILE 2>&1
rm -f ${P4HOME}/journals.rep/* >> $LOGFILE 2>&1

${P4HOME}/bin/p4d_${SDP_INSTANCE}_init start >> $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
get_offline_journal_num
replay_journals_to_offline_db

remove_old_logs
export CHECKPOINTS=${P4HOME}/journals.rep
remove_old_checkpoints_and_journals

$P4BIN pull -lj >> $LOGFILE
if [[ $? -ne 0 ]]; then
   die "Error: Replica not responding to pull. Check $P4SERVER replication."
fi

log "End $P4SERVER weekly sync replica log."
