#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG
#  This is an automatically generated prolog.
#
#  Copyright (C) Altran ACT S.A.S. 2017,2018,2019,2021.  All rights reserved.
#
#  ALTRAN_PROLOG_END_TAG
#
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/utilities/halevel.sh 1.3.1.2 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2010,2014 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
#
# @(#)  7d4c34b 43haes/usr/sbin/cluster/utilities/halevel.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
###############################################################################
# Command to report the Version, Release, Modification, and optionally
# the Service Pack level of the installed PowerHA SystemMirror product on 
# server nodes.
#
# It's the PowerHA SystemMirror implementation of the AIX oslevel command.
###############################################################################

function usage
{
    typeset -i rc=$1

    typeset -i FH=1
    (( rc != 0 )) && FH=2

    dspmsg -s 29 scripts.cat 2 "halevel: usage: halevel [-h|-?] [-s] [-x]\n\t-h Usage - this message\n\t-s Show Service Pack level\n\t-x Turn on debugging\n" >&$FH

    exit $rc
} # End of "usage()"


###############################################################################
#   M A I N    MAIN    Main    main
###############################################################################
typeset TAB=$(printf '\t')
export PS4='${0##*/}[$LINENO]($?):$TAB '
[[ $VERBOSE_LOGGING == "high" ]] && set -x

#
: Initialize Variables
#
export LANG=C   # Must have LANG=C because output of lslpp is parsed
s_flag=0        # To display Version Release Modification and Service Pack
ha_vrmf=0       # PowerHA SystemMirror VRMF
ha_fix=0        # PowerHA SystemMirror Fix Level or Service Pack
v_flag=no       # Initialize verbose flag to no
integer BUILD=0
integer EFFECTIVE=0
integer VRMF_FORMAT=0

#
:   get command line options
#
while getopts ':befhxsv' option ; do
    case $option in
        b ) BUILD=1 ;;
        e ) EFFECTIVE=1  ;;
        f ) VRMF_FORMAT=1  ;;
        s ) #
            :   Display VRM and SP
            #
            s_flag=yes
        ;;

        h ) #
            :   Display usage message, then exit
            #
            usage 0
        ;;

        x ) #
            :   Debugging mode 
            #
            VERBOSE_LOGGING=high
            set -x
        ;;

        v ) #
            :   Display verbose SP info
            #
            v_flag=yes
        ;;

        * ) #
            :   Caller is confused. Display usage message, then exit.
            #
            usage 1
        ;;
    esac
done

if (( BUILD )); then
    #
    : First, scan each file and extract its SCCS string, sort out the newest
    : build. This will be compared with the build number stored by the build
    : process itself, and the newest build wins.
    #
    typeset BUILD_INFO="" DATETIME=""
    find /usr/es -type d -name ui -prune -o -type f -exec what {} \; |\
         awk '{if ($4 ~ /[0-9]{4}[A-Z]_aha[0-9]{3}/) print $4, $5, $6, $7, $8, $9}' |\
         sort +0 > /var/hacmp/log/halevel.$$
    BUILD_INFO=$(tail -1 /var/hacmp/log/halevel.$$)
    BUILD_INFO=${BUILD_INFO%%+([[:space:]])*}
    if [[ -z $BUILD_INFO || \
          ${BUILD_INFO:0:4} != +([0-9]) ]]
    then
        BUILD_INFO=0  # Will use pkm_buildinfo.h in this case
    else
        BUILD_INFO=${BUILD_INFO%,}
    fi
    if [[ $v_flag == "yes" ]]; then
        DATETIME=$(grep "^$BUILD_INFO" /var/hacmp/log/halevel.$$ | \
                   sed 's/"//g' | sort | tail -1 | cut -d"$IFS" -f2-6)
        DATETIME=$(echo $DATETIME | sed 's/ \(20[0-9][0-9]\) / \1, /')
    fi
    rm -f /var/hacmp/log/halevel.$$

    #
    : Extract the build level stored by the build process itself
    #
    if [[ -s /usr/es/sbin/cluster/pkm_buildinfo.h ]]; then
        typeset DS="" TS="" BL="" REM="" NL=$'\n'
        head -3 /usr/es/sbin/cluster/pkm_buildinfo.h | tail -2 |\
        tr "$NL" " " | IFS=, read DS TS BL REM
        if [[ $v_flag == "yes" ]]; then
            DS=$(echo ${DS//\"/})
            TS=$(echo ${TS//\"/})
        fi
        BL=$(echo ${BL//\"/})

        typeset PKM_BL_NUM=${BL:0:4}
        typeset DISC_BL_NUM=${BUILD_INFO:0:4}
        if [[ $PKM_BL_NUM == +([0-9]) && $DISC_BL_NUM == +([0-9]) ]] && \
           (( PKM_BL_NUM >= DISC_BL_NUM ))
        then
            BUILD_INFO=$BL
            [[ $v_flag == "yes" ]] && DATETIME="$DS, $TS"
        fi
    fi

    if [[ $v_flag == "yes" ]]; then
        print -- "$BUILD_INFO ($DATETIME)"
    else
        print -- "$BUILD_INFO"
    fi

    exit 0
fi

##############################################################################
#
:   Here try and figure out what level of PowerHA SystemMirror is installed.  
#
if (( EFFECTIVE ))
then
    :   The "effective" version is the one that the cluster manager
    :   is actually running at.
    ha_vrmf=$(lslpp -lcq 'cluster.es.server.rte' 2>/dev/null | cut -f3 -d: | sort -t. +3 -n | tail -1)
else
    :   It is the largest VRMF of all installed PowerHA SystemMirror filesets.
    ha_vrmf=$(lslpp -lcq 'cluster.*' 2>/dev/null | cut -f3 -d: | sort -t. +3 -n | tail -1)
fi

##############################################################################
#
:   Make sure PowerHA SystemMirror filesets are installed
#
if [[ -z $ha_vrmf ]]
then
    dspmsg -s 27 scripts.cat 1 "halevel: ERROR: PowerHA SystemMirror does not appear to be installed\n" >&2
    exit 1
fi

##############################################################################
#
:   Display PowerHA SystemMirror VRMF
#
if [[ $s_flag == yes ]]
then
    #
    :	Display installed VRM and SP
    #
    ha_vrm=$(print $ha_vrmf | cut -d'.' -f1-3)
    ha_fix=$(print $ha_vrmf | cut -d'.' -f4) 

    if (( $ha_fix == 0 ))
    then
        (( VRMF_FORMAT )) && ha_fix='.0' || ha_fix=' GA'
    else
        (( VRMF_FORMAT )) && ha_fix=".$ha_fix" || ha_fix=" SP$ha_fix"
    fi

    print $ha_vrm$ha_fix

else
    #
    :	Display installed VRM only, without SP
    #
    print $ha_vrmf | cut -d'.' -f1-3
fi

exit 0
