#!/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
#------------------------------------------------------------------------------
export SDP_INSTANCE=${SDP_INSTANCE:-Unset} 
export SDP_INSTANCE=${1:-$SDP_INSTANCE} 
if [[ $SDP_INSTANCE == Unset ]]; then 
   echo -e "\nError: Instance parameter not supplied.\n" 
   echo "You must supply the Perforce instance as a parameter to this script." 
   exit 1 
fi 

# Load SDP controlled shell environment.
source /p4/common/bin/p4_vars "$SDP_INSTANCE" ||\
   { echo -e "\nError: Failed to load SDP environment.\n"; exit 1; }

source $P4CBIN/backup_functions.sh ||\
   { echo -e "\nError: Failed to load SDP backup_functions.sh lib.\n"; exit 1; }

AWK=awk
MAIL=mail

export AWK
export MAIL

cd $P4CBIN || { echo -e "\nError: Failed to cd to $P4CBIN.\n"; exit 1; }
common_dir=`pwd -P` 

if [[ -d $common_dir ]]; then
  cd $common_dir || { echo -e "\nError: Failed to cd to $common_dir.\n"; exit 1; }
else
  echo $common_dir does not exist.
  exit 1
fi

######### Start of Script ##########

check_vars
set_vars

export LOGFILE=$LOGS/upgrade.log.$(date +'%Y%m%d-%H%M')

check_dirs
check_uid

[[ -f $common_dir/p4 ]] || { echo "No p4 in $common_dir" ; exit 1 ;}
[[ -f $common_dir/p4d ]] || { echo "No p4d in $common_dir" ; exit 1 ;}

chmod 777 $common_dir/p4
chmod 700 $common_dir/p4d
[[ -f $common_dir/p4broker ]] && chmod 755 $common_dir/p4broker
[[ -f $common_dir/p4p ]] && chmod 755 $common_dir/p4p

P4RELNUM=`./p4 -V | grep -i Rev. | $AWK -F / '{print $3}'`
P4DRELNUM=`./p4d -V | grep -i Rev. | $AWK -F / '{print $3}'`
P4BLDNUM=`./p4 -V | grep -i Rev. | $AWK -F / '{print $4}' | awk '{print $1}'`
P4DBLDNUM=`./p4d -V | grep -i Rev. | $AWK -F / '{print $4}' | awk '{print $1}'`
CURRENT_RELNUM=`./p4d_${SDP_INSTANCE}_bin -V | grep -i Rev. | $AWK -F / '{print $3}'`


log "Start $P4SERVER Upgrade"

[[ -f p4_$P4RELNUM.$P4BLDNUM ]] || cp p4 p4_$P4RELNUM.$P4BLDNUM
[[ -f p4d_$P4DRELNUM.$P4DBLDNUM ]] || cp p4d p4d_$P4DRELNUM.$P4DBLDNUM
[[ -f p4_${P4RELNUM}_bin ]] && unlink p4_${P4RELNUM}_bin
ln -s p4_$P4RELNUM.$P4BLDNUM p4_${P4RELNUM}_bin >> "$LOGFILE" 2>&1
[[ -f p4d_${P4DRELNUM}_bin ]] && unlink p4d_${P4DRELNUM}_bin
ln -s p4d_$P4DRELNUM.$P4DBLDNUM p4d_${P4DRELNUM}_bin >> "$LOGFILE" 2>&1
[[ -f p4_bin ]] && unlink p4_bin
ln -s p4_${P4RELNUM}_bin p4_bin >> "$LOGFILE" 2>&1

if [[ -L p4broker_${SDP_INSTANCE}_bin ]]; then
  /p4/${SDP_INSTANCE}/bin/p4broker_${SDP_INSTANCE}_init stop >> $LOGFILE 2>&1
  P4BRELNUM=`./p4broker -V | grep -i Rev. | $AWK -F / '{print $3}'`
  P4BBLDNUM=`./p4broker -V | grep -i Rev. | $AWK -F / '{print $4}' | $AWK '{print $1}'`
  [[ -f p4broker_$P4BRELNUM.$P4BBLDNUM ]] || cp p4broker p4broker_$P4BRELNUM.$P4BBLDNUM
  [[ -L p4broker_${P4BRELNUM}_bin ]] && unlink p4broker_${P4BRELNUM}_bin
  ln -s p4broker_$P4BRELNUM.$P4BBLDNUM p4broker_${P4BRELNUM}_bin
  [[ -L p4broker_${SDP_INSTANCE}_bin ]] && unlink p4broker_${SDP_INSTANCE}_bin
  ln -s p4broker_${P4BRELNUM}_bin p4broker_${SDP_INSTANCE}_bin
  /p4/${SDP_INSTANCE}/bin/p4broker_${SDP_INSTANCE}_init start >> $LOGFILE 2>&1
fi

if [[ -L p4p_${SDP_INSTANCE}_bin ]]; then
  /p4/${SDP_INSTANCE}/bin/p4p_${SDP_INSTANCE}_init stop >> $LOGFILE 2>&1
  P4PRELNUM=`./p4p -V | grep -i Rev. | $AWK -F / '{print $3}'`
  P4PBLDNUM=`./p4p -V | grep -i Rev. | $AWK -F / '{print $4}' | $AWK '{print $1}'`
  [[ -f p4p_$P4PRELNUM.$P4PBLDNUM ]] || cp p4p p4p_$P4PRELNUM.$P4PBLDNUM
  [[ -L p4p_${P4PRELNUM}_bin ]] && unlink p4p_${P4PRELNUM}_bin
  ln -s p4p_$P4PRELNUM.$P4PBLDNUM p4p_${P4PRELNUM}_bin
  [[ -L p4p_${SDP_INSTANCE}_bin ]] && unlink p4p_${SDP_INSTANCE}_bin
  ln -s p4p_${P4PRELNUM}_bin p4p_${SDP_INSTANCE}_bin
  /p4/${SDP_INSTANCE}/bin/p4p_${SDP_INSTANCE}_init start >> $LOGFILE 2>&1
fi

$P4CBIN/p4login

if [[ "$P4REPLICA" == "FALSE" ]] || [[ $EDGESERVER -eq 1 ]]; then
   get_journalnum
fi

/p4/${SDP_INSTANCE}/bin/p4d_${SDP_INSTANCE}_init stop >> $LOGFILE 2>&1

# Don't upgrade the database for minor upgrades
if [[ $CURRENT_RELNUM != $P4DRELNUM ]]; then
  # Only rotate the journal on the master. 
  if [[ "$P4REPLICA" == "FALSE" ]] && [[ $EDGESERVER -eq 0 ]]; then
    p4d_truncate_journal
    get_offline_journal_num
    replay_journals_to_offline_db
  fi
  unlink p4d_${SDP_INSTANCE}_bin >> "$LOGFILE" 2>&1
  ln -s p4d_${P4DRELNUM}_bin p4d_${SDP_INSTANCE}_bin >> "$LOGFILE" 2>&1

  log "Upgrading databases in $P4ROOT."
  $P4DBIN -r $P4ROOT -J off -xu >> "$LOGFILE" 2>&1
	# Upgrade the offline database on the master and on edge servers.
  if [[ "$P4REPLICA" == "FALSE" ]] || [[ $EDGESERVER -eq 1 ]]; then
    log "Upgrading databases in $OFFLINE_DB"
    # The value of '-t localhost:1666' is necessary for syntax reasons.
    # It allows the upgrade of the offline database without needing a license file.
    $P4DBIN -r $OFFLINE_DB -J off -t localhost:1666 -xu >> "$LOGFILE" 2>&1
  fi
fi

/p4/${SDP_INSTANCE}/bin/p4d_${SDP_INSTANCE}_init start >> $LOGFILE 2>&1

log "Finish $P4SERVER Upgrade"
