#!/bin/bash

# Entry point of OSV Image Upgrades
# osv_upgrade is upgrade8k's successor.
# This script preserves the interface with the other callers.

# Authors:
#    Nicolaos Chatiras,
#    Georgios Basis,
#    Antonios Chalas, for Atos (c), 2021

ARGS="$@"
PROC_NAME="$(basename $0)"
MAIN_LOGFILE="/log/prepare8k.log"
CFG8k=/etc/hiq8000/node.cfg
PRECHECK="/unisphere/srx3000/callp/bin/upgrade_precheck.sh"

source /usr/bin/common8k

function isInformativeCommand ( )
{
   [[ "${ARGS}" = "-s" ]] || return 1
   [[ "${ARGS}" = "-h" ]] || return 1
}

# This function checks if install8k is running
function install8kInProgress ( )
{
   local pid="$1"
   local ancestors_regex="$(getAllAncestors "${pid}" | tr ' ' '|')"

   local parent_procs="$(ps -ax | grep -Ew "${ancestors_regex}" | grep -v grep)"

   local current_node="$(uname -n)"
   getcfgvar ${CFG8k} node_1_name PRIMARY_HOST

   if [ "${current_node}" = "${PRIMARY_HOST}" ];
   then
      # If this is a recursive call do not re-initiate a logging session
      echo "${parent_procs}" | grep -qE "rpm.*UNSPmigration" && \
      echo "${parent_procs}" | grep -qE "sh.*install8k"
   else
      echo "${parent_procs}" | grep -qE "rpm.*UNSPmigration" && \
      ssh cluster_partner_alias 'ps -ax | grep -v grep | grep -qE "sh.*install8k"'
   fi
}

checkDBduplicate ()
{
   readonly checkDuplicates="/unisphere/srx3000/srx/dbscripts/repair/dupHunter.sh"
   readonly getDbVersion="/unisphere/srx3000/callp/bin/getDbVersion"
   local DBDUPLICATES=24

   node_mngr_num=$(ps -ef | grep RtpNm | wc | awk '{ print $1 }')
   if [ $node_mngr_num -lt 2 ]; then
      return 0
   fi

  if [ -x "$checkDuplicates" ]
  then
    $checkDuplicates > /dev/null 2>&1
    if [ $? -ne 0 ]
    then
       # DB state O.K.
       $getDbVersion > /dev/null 2>&1
       if [ $? -eq 0 ]
       then
	      echo "$(date) : upgrade8k : WARNING: Duplicate entries were found in the database. Run RapidStat to check which entries are affected."
         echo "$(date) : upgrade8k : WARNING: Duplicate entries were found in the database. Run RapidStat to check which entries are affected." >> /log/prepare8k.log 2> /dev/null
         return $DBDUPLICATES
       fi
     fi
   fi
   return 0
}

### Main ###
readonly getLicFilevesrion="/unisphere/srx3000/callp/bin/getLicver.sh"

echo "Calling upgrade8k ${ARGS}"

if [ "$1" != "-s" ]; then
   echo " test running the code"
   
   checkDBduplicate

   if [ -x "$getLicFilevesrion" ]
   then
      su - srx -c "$getLicFilevesrion"
   fi
   
fi

# If this is a recursive call or install8k is in progress, do not re-initiate a logging session
# There is no need to log informative operations as well (e.g -s, -h)
if processCalledByImageUpgrade "$$" || \
   install8kInProgress "$$"         || \
   isInformativeCommand "$@";
then
   if [ "$(whoami)" = "srx" ]; then
      sudo /usr/bin/licensecontroller "${ARGS}"
   else
      su - srx -c "/usr/bin/licensecontroller ${ARGS}"
   fi
   exit $?
fi

if [ "$(whoami)" = "srx" ]; then
   sudo  /usr/bin/licensecontroller "${ARGS}" 2>&1 | tee -a ${MAIN_LOGFILE}
else
   su - srx -c "/usr/bin/licensecontroller ${ARGS}" 2>&1 | tee -a ${MAIN_LOGFILE}
fi
