#!/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 used to change the role of a standby server to the master.
# Make sure replication is in sync and stop the master first.
#
# After validating that the new master is working properly, you can then switch DNS. 
#
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/convert_standby_to_master.log

######### Start of Script ##########
check_vars
set_vars
check_uid
check_dirs
log "Start failover"
stop_p4d
rm -f $P4ROOT/server.id.replica
mv $P4ROOT/server.id $P4ROOT/server.id.replica
echo $P4MASTER_ID > $P4ROOT/server.id
echo p4d_master > $P4ROOT/sdp_server_type.txt
jnl=$(cat ${P4ROOT}/statejcopy | cut -d "/" -f 1)
rm -f $P4JOURNAL
mv $LOGS/journal.${jnl} $P4JOURNAL
rm -f ${P4ROOT}/state*
rm -f ${P4ROOT}/rdb.lbr
$P4DBIN -r $P4ROOT -jr -f $P4JOURNAL
start_p4d
echo "Don't forget to update the address of the new standby server form."
log "End failover"

