#!/bin/bash
#
# Copyright 2022 Cohesity Inc.
#
# Author: Jaitesh Pachipulusu
#
# This executable(script) is called on the local node by apply-patch. It
# extracts the apply_baseos_patch.sh script from the baseos patch tarball
# and executes it.
# This script will be run from DATA_DIR.

# Standard places
COHESITY_HOME_DIR="/home/cohesity"
DATA_DIR="${COHESITY_HOME_DIR}/data"
PATCHES_DIR="${DATA_DIR}/patches"
PATCH_TMPDIR="${DATA_DIR}/iris"
BASEOS_DIR="${PATCHES_DIR}/baseos"

declare -a SOURCE_DIRS_ARR=(
  "${PATCH_TMPDIR}" \
  "${DATA_DIR}" \
  "${PATCHES_DIR}" \
  "${COHESITY_HOME_DIR}" \
)

PROGNAME=$(basename $0)
PROG_VERSION=1

function usage() {
  echo "Usage: ${PROGNAME} <baseos-patch-file>"
  echo "    <baseos-patch-file-name is a patch file like"
  echo "        baseos-6.6.0d_u2_cpd-patch-2022Mar07-d20a8f33.tgz"
  echo
  exit 1
}

# Returns 1 if apply_baseos_patch.sh (orch process) is running on the current
# node.
function concurrent_baseos_patch_application_check() {
  running_orch=$(sudo ps axf | grep apply_baseos_patch.sh | grep -v grep)
  rc=$?
  if [ $rc -eq 0 ]; then
    echo "BaseOS patch application is already running."
    echo "PID: $(echo ${running_orch} | awk '{print $1}')"
    echo "Do not apply BaseOS patch simultaneously on one or more nodes."
    return 1
  fi
  return 0
}

#------------------------------------------------------------------------------
if [ "$#" -ne "1" ]; then
  usage
fi

concurrent_baseos_patch_application_check
rc=$?
if [ $rc -ne 0 ]; then
  exit $rc
fi

readonly baseos_patch_file=$1
readonly baseos_patch_file_name=$(basename ${baseos_patch_file})

# Clean BASEOS_DIR before starting the patching.
rm -rf ${BASEOS_DIR}
mkdir -p ${BASEOS_DIR}

# We need to place the baseos patch tarball in BASEOS_DIR for next steps(untar).
# If absolute path of the baseos patch was given. copy it and we're done.
if [ -f ${baseos_patch_file} ]; then
  sudo cp -f ${baseos_patch_file} ${BASEOS_DIR}/ > /dev/null 2>&1
else
  # Although we expect baseos patch to be present only in the below two
  # locations,
  # PATCHES_DIR - when apply_patch.sh is used to apply hotfix.
  # PATCH_TMPDIR - when hotfix is applied via UI.
  # There were instances where it was placed in DATA_DIR and COHESITY_HOME_DIR
  # and apply command was called. So check for these extra 2 locations too.
  for dir in ${SOURCE_DIRS_ARR[@]}; do
    sudo cp -f ${dir}/${baseos_patch_file_name} ${BASEOS_DIR}/ > /dev/null 2>&1
  done
fi

sudo chmod -f 664 ${BASEOS_DIR}/${baseos_patch_file_name}
sudo chown -f cohesity:cohesity ${BASEOS_DIR}/${baseos_patch_file_name}

if [ ! -f ${BASEOS_DIR}/${baseos_patch_file_name} ]; then
  echo "BaseOS patch file ${BASEOS_DIR}/${baseos_patch_file_name} not found."
  exit 1
fi

# This is required for apply-patch to monitor the baseos patch apply status.
echo $$ > ${PATCHES_DIR}/mypid

cd ${DATA_DIR}

# Now extract only the main orchestrator script into DATA_DIR.
# We are stripping 'patches', 'baseos' and putting apply_baseos_patch.sh
# in the DATA_DIR
tar -C ${DATA_DIR} -xf "${BASEOS_DIR}/${baseos_patch_file_name}" \
  patches/baseos/apply_baseos_patch.sh --strip-components=2
code="$?"
if [ "${code}" != "0" ]; then
  echo "Failed to extract apply_baseos_patch.sh script from tarball."
  echo "Code: ${code}"
  exit 1
fi

# Now run the script.
${DATA_DIR}/apply_baseos_patch.sh ${baseos_patch_file_name}
rc=$?

exit $rc
