#!/bin/bash

# -------------------------------------------------------
# file   : activate8k.sh
# purpose: Activates the fallback system load.
# authors:
#	Alexandros Cheimarios <alexandros.cheimarios@unify.com>
#	Miltiadis Koutsokeras <miltiadis.koutsokeras@unify.com>
#	Vittal.S
#	
# usage:
#	activate8k [  -h | -q | -v | -reset | -done | -check | -auto ]
#
# -------------------------------------------------------

### Script setup
_SELF_="$(basename "$(readlink -e "${BASH_SOURCE[0]}")" && echo X)" && \
readonly _SELF_="${_SELF_%$'\nX'}"
_SELFDIR_="$(dirname "$(readlink -e "${BASH_SOURCE[0]}")" && echo X)" && \
readonly _SELFDIR_="${_SELFDIR_%$'\nX'}"

### Constants
readonly sync8k_script_name='sync8k'
readonly sync8k_path="${_SELFDIR_}/${sync8k_script_name}"
readonly grubinstaller="${_SELFDIR_}/install_bootloader.sh"
readonly root_prim_part=2
readonly root_sec_part=3
readonly optimg_part=12
readonly grub1menu="/boot/grub/menu.lst"
readonly grub2menu="/boot/grub2/grub.cfg"
readonly IMGDIR='/img/rootimg'
readonly SRXBIN='/unisphere/srx3000/srx/bin/'
readonly SRXBOOT='/unisphere/srx3000/srx/startup/srxBootParams'
readonly LOGFILE='/log/activate8k.auto'
readonly MYPID="${$}"
#///////////////////  Exit Error Codes ////////////////////////
readonly Success=0
readonly UidMisMatch=1
readonly NoFallbackAvailable=2
readonly UserAbortActivate=3
readonly UnknownParameter=4
readonly ImageNotActivated=5
readonly ClusterIcPlumbFailed=6
readonly InvalidNodeNames=7
readonly GrubInstFailure=8
readonly InvalidDiskType=9
readonly Activate8kAlreadyRunning=10
readonly ActivateInitiationFailure=11
readonly ActivateInitNotFound=12
readonly ActivateInitNotSuccessfull=13
readonly Sync8kAlreadyRunning=14
readonly CannotDetectActiveLoad=15
readonly CannotReadCfgValue=16
readonly MalformedMountTab=17
readonly UnsupportedProduct=18
#///////////////////  Exit Error Codes ////////////////////////

### Include files
if ! source ${_SELFDIR_}/common.inc
then
    echo "FATAL ERROR: Cannot include ${_SELFDIR_}/common.inc" 1>&2
    exit 128
fi

### Functions

# -------------------------------------------------------
# function: syncnexit
# args    :  none
# description:
#    always sync file system before exit. Remember murpy.
# -------------------------------------------------------
syncnexit( ) {
   sync
   exit ${1}
}

# -------------------------------------------------------
# function:  log
# args    :  none
# description:
#   log every bit.
# -------------------------------------------------------
log( ) {
   echo -n "[${_SELF_} - ${MYPID}]: " >> ${LOGFILE}
   echo "$1" | tee -a ${LOGFILE}
}

# ----------------------------------------------------------------
# function: rssh
# args    : none
# description:
#   Make sure  to execure command for partner only in regular mode.
# ----------------------------------------------------------------
rssh( ) {
   if [ "X${LIVE8kUPGRADE}X" = "XfalseX" ]
   then
      ssh $*
   else
      log "   - Skipping ssh to partner"
   fi
}

# -------------------------------------------------------
# function: display
# args    :  none
# description:
#   displays layout
# -------------------------------------------------------
display( ) {
   if [ "X${product}X" = "XHiPath8000X" ]
   then
       eval "dfsda[0]=$(df -h /dev/${diskdev}2   | grep '^/dev' | awk '{ print $5 }')"
       eval "dfsda[1]=$(df -h /dev/${diskdev}3   | grep '^/dev' | awk '{ print $5 }')"
       eval "dfsda[2]=$(df -h /dev/${diskdev}5   | grep '^/dev' | awk '{ print $5 }')"
       eval "dfsda[3]=$(df -h /dev/${diskdev}6   | grep '^/dev' | awk '{ print $5 }')"
       eval "dfsda[4]=$(df -h /dev/${diskdev}7   | grep '^/dev' | awk '{ print $5 }')"
       eval "dfsda[5]=$(df -h /dev/${diskdev}8   | grep '^/dev' | awk '{ print $5 }')"
       eval "dfsda[6]=$(df -h /dev/${diskdev}9   | grep '^/dev' | awk '{ print $5 }')"
       eval "dfsda[7]=$(df -h /dev/${diskdev}10  | grep '^/dev' | awk '{ print $5 }')"
       eval "dfsda[8]=$(df -h /dev/${diskdev}11  | grep '^/dev' | awk '{ print $5 }')"
       eval "dfsda[9]=$(df -h /dev/${diskdev}12  | grep '^/dev' | awk '{ print $5 }')"
       eval "dfsda[10]=$(df -h /dev/${diskdev}13 | grep '^/dev' | awk '{ print $5 }')"
       eval "dfsda[11]=$(df -h /dev/${diskdev}14 | grep '^/dev' | awk '{ print $5 }')"
       eval "dfsda[12]=$(df -h /dev/${diskdev}15 | grep '^/dev' | awk '{ print $5 }')"
   else
       eval "dfsda[0]=$(df -h /dev/${diskdev}2   | grep '^/dev' | awk '{ print $5 }')"
       eval "dfsda[1]=$(df -h /dev/${diskdev}3   | grep '^/dev' | awk '{ print $5 }')"
       eval "dfsda[2]=$(df -h /dev/${diskdev}5   | grep '^/dev' | awk '{ print $5 }')"
       eval "dfsda[3]=$(df -h /dev/${diskdev}6   | grep '^/dev' | awk '{ print $5 }')"
       eval "dfsda[4]=$(df -h /dev/${diskdev}7   | grep '^/dev' | awk '{ print $5 }')"
       eval "dfsda[5]=$(df -h /dev/${diskdev}8   | grep '^/dev' | awk '{ print $5 }')"
       eval "dfsda[6]=$(df -h /dev/${diskdev}9   | grep '^/dev' | awk '{ print $5 }')"
   fi

   # Get the active load
   local result_exit_code=''
   get_active_load ${diskdev}; result_exit_code=$?
   if [ "X${RESET}X" != "XtrueX" ]
   then

      if [ ${result_exit_code} -eq 0 ] # Primary active
      then
          echo "Located the active image: /dev/${diskdev}2( primary )"
          syncpart="primary"
          syncother="secondary"
          sda2active="(*)"
      elif [ ${result_exit_code} -eq 1 ] # Secondary active
      then
          echo "Located the active image: /dev/${diskdev}3( secondary )"
          syncpart="secondary"
          syncother="primary"
          sda3active="(*)"
      else
          logerror "FATAL: Cannot detect the active image load of block device [${diskdev}]"
          exit ${CannotDetectActiveLoad}
      fi

   else

      if [ ${result_exit_code} -eq 0 ] # Primary active
      then
          echo "Resetting the active image: /dev/${diskdev}2( primary )"
          syncpart="secondary"
          syncother="primary"
          sda2active="(*)"
      elif [ ${result_exit_code} -eq 1 ] # Secondary active
      then
          syncpart="primary"
          syncother="secondary"
          echo "Resetting the active image: /dev/${diskdev}3( secondary )"
          sda3active="(*)"
       else
          logerror "FATAL: Cannot detect the active image load of block device [${diskdev}]"
          exit ${CannotDetectActiveLoad}
       fi

   fi

   echo "Detected ${disktype} disk."
   echo "///////////////////////////////////////////////////////////"
   echo "-----------------------------------------------------------"
   echo ".                                                         ."
   echo ".        Root    Root                                     ."
   echo ". swap   ImgPrim ImgSec   unisphere   more ...      var   ."
   echo ".  |       |      |         /  \                   /  \   ."
   echo ". \ /     \ /    \ /       /    \                 /    \  ."
   echo ".  |       |      |      Prim   Sec             Prim  Sec ."
   echo ". ${diskdev}1   ${diskdev}2    ${diskdev}3     ${diskdev}5        ...        ${diskdev}15    ."
   echo ".        ${sda2active}        ${sda3active}                                      ."
   echo ".                                                         ."
   echo "-----------------------------------------------------------"
   echo ".(*) on the ${diskdev} line shows active root.                   ."
   echo "///////////////////////////////////////////////////////////"
   echo

   echo "Disk Map"
   echo "///////////////////////////////////////////////////////////"
   echo "-----------------------------------------------------------"
   echo ".  partition          mount      %used                     "
   if [ "X${product}X" = "XHiPath8000X" ]
   then
       echo ".  /dev/${diskdev}2          root       ${dfsda[0]}"
       echo ".  /dev/${diskdev}3          root       ${dfsda[1]}"
       echo ".  /dev/${diskdev}5          unisphere  ${dfsda[2]}"
       echo ".  /dev/${diskdev}6          global     ${dfsda[3]}"
       echo ".  /dev/${diskdev}7          log        ${dfsda[4]}"
       echo ".  /dev/${diskdev}8          tmp        ${dfsda[5]}"
       echo ".  /dev/${diskdev}9          home       ${dfsda[6]}"
       echo ".  /dev/${diskdev}10         software   ${dfsda[7]}"
       echo ".  /dev/${diskdev}11         cdr        ${dfsda[8]}"
       echo ".  /dev/${diskdev}12         opt        ${dfsda[9]}"
       echo ".  /dev/${diskdev}13         tpa        ${dfsda[10]}"
       echo ".  /dev/${diskdev}14         enterprise ${dfsda[11]}"
       echo ".  /dev/${diskdev}15         var        ${dfsda[12]}"
   else
       echo ".  /dev/${diskdev}2          root       ${dfsda[0]}"
       echo ".  /dev/${diskdev}3          root       ${dfsda[1]}"
       echo ".  /dev/${diskdev}5          opt        ${dfsda[2]}"
       echo ".  /dev/${diskdev}6          db         ${dfsda[3]}"
       echo ".  /dev/${diskdev}7          var        ${dfsda[4]}"
       echo ".  /dev/${diskdev}8          tmp        ${dfsda[5]}"
       echo ".  /dev/${diskdev}9          software   ${dfsda[6]}"
   fi

   echo "-----------------------------------------------------------"
   echo "///////////////////////////////////////////////////////////"

   #############################################################################
   # Check how the boot loader is setup for the next boot and inform the user
   if [ "X${syncpart}X" = "XprimaryX" ]
   then
       bootpart="(hd0,1)"
       msdosbootpart="(hd0,msdos2)"
   else
       bootpart="(hd0,2)"
       msdosbootpart="(hd0,msdos3)"
   fi

       # First check the latest GRUB2 config instead of the legacy GRUB1 to avoid the case of a stale file
       if [ -f ${grub2menu} ]
       then
           sda2Mount=`lsblk | grep sda2 | awk '{print $7}'`
            if [ "$sda2Mount" == "/img/rootimg" ]; then
                currentLoad="secondary"
            else
                currentLoad="primary"
            fi
            grub=`cat /boot/grub2/grub.cfg`
            nextBoot=`echo "$grub" | grep "set root='(" | head -n1 | sed -n 's/.*root=//p'`
            if [ "$nextBoot" == "'(hd0,msdos3)'" ]; then
                if [ "$currentLoad" == "secondary" ]; then
                    result_exit_code=0
                else
                    result_exit_code=1
                fi
            else
                if [ "$currentLoad" == "primary" ]; then
                    result_exit_code=0
                else
                    result_exit_code=1
                fi
            fi

       elif [ -f ${grub1menu} ]
       then
           cat ${grub1menu} | grep "kernel" | head -1 | grep -q "${bootpart}"; result_exit_code=$?
       else
           logerror "Cannot find the boot loader configuration file in order to determine the image to be loaded in next boot"
       fi

   if [ ${result_exit_code} -eq 0 ]
   then
       log "Note:Your active(current) image will be loaded on next boot."
   else
       log "Note:Your fallback image will be loaded on next boot."
   fi
   #############################################################################

   if [ -s ${IMGDIR}/lock/activate8k.lock ]
   then
       cat ${IMGDIR}/lock/activate8k.lock
   fi

}

# -------------------------------------------------------
# function: getloadinfo
# args    :  none
# description:
#   Find the load and patchset information.
# -------------------------------------------------------
getloadinfo( ) {

   if [ "X${product}X" = "XHiPath8000X" ]
   then

       # Get BASE LOAD VERSIONS
       if get_load_buildid 'primary'
       then
           primloadversion="${_LOADVERSION_}"
       else
           primloadversion="None"
       fi
       if get_load_buildid 'secondary'
       then
           secloadversion="${_LOADVERSION_}"
       else
           secloadversion="None"
       fi

       # Get full patchset versions
       if get_load_fullpsversion 'primary'
       then
           primpatchversion="${_LOADFULLPSVERSION_}"
       else
           primpatchversion="None"
       fi
       if get_load_fullpsversion 'secondary'
       then
           secpatchversion="${_LOADFULLPSVERSION_}"
       else
           secpatchversion="None"
       fi

   else # if [ "X${product}X" = "XHiPath8000X" ]

      if [ "X${product}X" = "XOpenScapeX" ]
      then
         primloadversion="OpenScape$component-Primary"
         secloadversion="OpenScape$component-Secondary"
         primpatchversion="None"
         secpatchversion="None"

         # TODO: This is not a regular Openscape Voice product, but can we use the common function to get the active load?
         # For now we leave it as it is to retain backwards compatibility, we don't have such a setup to test anyway...
         if cat /etc/fstab | grep "/var" | grep -v "^/dev/${diskdev}" | grep -q 'primary'
         then
            primproduct="$(cat /etc/hiq8000/node.cfg| grep 'product_type' | awk -F':' '{ print $2 }' | sed -e 's/ //g')"
            secproduct="$(cat  /img/rootimg/etc/hiq8000/node.cfg| grep 'product_type' | awk -F':' '{ print $2 }' | sed -e 's/ //g')"
            primbuild="$(cat   /etc/hiq8000/node.cfg| grep 'srx_build_id' | awk -F':' '{ print $2 }' | sed -e 's/ //g')"
            secbuild="$(cat    /img/rootimg/etc/hiq8000/node.cfg| grep 'srx_build_id' | awk -F':' '{ print $2 }' | sed -e 's/ //g')"
         else
            primproduct="$(cat /img/rootimg/etc/hiq8000/node.cfg| grep 'product_type' | awk -F':' '{ print $2 }' | sed -e 's/ //g')"
            secproduct="$(cat  /etc/hiq8000/node.cfg| grep 'product_type' | awk -F':' '{ print $2 }' | sed -e 's/ //g')"
            primbuild="$(cat   /img/rootimg/etc/hiq8000/node.cfg| grep 'srx_build_id' | awk -F':' '{ print $2 }' | sed -e 's/ //g')"
            secbuild="$(cat    /etc/hiq8000/node.cfg| grep 'srx_build_id' | awk -F':' '{ print $2 }' | sed -e 's/ //g')"
         fi
         primproduct=${primproduct:-None}
         secproduct=${secproduct:-None}
         primbuild=${primbuild:-None}
         secbuild=${secbuild:-None}

         primloadversion="${primproduct}${component}-${primbuild}"
         secloadversion="${secproduct}${component}-${secbuild}"
      else
         echo "${_SELF_}: Failed to detect product in node.cfg."
         exit ${CannotReadCfgValue}
      fi

   fi # if [ "X${product}X" = "XHiPath8000X" ]

}

# -------------------------------------------------------
# function:  complete
# args    :  none
# description:
#    sets the % complete in 
# -------------------------------------------------------
complete( ) {
   lockfile -s 1 /lock/activate8k.usg
   echo $1 > /lock/activate8k.dmap
   rm -f /lock/activate8k.usg
}

# -------------------------------------------------------
# function: activate
# args    :  none
# description:
#   Makes sure that the versions are the same after sync.
# -------------------------------------------------------
activate( ) {
   #<AcquireLock>
      lockfile -l 30  /lock/activate8k.usg
      echo "${_SELF_}: Acquired lock."
      rm -f /lock/activate8k.usg
   #</AcquireLock>

   complete 0

   if [ "X${FORCEACTIVATE}X" = "XfalseX" ]
   then
      # Check if sync8k is already running
      local sync8k_pids=$(pgrep -x ${sync8k_script_name})

      # If there is a sync8k running and it is not our parent process, abort...
      if [ "X${sync8k_pids}X" != "XX" ]
      then
         # Check all the list of running sync8k commands and abort if something dangerous is found
         local sync8k_safe_patterns="\-auto|\-progress|\-status|\-live|\-manual|\-v|\-q[ \t]*$|\-h"
         for pid in ${sync8k_pids}
         do
             logdebug "Checking ${sync8k_script_name} command [$(ps -o pid=,args= ${pid})]"

             # If this pid is our process parent, continue all OK
             [ "X${sync8k_pid}X" != "X${PPID}X" ] && continue
             
             # if the options in the sync8k command are safe, continue
             if echo "$(ps -o pid=,args= ${pid})" | egrep -q "${sync8k_safe_patterns}"
             then
                  continue
             fi

             # We found a sync8k that copies image loads, we should stop right here
             echo "Looks like an instance of ${sync8k_script_name} is running, process ID(s) [${sync8k_script_name}]."
             ps -ef f | grep ${sync8k_script_name} 1>/log/activate8k.auto 2>&1
             exit ${Sync8kAlreadyRunning}
         done
      fi

      # Check if _SELF_ is already running
      local my_pids_file="/tmp/${_SELF_}${MYPID}"
      pgrep -x "${_SELF_}" | grep -v "${MYPID}" >"${my_pids_file}"
      local activate8k_running_pid="$(cat ${my_pids_file})"
      logdebug "${_SELF_} process ids [${activate8k_running_pid}], my process ID[${MYPID}]"
      if [ "X${RESET}X" = "XfalseX" -a "X${QUICKRESET8k}X" = "XfalseX" ]
      then
         if [ "X${activate8k_running_pid}X" != "XX" ]
         then
            echo "Looks like an instance of ${_SELF_} is already running."
            ps -ef f | grep ${_SELF_} 1>/log/activate8k.auto 2>&1
            exit $Activate8kAlreadyRunning
         fi
      fi
      rm -f "${my_pids_file}"
   fi

   # Gather and print a detailed view of image loads's versions
   getloadinfo

   complete 10

   #<ValidateCfType>
      cftype="$(cfgread cf_type)"
      if [ "X${cftype}X" = "X3X" ]
      then
         cipselector="#"
      else
         cipselector=""
      fi
   #</ValidateCfType>
   echo ""
   echo "Detected hard disk device [${diskdev}]."
   echo "////////////////////////////////////////////////////////////"
   echo "------------------------------------------------------------"
   echo "Detected Versions: "
   if [ "X${syncpart}X" = "XprimaryX" ]
   then
      if [ "X${RESET}X" != "XtrueX" ]
      then
         echo "   Active   Version:$syncpart: ${primloadversion}/${primpatchversion}"
         echo "   Fallback Version:$syncother: ${secloadversion}/${secpatchversion}"
      else
         echo "   Active   Version:$syncother: ${secloadversion}/${secpatchversion}"
         echo "   Fallback Version:$syncpart: ${primloadversion}/${primpatchversion}"
      fi
      activeloadopt="Linux-Active-$syncother-$secloadversion-$secpatchversion"
      fallbackloadopt="Linux-Fallback-$syncpart-${primloadversion}-${primpatchversion}"
   else
      if [ "X${RESET}X" != "XtrueX" ]
      then
         echo "   Active   Version:$syncpart: ${secloadversion}/${secpatchversion}"
         echo "   Fallback Version:$syncother: ${primloadversion}/${primpatchversion}"
      else
         echo "   Active   Version:$syncother: ${primloadversion}/${primpatchversion}"
         echo "   Fallback Version:$syncpart: ${secloadversion}/${secpatchversion}"
      fi
      activeloadopt="Linux-Active-$syncother-${primloadversion}-${primpatchversion}"
      fallbackloadopt="Linux-Fallback-$syncpart-$secloadversion-$secpatchversion"
   fi
   echo "------------------------------------------------------------"
   echo "////////////////////////////////////////////////////////////"

   complete 20

   if [ "X${syncpart}X" = "XprimaryX" -a "X${secloadversion}X" = "XNonex" ]
   then
       echo "Aborting activate. No fallback available."
       syncnexit $NoFallbackAvailable
   fi
   if [ "X${syncpart}X" = "XsecondaryX" -a "X${primloadversion}X" = "XNoneX" ]
   then
       echo "Aborting activate. No fallback available."
       syncnexit $NoFallbackAvailable
   fi
   if [ "X${QUICKRESET8k}X" = "XfalseX" ]
   then
      echo "Note: Typing yes to the question below activates the fallback"
      echo "    : $activeloadopt"
      echo -n "Do you want to continue (yes/no)?"
      read answer
      if [ "X${answer}X" != "XyesX" ]
      then
         echo "O.k. Activate aborted."
         syncnexit $UserAbortActivate
      fi
   fi

   complete 40

   echo -n "[*] Setting up the boot options : "
   # Update the boot loader
   ${grubinstaller} "${syncother}" "/dev/${diskdev}" ${root_prim_part} ${root_sec_part} ${optimg_part} \
      'none' '' '/dev/sda' 1>>$LOGFILE 2>&1 || syncnexit ${GrubInstFailure}

   # Sync the files written by the boot loader setup, we noticed that essential GRUB modules
   # may be missing on the next boot up, probably due to the reboot of the nodes, without a clean
   # umount.
   sync

   #<SetSrxBootParms>
      # The load to be activated should boot by default to 2 0
      if [ "X${product}X" = "XHiPath8000X" ]
      then
         echo "2 0 /log/startup_srx.log /log/startup_rtp.log" > \
                 /img/unisphereimg/${syncother}/srx3000/srx/startup/srxBootParams
      fi
   #</SetSrxBootParms>

   #<SetCipCf>
      if [ -f /img/rootimg/etc/cip.cf -a -f /img/rootimg/etc/hiq8000/node.cfg ]
      then

         passivenodea="$(cat /img/rootimg/etc/hiq8000/node.cfg| grep 'node_1_name' \
                                                | awk -F':' '{ print $2 }' | sed -e 's/[ \t]*//g')"
         passivenodeb="$(cat /img/rootimg/etc/hiq8000/node.cfg| grep 'node_2_name' \
                                                | awk -F':' '{ print $2 }' | sed -e 's/[ \t]*//g')"
         cat << CIPEOF >/img/rootimg/etc/cip.cf 
${cipselector}${passivenodea} ${passivenodea}_cip0:netmask:255.255.255.255
${cipselector}${passivenodeb} ${passivenodeb}_cip0:netmask:255.255.255.255
CIPEOF

      fi
   #</SetCipCf>
   if [ -d /img/rootimg/lock ]
   then
      touch /img/rootimg/lock/activate8k.lock
      {
          echo "Last ${_SELF_} : $(date -u)"
          if [ "X${RESET}X" != "XtrueX" ]
          then
             echo "Note            : ${syncpart} was active during activate8k."
          else
             echo "Note            : ${syncother} was active during activate8k."
          fi
      }  > ${IMGDIR}/lock/activate8k.lock 
   fi

   complete 80

   echo "ok"

   if [ "X${RESET}X" != "XtrueX" ]
   then
      echo "////////////////////////////////////////////////////////////////"
      echo "----------------------------------------------------------------"
      echo "|Note:                                                          |"
      echo "|   Your fallback image will be loaded on the next boot.        |"
      echo "|   Remember to run ${_SELF_} -done after next reboot.         |"
      echo "////////////////////////////////////////////////////////////////"
      echo "----------------------------------------------------------------"
      sync

      if [ -f /lock/activate8k.aulock ]
      then
         rm -f /lock/activate8k.aulock

         # We cannot use ${_SELFDIR_}/activate8k.initservice safely here since UNSP migration Toolkit
         # delivers /usr/bin/activate8k but NOT the /usr/bin/activate8k.initservice
         local activate_init_service="${SRXBIN}/activate8k.initservice"

         if [ -f ${activate_init_service} ]
         then
             # We activate the service appropriatelly depending on OS type (SLES 11, 12, etc.)

             # <If_SystemV>
             # SLES 10, 11
             if grep -qi 'Version = 11\|Version = 10' /img/rootimg/etc/SuSE-release
             then

                # activate auto service
                cp ${activate_init_service} /img/rootimg/etc/init.d/activate
                pushd /img/rootimg/etc/init.d/rc3.d
                ln -s ../activate S20activate
                popd

                # Chroot and run the insserv to create the /etc/init.d/rc*.d symbolic links
                if ! chroot /img/rootimg insserv 1>>/log/activate8k.auto 2>&1
                then
                    echo "${_SELF_}: Service activation was not successfull."
                    exit $ActivateInitNotSuccessfull
                fi

                #<DeActivateSymphonia>
                echo "${_SELF_}: Deactivating service for Symphonia applications"
                if [ -f /img/rootimg/etc/init.d/symphoniad ]
                then
                    chroot /img/rootimg insserv -r /etc/init.d/symphoniad
                    rm -f  /img/rootimg/etc/init.d/rc3.d/*symphoniad* /img/rootimg/etc/init.d/rc5.d/*symphoniad*
                fi
                #</DeActivateSymphonia>

             fi
             # <If_SystemV>

             #<If_systemd>
             # SLES 12 systemd and above
             if [ -d /img/rootimg/usr/local/lib/systemd/system ]
             then
                 # Create a systemd unit file that will invoke the activation init script.
                 # Use the first Linux virtual terminal /dev/tty1 for standard output to allow
                 # the user to see the difference during boot up.
                 local activate_service_unit='/usr/local/lib/systemd/system/activate.service'
                 cat << SYSD >/img/rootimg/${activate_service_unit}
[Unit]
Description=activate service for OSV Image Installations
Before=getty@tty1.service
Requires=bz.target
After=bz.target srx_security.service srx_startup.service swo_startup.service spt_startup.service db_control.service

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=${activate_init_service} start 2>&1
StandardOutput=tty
StandardError=tty
TTYPath=/dev/tty1

[Install]
WantedBy=osv.target

SYSD

                 # Now that the service is created make it run at osv.target.wants
                 # Change root to the other load's rootfs and apply the symbolic link
                 # Delete first a stale link to recreate it from fresh
                 local activate_service_link='/etc/systemd/system/osv.target.wants/activate.service'
                 [ -L /img/rootimg/${activate_service_link} ] && rm -f /img/rootimg/${activate_service_link}
                 if ! chroot /img/rootimg/ ln -s ${activate_service_unit} ${activate_service_link} 1>/dev/null 2>&1
                 then
                     echo "${_SELF_}: Service activation was not successfull."
                     exit $ActivateInitNotSuccessfull
                 fi

                 #<DeActivateSymphonia>
                 echo "${_SELF_}: Deactivating service for Symphonia applications"
                 local systemd_dir="/img/rootimg/etc/systemd/system/"
                 for symlink in $(find ${systemd_dir} -type l -name "symphoniad.service" | xargs)
                 do
                     echo "Removing symbolic link ${symlink}"
                     rm -f ${symlink}
                 done
                 #</DeActivateSymphonia>
             fi
           #<If_systemd>

         else # [ -f ${activate_init_service} ]

            echo "Fatal Error: the activation boot service script was not found!!"
            exit $ActivateInitNotFound

         fi # [ -f ${_SELFDIR_}/activate8k.initservice ]

         echo "${_SELF_}: Service successfully activated on ${mynode}. Rebooting the nodes."

      fi # [ -f /lock/activate8k.aulock ]

      echo "Activate: Success."
      sync

   else # [ "$RESET" != "true" ]

      if [ "X${product}X" = "XHiPath8000X" ]
      then
         cat ${SRXBOOT} | sed -e 's/^[ \t]*2[ \t]*2/4 4/g' \
                              -e 's/^[ \t]*2[ \t]*0/4 4/g' \
                        > ${SRXBOOT}.new
         cp  ${SRXBOOT}.new ${SRXBOOT}
         rm -f ${SRXBOOT}.new
         sync
         echo "Reset: Success."
      fi

   fi # [ "$RESET" != "true" ]

   complete 100

   # Hack the multi-user target dependencies - ref. OSV-7321 OSV-9785 OSV-4138
   for i in /img/rootimg/usr/lib/systemd/system/multi-user.target.wants/after-local.service \
            /img/rootimg/usr/lib/systemd/system/multi-user.target.wants/getty.target;
   do
      [ -f "$i" ] && echo "* multi-user target dependency hack: remove ${i}" && rm -f ${i}
   done

   return ${Success}
}

# -------------------------------------------------------
# function: stopapps
# args    :  none
# description:
#    stop all the applications before bring system up.
# -------------------------------------------------------
stopapps( ) {
   if [ -f /etc/init.d/symphoniad ]
   then
      echo "Stopping applications now."

      # <If_SystemV>
      if grep -qi 'Version = 11\|Version = 10' /img/rootimg/etc/SuSE-release
      then
          /etc/init.d/symphoniad stop 1>>/log/activate8k.auto 2>&1
      fi
      # </If_SystemV>

      # <If_systemd>
      if [ -d /usr/local/lib/systemd/system ]
      then
          systemctl stop symphoniad.service 1>>/log/activate8k.auto 2>&1
      fi
      # </If_systemd>

   fi
}

# -------------------------------------------------------
# function: startapps
# args    :  none
# description:
#    startapps all the applications before bring system up.
# -------------------------------------------------------
startapps( ) {
   if [ -f /etc/init.d/symphoniad ]
   then
      echo "Starting applications now."

      # <If_SystemV>
      if grep -qi 'Version = 11\|Version = 10' /img/rootimg/etc/SuSE-release
      then
          /etc/init.d/symphoniad start 1>>/log/activate8k.auto 2>&1
      fi
      # </If_SystemV>

      # <If_systemd>
      if [ -d /usr/local/lib/systemd/system ]
      then
          systemctl start symphoniad.service 1>>/log/activate8k.auto 2>&1
      fi
      # </If_systemd>

   fi
}

# -------------------------------------------------------
# function: initvars
# args    :  none
# description:
#   Initializes vars and return codes used by this script.
# -------------------------------------------------------
initvars( ) {
   RESET="false"
   FINISHACTIVATION=false
   QUICKRESET8k=${QUICKRESET8k:-"false"}
   FORCEACTIVATE=false;
   LIVE8kUPGRADE=false
   MANUAL=false

   product=$(cfgread product_type)
   component=$(/sbin/cfgread component_type)

   if [ "X${product}X" = "XX" ]
   then
      if rpm -qi UNSPbase 1>/dev/null 2>&1
      then
         product="HiPath8000"
      else
         echo "${_SELF_}: Failed to detect product in node.cfg."
         exit ${CannotReadCfgValue}
      fi
   fi
   #<CheckSupportedTypes>
   if [ "X${product}X" != "XHiPath8000X" -a "X${product}X" != "XOpenScapeX" ]
   then
      echo "${sync8k_script_name}: Failed to detect supported product in node.cfg."
      exit ${CannotReadCfgValue}
   fi
   #</CheckSupportedTypes>

   mkdir -p /lock

   #<CheckHwAbstraction>
   # Detect disk type
   if ! get_block_device_of_rootfs
   then
       logerror "Cannot detect the hard disk device of this system."
       exit ${InvalidDiskType}
   fi
   diskdev="${_OSVROOTFSBLKDEV_}"

   # Check if the system supports dual OSV loads, primary/secondary
   if ! disk_has_dual_osv_loads "${diskdev}"
   then
       echo "Detected non imaged disk."
       exit ${Success}
   fi

   if [[ ${diskdev} == sd[a-z] ]]
   then
       disktype=scsi
   elif [[ ${diskdev} == hd[a-z] ]]
   then
       disktype=ide
   else
       logerror "[*] Block device [${diskdev}] has unknown disk type."
       exit $InvalidDiskType
   fi
   #</CheckHwAbstraction>

   #<ACPIvsNone>
       hwplatform="$(cfgread hw_platform)"
       case "$hwplatform" in
          'RX200'|'x3550M3'|'RX200S7'|'x3550M4')
             ACPIOPTS=""
             ;;
          *)
             ACPIOPTS="acpi=ht"
             ;;
       esac
   #</ACPIvsNone>

   #<Linux32vs64>
   archmatch=true
   if mount | egrep -q "udev|devtmpfs"
   then
      ARCH="x86_64"
      osopts="vga=0x314 ${ACPIOPTS} resume=/dev/${diskdev}1 splash=silent showopts"
      failsafeopts="vga=normal showopts ide=nodma apm=off acpi=off noresume edd=off 3"
      if [ ! -d /img/rootimg/lib/udev ]
      then
         fbopts="selinux=0 splash=silent ide=nodma apm=off ${ACPIOPTS} barrier=off hwprobe=+cpuemu resume=/dev/${diskdev}1 elevator=cfq showopts"
         fbfailsafeopts="showopts ide=nodma apm=off acpi=off vga=normal noresume selinux=0 barrier=off nosmp noapic maxcpus=0 3"
         archmatch=false
      fi
   else
      ARCH=""
      osopts="selinux=0 splash=silent ide=nodma apm=off ${ACPIOPTS} barrier=off hwprobe=+cpuemu resume=/dev/${diskdev}1 elevator=cfq showopts"
      failsafeopts="showopts ide=nodma apm=off acpi=off vga=normal noresume selinux=0 barrier=off nosmp noapic maxcpus=0 3"
      if [ -d /img/rootimg/lib/udev ]
      then
         archmatch=false
         fbopts="vga=0x314 ${ACPIOPTS} resume=/dev/${diskdev}1 splash=silent showopts"
         fbfailsafeopts="vga=normal showopts ide=nodma apm=off acpi=off noresume edd=off 3"
      fi
   fi
   #</Linux32vs64>

   #<CheckCfg>
      testbed="$(cfgread test_bed)"
      primhost="$(cfgread node_1_name)"
      sechost="$(cfgread node_2_name)"
      mynode="$(uname -n)"
   #</CheckCfg>

   LiveUpdate8k=false
}

# -------------------------------------------------------
# function: show_banner
# args: none
# description:
#	Show a startup banner with basic info that will help
#	detect script startups in the log file.
# -------------------------------------------------------
show_banner( ) {
    log "################################################"
    loginfo "Starting image load activation script in ${mynode}"
    log "################################################"
}

# -------------------------------------------------------
# function: check
# args: None
# description:
#	Check if the currently booted image load requires activation.
# -------------------------------------------------------
check( ) {

   # This function does not only display stuff, it also initializes
   # a ton of current system's state variables
   display

   echo -n "Checking if the current image needs complete activation: "
   if [ "X${product}X" = "XHiPath8000X" ]
   then
      grep -q "netmask:255.255.255.255" /etc/cip.cf 1>/dev/null 2>&1
      nmstatus=$?
      egrep -q "^4[ \t]*4" /unisphere/srx3000/srx/startup/srxBootParams
      bpstatus=$?
      if [ -f /lock/activate8k.lock -o $nmstatus -eq 0 -o $bpstatus -ne 0 ]
      then
         echo "yes"
      else
         echo "no"
         syncnexit $Success
      fi
   elif [ "X${product}X" = "XOpenScapeX" ]
   then
       if [ -f /lock/activate8k.lock ]
       then
           echo "yes"
       else 
           echo "no"
           syncnexit $Success
       fi
   else
       echo "no"
       syncnexit $Success
   fi

   QuietActivate8k=false

}

# -------------------------------------------------------
# function: checksrxctrl 
# args    :  none
# description:
#   Checks if srxctrl is already running.Waits till its done.
# -------------------------------------------------------
checksrxctrl( ) {
   testbed="$(cfgread test_bed)"
   primhost="$(cfgread node_1_name)"
   sechost="$(cfgread node_2_name)"
   mynode="$(uname -n)"

   if [ "X${testbed}X" = "XclusterX" ]
   then
      echo "${_SELF_}: Waiting for any instance of srxctrl to terminate on $sechost."
      while ( true )
      do
         if [ "X${mynode}X" = "X${primhost}X" ]
         then
            pidsrxctrl="$(rssh -o ConnectTimeout=16 $sechost  'pgrep -x srxctrl' 2>/dev/null | sed -e 's/[ \t]*//g')" 
         else
            pidsrxctrl="$(rssh -o ConnectTimeout=16 $primhost 'pgrep -x srxctrl' 2>/dev/null | sed -e 's/[ \t]*//g')" 
         fi

         if [ "X${pidsrxctrl}X" = "XX" ]
         then
            break
         fi

         sleep 8
      done
      echo "${_SELF_}: No instance of srxctrl running on partner."
   fi

   echo "${_SELF_}: Waiting for any instance of srxctrl to terminate on $mynode."

   while ( true )
   do
      pidsrxctrl="$(pgrep -x 'srxctrl' 2>/dev/null | sed -e 's/[ \t]*//g')" 
      if [ "X${pidsrxctrl}X" = "XX" ]
      then
         break
      fi
      sleep 8
   done

   echo "${_SELF_}: No instance of srxctrl running on local."
}

# -------------------------------------------------------
# function: finishactivate
# args    :  none
# description:
#   called on reboot after activation, checks images and
#   bootup only if the versions match.
# -------------------------------------------------------
finishactivate( ) {

   #<GetNodeCfgParms>
       testbed="$(cfgread test_bed)"
       primnode="$(cfgread node_1_name)"
       secnode="$(cfgread node_2_name)"
       primnodecip="$(cfgread node_1_cip_logical)"
       secnodecip="$(cfgread node_2_cip_logical)"
       cftype="$(cfgread cf_type)"
       cipnetmask="$(cfgread cip_netmask)"
       cip2netmask="$(cfgread cip2_netmask)"
   #</GetNodeCfgParms>

   #<SetSrxBootParms>
      if [ "X${product}X" = "XHiPath8000X" ]
      then
         echo "2 0 /log/startup_srx.log /log/startup_rtp.log" > \
                 /img/unisphereimg/${syncother}/srx3000/srx/startup/srxBootParams

         # Properly re-enable Symphonia application, according to init system (SystemV or SystemD)
         # <If_SystemV>
         if grep -qi 'Version = 11\|Version = 10' /img/rootimg/etc/SuSE-release
         then
             if [ -f /etc/init.d/symphoniad ]
             then
                 log "Re-enabling the Symphonia boot service"
                 insserv /etc/init.d/symphoniad 1>>/log/activate8k.auto 2>&1
             else
                 log "No Symphonia boot service in this system..."
             fi
         fi
         # </If_SystemV>

         # <If_systemd>
         if [ -d /usr/local/lib/systemd/system ]
         then
             if ! systemctl status symphoniad.service | grep -q 'Loaded:.*not-found'
             then
                 log "Re-enabling the Symphonia boot service"
                 systemctl enable symphoniad.service 1>>/log/activate8k.auto 2>&1
                 systemctl daemon-reload 1>>/log/activate8k.auto 2>&1
             else
                 log "No Symphonia boot service in this system..."
             fi
         fi
         # </If_systemd>
      fi
   #</SetSrxBootParms>

   #<ValidateCfType>
      if [ "X${cftype}X" = "X3X" ]
      then
         cipselector="#"
         ifconfig cip0 0.0.0.0 down 2>/dev/null
      else
         cipselector=""
      fi
   #</ValidateCfType>

   #<SetCipCf>
      if [ -f /img/rootimg/etc/cip.cf -a -f /img/rootimg/etc/hiq8000/node.cfg ]
      then

         passivenodea="$(cat /img/rootimg/etc/hiq8000/node.cfg| grep 'node_1_name' \
                                                | awk -F':' '{ print $2 }' | sed -e 's/[ \t]*//g')"
         passivenodeb="$(cat /img/rootimg/etc/hiq8000/node.cfg| grep 'node_2_name' \
                                                | awk -F':' '{ print $2 }' | sed -e 's/[ \t]*//g')"
         if [ "X${testbed}X" = "XclusterX" ]
         then
             cat << CIPEOF >/img/rootimg/etc/cip.cf 
${cipselector}${passivenodea} ${passivenodea}_cip0:netmask:255.255.255.255
${cipselector}${passivenodeb} ${passivenodeb}_cip0:netmask:255.255.255.255
CIPEOF
         else
             cat << CIPEOF >/img/rootimg/etc/cip.cf 
${cipselector}${passivenodea} ${passivenodea}_cip0:netmask:255.255.255.255
CIPEOF
         fi

      fi
   #</SetCipCf>

    #<SelectNmForL3>
       if [ "X${cftype}X" != "X3X" ]
       then
         primnodecipnm=$cipnetmask
         secnodecipnm=$cipnetmask
       else
         primnodecipnm=$cipnetmask
         secnodecipnm=$cip2netmask
       fi
    #</SelectNmForL3>
    mynode="$(uname -n)"
    if [ "X${mynode}X" != "X${primnode}X" -a "X${mynode}X" != "X${secnode}X" ]
    then
       echo "Sorry, your system does not reflect the node.cfg settings."
       echo "Check node names ..."
       syncnexit $InvalidNodeNames
    fi
    if [ "X${mynode}X" = "X${primnode}X" ]
    then
       mynodecipnm=$primnodecipnm
       mynodecip=$primnodecip
       othernode=$secnode
    else
       mynodecipnm=$secnodecipnm
       mynodecip=$secnodecip
       othernode=$primnode
    fi

    if [ "X${testbed}X" = "XclusterX" ]
    then
        ###########################################################################
        #<ClusterDuplex>

        # Shut down the cluster interconnect in case of 
        if [ "X${cftype}X" = "X2X" ]
        then
            echo -n "Closing cluster interconnect on node $(uname -n) : "
            # Is this ifconfig safe for the future?
            if ifconfig cip0 down 1>/dev/null 2>&1
            then
                echo "ok"
            else
                echo "failed"
                syncnexit $ClusterIcPlumbFailed
            fi
        fi

        # Get currently loaded system's build ID
        echo -n "Checking version on current node : ${mynode} : "
        local myloadversion="None"
        if get_load_buildid 'current'
        then
            myloadversion="${_LOADVERSION_}"
        fi
        # Get currently loaded system's patchset
        local mypatchversion="None"
        if get_load_fullpsversion 'current'
        then
            mypatchversion="$(echo ${_LOADFULLPSVERSION_} | awk -F'-' '{ print $1 }')"
        fi
        myversion="${myloadversion}/${mypatchversion}"
        echo ${myversion}

        # Get load info of the partner node
        echo -n "Checking reachability of remote node : ${othernode} : "
        local othernodereachable=''
        rssh $othernode -o ConnectTimeout=16 "ifconfig -a" 2>/dev/null 1>/tmp/${othernode}.ifcfg; othernodereachable=$?
        if [ ${othernodereachable} -eq 0 ]
        then
            echo "ok"

            # Ask the _SELF_ of the other side for the active image, it will use the common code to detect it
            local check_partner=$(rssh $othernode -o ConnectTimeout=16 "${_SELFDIR_}/${_SELF_} -check" 2>/dev/null | grep '^Located the active image')
            local othersyncpart=''
            if echo ${check_partner} | grep -q 'primary'
            then
                othersyncpart="primary"
            elif echo ${check_partner} | grep -q 'secondary'
            then
                othersyncpart="secondary"
            else
                logwarning "Cannot detect active image load of partner node [${othernode}]"
                othersyncpart=''
            fi

            echo -n "Checking version on remote node : ${othernode} : "

            if [ "X${LIVE8kUPGRADE}X" = "XfalseX" ]
            then

                # Ask the sync8k of the other side for the load versions, it will use the common code to detect them
                local partner_fullpsversion=$(rssh $othernode -o ConnectTimeout=16 "${sync8k_path} -v" 2>/dev/null | grep "${othersyncpart:-NOTSET}" | awk -F'/' '{ print $2 }')
                local otherloadversion="$(echo ${partner_fullpsversion} | awk -F'-' '{ print $2 }')"
                local otherpatchversion="$(echo ${partner_fullpsversion} | awk -F'-' '{ print $1 }')"
                otherloadversion=${otherloadversion:-None}
                otherpatchversion=${otherpatchversion:-None}

            else # if [ "X${LIVE8kUPGRADE}X" = "XfalseX" ]

                # In case of live upgrade, we assume that we have the same version
                otherloadversion=$myloadversion
                otherpatchversion=$mypatchversion
                othersyncpart=$syncpart

            fi # if [ "X${LIVE8kUPGRADE}X" = "XfalseX" ]

            otherversion="$otherloadversion/$otherpatchversion"
            echo ${otherversion}

            # If there is a mismatch of active loads or version between the nodes, we drop the partner node to state 2
            if [ "X${myversion}X" != "X${otherversion}X" -o "X${syncpart}X" != "X${othersyncpart}X" ]
            then

                echo "Shutting down remote node : $othernode : ..."

                checksrxctrl # Wait to settle...
                rssh $othernode "/unisphere/srx3000/srx/startup/srxctrl 2 nc" # Drop othernode to state 2
                stopapps
                checksrxctrl # Wait to settle...
                /unisphere/srx3000/srx/startup/srxctrl 4 nc # Set this node to state 4
                startapps

                echo "////////////////////////////////////////////////////////////////"
                echo "----------------------------------------------------------------"
                echo "|Note:                                                          |"
                [ "X${myversion}X" != "X${otherversion}X" ] && echo "|   Version differ. Cluster interconnect will not be open.      |"
                if [ "X${syncpart}X" != "X${othersyncpart}X" ]
                then
                    echo "|   Image runs on different partitions.                         |"
                    echo "|      - $mynode : $syncpart.                                    "
                    echo "|      - $othernode : $othersyncpart.                            "
                    echo "|   Cluster Interconnect will not be open.Remote node shutdown. |"
                fi
                echo "|   The system will be up without these interconnects.          |"
                echo "|   Rerun ${_SELF_} -done once both nodes are at same level.   |"
                echo "|                                                               |"
                echo "----------------------------------------------------------------"
                echo "////////////////////////////////////////////////////////////////"

            else # if [ "$myversion" != "$otherversion" -o "$syncpart" != "$othersyncpart" ]

                cat << CIPEOF >/etc/cip.cf 
${cipselector}${primnode} ${primnode}_cip0:netmask:${primnodecipnm}
${cipselector}${secnode} ${secnode}_cip0:netmask:${secnodecipnm}
CIPEOF

                #<ValidateCfType>
                if [ "X${cftype}X" != "X3X" ]
                then
                    ifconfig cip0 $mynodecip netmask $mynodecipnm up
                fi
                #</ValidateCfType>

                echo "////////////////////////////////////////////////////////////////"
                echo "----------------------------------------------------------------"
                echo "|Note:                                                          |"
                echo "|    Resetting current load to be active load.                  |"
                echo "////////////////////////////////////////////////////////////////"
                echo "----------------------------------------------------------------"
                export QUICKRESET8k="true"
                ${_SELFDIR_}/${_SELF_} -reset
                stopapps
                checksrxctrl # Wait to settle...
                /unisphere/srx3000/srx/startup/srxctrl 4 nc
                startapps
                rm -f /lock/activate8k.lock
                echo "////////////////////////////////////////////////////////////////"
                echo "----------------------------------------------------------------"
                echo "|Note:                                                          |"
                echo "|   Version match. Cluster interconnect is open.                |"
                echo "|   The system will be up with these interconnects.             |"
                echo "----------------------------------------------------------------"
                echo "////////////////////////////////////////////////////////////////"
                echo "Activate: Success."

            fi # if [ "$myversion" != "$otherversion" -o "$syncpart" != "$othersyncpart" ]

        else # if [ ${othernodereachable} -eq 0 ]

            echo "unreachable"
            stopapps
            checksrxctrl # Wait to settle...
            /unisphere/srx3000/srx/startup/srxctrl 4 nc
            startapps
            echo "/////////////////////////////////////////////////////////////////////////////"
            echo "-----------------------------------------------------------------------------"
            echo "|Note:                                                                      |"
            echo "|   Remote node is not reachable. Hence Cluster interconnect cannot be open.|"
            echo "|   The system will be up without these interconnects.                      |"
            echo "|   Rerun ${_SELF_} -done once both nodes are at same load and reachable.  |"
            echo "-----------------------------------------------------------------------------"
            echo "/////////////////////////////////////////////////////////////////////////////"

        fi # if [ ${othernodereachable} -eq 0 ]

        #</ClusterDuplex>
        ###########################################################################

    else # if [ "$testbed" = "cluster" ]

        ###########################################################################
        #<IntegratedSimplex>
             echo "////////////////////////////////////////////////////////////////"
             echo "----------------------------------------------------------------"
             echo "|Note:                                                          |"
             echo "|    Resetting current load to be active load.                  |"
             echo "////////////////////////////////////////////////////////////////"
             echo "----------------------------------------------------------------"
             export QUICKRESET8k="true"
             ${_SELFDIR_}/${_SELF_} -reset
             stopapps
             checksrxctrl
             /unisphere/srx3000/srx/startup/srxctrl 4 nc
             startapps
             rm -f /lock/activate8k.lock
             if [ -f /etc/cip.cf ]
             then
          cat << CIPEOF >/etc/cip.cf
${primnode} ${primnode}_cip0:netmask:${primnodecipnm}
CIPEOF
             fi
             echo "////////////////////////////////////////////////////////////////"
             echo "----------------------------------------------------------------"
             echo "|Note:                                                          |"
             echo "|   Load activated for simplex.                                 |"
             echo "----------------------------------------------------------------"
             echo "////////////////////////////////////////////////////////////////"
             echo "Activate: Success."
        #</IntegratedSimplex>
        ###########################################################################

    fi # if [ "$testbed" = "cluster" ]

    sync
    #</ActivateOnBoot>
}

# ----------------------------------------------------------------
# function: cleanrepo
# args    : none
# description:
#   cleanup remote upgrade repo and set it back to upload status.
# ----------------------------------------------------------------
cleanrepo( ) {
   local REPOSITORY=/repository
   local activeinfo="$(${sync8k_path} -q none)"
   local activenotrail=${activeinfo%,*}
   local activeimg=${activenotrail#*:}
   local passiveimg=${activeinfo#*Fallback:}
   local activemode="$(cat /img/logimg/${activeimg}/upgrade.lock 2>/dev/null | head -1)"
   local passivemode="$(cat /img/logimg/${passiveimg}/upgrade.lock 2>/dev/null | head -1)"

   if [ "X${activemode}X" = "XOutageFreeX" ]
   then
       rm -f /img/logimg/${activeimg}/upgrade.lock
   fi

   if [ "X${passivemode}X" = "XOutageFreeX" ]
   then
       rm -f /img/logimg/${passiveimg}/upgrade.lock
   fi

   rm -f /lock/activate8k.aulock

   # The Symphonia Framework user and group should be added only if the
   # system contains the Symphonia applications, e.g. Integrated Simplex or OSEE version<V9
   # It causes the following RapidStat warnings:
   # WARNING: User name(s) 'sym' appear multiple times in /etc/passwd file
   # WARNING: Group name(s)'sym' appear multiple times in /etc/group
   # WARNING: The default account 'sym' does not exist
   # TODO: change the conditional to work only for system with applications installed
   if ! cat /etc/group | grep -q "sym:"
   then
      echo "sym:x:102:113:Symphonia Framework:/enterprise:/bin/false" >> /etc/passwd
      echo "sym:!:113:" >> /etc/group
   fi

   mkdir -p ${REPOSITORY}/upload
   chown sym:rtpgrp ${REPOSITORY}/upload
   chmod g+w     ${REPOSITORY}/upload

   for idx in $(find ${REPOSITORY}/upload -name \*.zip)
   do
      basezip=$(basename $idx .zip)
      rm -f ${REPOSITORY}/media/patch/${basezip}.*
   done

   local list_of_repofiles=$(ls \
${REPOSITORY}/media/node.cfg.* \
${REPOSITORY}/media/response.cfg.* \
${REPOSITORY}/media/patch/* \
${REPOSITORY}/media/iso/* \
${REPOSITORY}/media/*.lic \
${REPOSITORY}/media/export/*.lic \
2>/dev/null | xargs)
   for repofile in ${list_of_repofiles}
   do
       logdebug "Trying to move the file [${repofile}] from ${REPOSITORY}/media to ${REPOSITORY}/upload"

       # If the file or folder exists
       if [ -f ${repofile} -o -d ${repofile} ]
       then
           mv -f ${repofile} ${REPOSITORY}/upload/
       fi
   done

}

# -------------------------------------------------------
# function:  activatecomplete
# args    :  none
# description:
#    Initiates activation with either reboot or reset
# -------------------------------------------------------
activatecomplete( ) {
   status=0
   awcargs=$1
   testbed="$(cfgread test_bed)"
   primhost="$(cfgread node_1_name)"
   sechost="$(cfgread node_2_name)"
   mynode=$(uname -n)

   case "$awcargs" in

      'reset')
         if [ "X${QUICKRESET8k}X" = "XfalseX" ]
         then
            if [ "X${testbed}X" = "XclusterX" -a "X${LIVE8kUPGRADE}X" = "XfalseX" ]
            then
               if [ "X${mynode}X" = "X${primhost}X" ]
               then
                  othernode=$sechost
                  nohup ssh $sechost "${_SELFDIR_}/${_SELF_} -quiet -reset 1>>/log/activate8k.auto 2>>/log/activate8k.auto" &
                  status=$?
               else
                  othernode=$primhost
                  nohup ssh $primhost "${_SELFDIR_}/${_SELF_} -quiet -reset 1>>/log/activate8k.auto 2>>/log/activate8k.auto" &
                  status=$?
               fi
               if [ $status -ne 0 ]
               then
                  echo "${_SELF_}:Could not initiate ${_SELF_} on $othernode."
                  exit $ActivateInitiationFailure
               fi
            fi
         fi
         export QUICKRESET8k="true"
         ${_SELFDIR_}/${_SELF_} -reset
         status=$?
         ;;

      'reboot')
         if [ "X${QUICKRESET8k}X" = "XfalseX" ]
         then
            if [ "X${testbed}X" = "XclusterX" -a "X${LIVE8kUPGRADE}X" = "XfalseX" ]
            then
               if [ "X${mynode}X" = "X${primhost}X" ]
               then
                  interface=$(cfgread nafo3 | awk '{ print $2 }')
                  othernode=$sechost
                  nohup ssh $sechost "${_SELFDIR_}/${_SELF_} -quiet -reboot 1>>/log/activate8k.auto 2>>/log/activate8k.auto" &
                  status=$?
               else
                  interface=$(cfgread nafo3 | awk '{ print $3 }')
                  othernode=$primhost
                  nohup ssh $primhost "${_SELFDIR_}/${_SELF_} -quiet -reboot 1>>/log/activate8k.auto 2>>/log/activate8k.auto" &
                  status=$?
               fi
               if [ $status -ne 0 ]
               then
                  echo "${_SELF_}: Could not initiate ${_SELF_}/${awcargs} on $othernode."
                  exit $ActivateInitiationFailure
               fi
               echo -n "${_SELF_}: Waiting for partner(${othernode}_cip0) to go down : "
               while ( true )
               do
                  if [ "X${interface}X" != "XX" ]
                  then
                     ping -I $interface -c 1 ${othernode}_cip0 1>/dev/null 2>&1
                  else
                     ping -c 1 ${othernode}_cip0 1>/dev/null 2>&1
                  fi
                  if [ $? -ne 0 ]
                  then
                     break;
                  fi
                  echo -n "."
                  sleep 2
               done
            fi
            sleep 8
         fi

         echo "Rebooting $(uname -n)"
         sync

         #<AvoidRace>
         [ "X${mynode}X" = "X${primhost}X" ] && sleep 4
         #</AvoidRace>

         # Reboot the system, avoid -f to allow for proper umount of filesystems
         reboot
         ;;

      *)
         echo "${_SELF_}: Unknown activate completion command."
         exit $ActivateInitiationFailure
         ;;

   esac

   echo "${_SELF_}: Success/${awcargs}." 1>>/log/activate8k.auto 2>>/log/activate8k.auto
   exit $status
}

# -------------------------------------------------------
# function:  cwactivate 
# args    :  none
# description:
#   runs this script on both nodes of the cluster.
# -------------------------------------------------------
cwactivate( ) {
   testbed="$(cfgread test_bed)"
   primhost="$(cfgread node_1_name)"
   sechost="$(cfgread node_2_name)"
   mynode=$(uname -n)

   touch /lock/activate8k.aulock

   if [ "X${testbed}X" = "XclusterX" ]
   then
      if [ "X${mynode}X" = "X${primhost}X" ]
      then
         rssh -n -o ConnectTimeout=16 $sechost "touch /lock/activate8k.aulock" 1>/dev/null 2>/dev/null
      else
         rssh -n -o ConnectTimeout=16 $primhost "touch /lock/activate8k.aulock" 1>/dev/null 2>/dev/null
      fi
      if [ $? -ne 0 ]
      then
         log "${_SELF_}: Could not initiate ${_SELF_} on ${mynode}."
         exit $ActivateInitiationFailure
      fi
   fi

   #<CheckRemote>
      if [ "X${FORCEACTIVATE}X" = "XtrueX" ]
      then
         force="-f"
      else
         force=""
      fi

      if [ "X${testbed}X" = "XclusterX" ]
      then
         if [ "X${LIVE8kUPGRADE}X" = "XtrueX" ]
         then
            log "${_SELF_}: Cluster detected: Will run ${_SELF_} on local node only."
         else
            log "${_SELF_}: Cluster detected: Will run ${_SELF_} on both nodes."
         fi
         ${_SELFDIR_}/${_SELF_} $force -quiet 1>>/log/activate8k.auto 2>>/log/activate8k.auto
         if [ $? -ne 0 ]
         then
            activatecomplete reset
            log "${_SELF_}: Could not initiate ${_SELF_} on ${mynode}."
            exit $ActivateInitiationFailure
         fi

         if [ "X${mynode}X" = "X${primhost}X" ]
         then
            othernode=$sechost
            rssh -n $sechost "${_SELFDIR_}/${_SELF_} ${force} -quiet 1>/log/activate8k.auto 2>/log/activate8k.auto"
            status=$?
         else
            othernode=$primhost
            rssh -n $primhost "${_SELFDIR_}/${_SELF_} ${force} -quiet 1>/log/activate8k.auto 2>/log/activate8k.auto"
            status=$?
         fi
         if [ $status -ne 0 ]
         then
            activatecomplete reset
            echo "${_SELF_}: Could not initiate ${_SELF_} on $othernode."
            exit $ActivateInitiationFailure
         fi
      else
         log "${_SELF_}: Simplex detected. Will run ${_SELF_} on current node."
         ${_SELFDIR_}/${_SELF_} $force -quiet 1>/log/activate8k.auto 2>/log/activate8k.auto
      fi
   #</CheckRemote>

   sync
   display

   if [ "X${MANUAL}X" != "XtrueX" ]
   then
      activatecomplete reboot
   fi
}

# -------------------------------------------------------
# function: displayprogress
# args    :  none
# description:
#   checks progress of activate8k.
# -------------------------------------------------------
displayprogress( ) {
   testbed="$(cfgread test_bed)"
   primhost="$(cfgread node_1_name)"
   sechost="$(cfgread node_2_name)"
   mynode=$(uname -n)
   rm -f /log/activate8k.out* 
   #<CheckRemote>
      if [ "X${testbed}X" = "XclusterX" ]
      then
         if [ "X${QUICKRESET8k}X" = "XfalseX" ]
         then
            if [ "X${mynode}X" = "X${primhost}X" ]
            then
               rssh $sechost "${_SELFDIR_}/${_SELF_} -quiet -progress" 1>/log/activate8k.out.partner 2>/dev/null
               status=$?
            else
               rssh $primhost "${_SELFDIR_}/${_SELF_} -quiet -progress" 1>/log/activate8k.out.partner 2>/dev/null
               status=$?
            fi
            if [ $status -ne 0 ]
            then
               log "${_SELF_}: Could not initiate activate8k."
               exit $ActivateInitiationFailure
            fi

         fi
      fi
   #</CheckRemote>

   lockfile -s 1 /lock/activate8k.usg
   if [ ! -f /lock/activate8k.dmap ]
   then
      log "${_SELF_}: No activate is in progress."
      rm -f /lock/activate8k.usg
      return
   fi
   percent=$(cat /lock/activate8k.dmap 2>/dev/null)

   if [ "X${XLOGFILE}X" = "XX" ]
   then
      if [ "X${LiveUpdate8k}X" = "XtrueX" ]
      then
         echo "/////////////////////////////////////////////"
         echo "${sync8k_script_name}: Hit <ctrl+c> to stop live monitoring."
         echo "/////////////////////////////////////////////"
      fi
      echo "${_SELF_}: $(uname -n) : ${percent}% complete."
   else
      echo "${_SELF_}: $(uname -n) : ${percent}% complete." >> $XLOGFILE
   fi
   if [ -f /log/activate8k.out.partner -a "X${QuietActivate8k}X" = "XfalseX" ]
   then
      if [ "X${XLOGFILE}X" = "XX" ]
      then
         cat /log/activate8k.out.partner 2>/dev/null
      else
         cat /log/activate8k.out.partner >> $XLOGFILE
      fi
   fi

   rm -f /lock/activate8k.usg
}

# -------------------------------------------------------
# function: checkprim
# args    : none
# description:
#   Checks for primary node. Allow certain commands from
#   primary node only!.
# -------------------------------------------------------
checkprim( ) {
   if [ "X${mynode}X" = "X${sechost}X" -a "X${QuietActivate8k}X" = "XfalseX" ]
   then
      if [ "X${LIVE8kUPGRADE}X" = "XtrueX" ]
      then
         log "${_SELF_}: This ${_SELF_} command can be run from $primhost only!."
         exit $SyncInitiationFailure
      fi
   fi
}

# -------------------------------------------------------
# function: usage
# args    :  none
# description:
#   prompts the user to usage of this script.
# -------------------------------------------------------
usage( ) {
   echo "Usage: ${_SELF_} [  -h | -q | -v | -reset | -done | -check | -auto ]"
   echo "  Note: With no options ${_SELF_} activates fallback on next reboot."
   echo "      : activation is a three step process."
   echo "      : first ${_SELF_} is run to enable activation of fallback image."
   echo "      : next, system is rebooted,at which point the fallback is active."
   echo "      : next, ${_SELF_} -done finishes the activation."
   echo "      : activation needs to be run on each node if cluster."
   echo "  ${_SELF_} -h     : display's this help."
   echo "  ${_SELF_} -v     : display's activation status of the images."
   echo "  ${_SELF_} -q     : display's status of active and fallback images."
   echo "  ${_SELF_} -reset : undo any previous activation."
   echo "  ${_SELF_} -done  : completes activation on reboot."
   echo "  ${_SELF_} -check : checks if activation needs to be done."
   echo "  ${_SELF_} -auto  : automatically activates fallback, and completes the fallback."
}


### Main logic

# Usage help
if [ ${#} -gt 0 -a "X${1}X" == "X-hX" ]
then
    usage
    syncnexit ${Success}
fi

#[0]. Check for user root.
USER=$(id -u)
ARGS=$*
if [ $USER -ne 0 ]
then
    echo "${_SELF_}: You  must be root to run this script."
    syncnexit $UidMisMatch
fi

# Check mount tab and propose to the user the fix.
check_mount_tab
case $? in
1) logwarning "The /etc/mtab mount tab is not setup properly!! Please apply the recommendation above manually." ;;

2) logwarning "Your system requires a reboot, please plan it as soon as possible!
${_SELF_} operations may encounter problems if the reboot is not performed in order to populate properly the /etc/mtab" ;;

3)
    logerror "Cannot detect operating system type!! This is fatal cannot continue.
Please check the status of the RPM package 'sles-release' and the contents of the files:
/etc/SuSE-release
/etc/os-release
"
    exit ${UnsupportedProduct}
;;
esac

#[1]. Initialize the variables.
initvars

#[2]. Show banner in order to display the start of script invocation in logs
show_banner

#[3]. Parse the arguments.

#<NonTraditionalCommands>
while [ "$#" -gt "0" ]
do
   case $1 in
      '-progress')
         #<CheckProgress>
            shift
            XLOGFILE=$1
            checkprim
            displayprogress
            exit $Success
         #</CheckProgress>
         ;;
      '-status')
         #<CheckStatus>
            shift
            lockfile -s 1 /lock/activate8k.usg
            if [ -f /lock/activate8k.lock ]
            then
               cat /lock/activate8k.lock
            else
               log "${_SELF_}: No ${_SELF_} is in progress."
            fi
            rm -f /lock/activate8k.usg
            exit $Success
         #</CheckStatus>
         ;;
      '-quiet')
         #<NonInteractiveMode>
            shift
            QuietActivate8k=true
            QUICKRESET8k=true
         #</NonInteractiveMode>
         ;;
      '-live')
         #<LiveUpdate>
            shift
            LiveUpdate8k=true
            checkprim
            watch -n 4 "${_SELFDIR_}/${_SELF_} -progress"
            exit $Success
         #</LiveUpdate>
         ;;
      '-local')
         shift
         LIVE8kUPGRADE=true
         ;;
      '-manual')
         #<ClusterWideActivate>
            shift
            checkprim
            MANUAL=true
            Activate8kAuto=true
            ${sync8k_path} -v
            cwactivate
            exit $Success
         #</ClusterWideActivate>
         ;;
      '-auto')
            log "${_SELF_}: auto option selected."
            checkprim
            Activate8kAuto=true
            ${sync8k_path} -v
            cwactivate
            activatecomplete reboot
            exit $Success
            ;;
      '-reboot')
         #<ClusterWideReset>
            shift
            checkprim
            activatecomplete reboot
            exit $Success
         #</ClusterWideReset>
         ;;
      '-f')
         shift
         FORCEACTIVATE=true
         ;;
      *)
         break
         ;;
   esac
done
#</NonTraditionalCommands>

while [ "$#" -gt "0" ]
do
   case $1 in
     '-reset')
        RESET="true"
        cleanrepo
        ;;
     '-check')
        check
        syncnexit $Success
        ;;
     '-done')
        FINISHACTIVATION=true
        check
        finishactivate
        syncnexit $Success
        ;;
     '-v'|'-q')
        display
        exit 0
        ;;
     '-h')
        usage
        syncnexit $Success
        ;;
     '-f')
        FORCEACTIVATE=true
        ;;
     *)
        log "Unknown parameter."
        syncnexit $UnknownParameter
        ;;
   esac
   shift
done

#[3].Display statistics.
display

#[4]. Activate or reset.
activate

