#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG
#  This is an automatically generated prolog.
#
#  Copyright (C) Altran ACT S.A.S. 2017,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/events/utils/cl_telinit.sh 1.3 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2008,2009 
# 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/events/utils/cl_telinit.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM

###############################################################################
#
#   COMPONENT_NAME: UTILITIES
#
#   FUNCTIONS: run_telinit
#
###############################################################################

############################################################################
# Function:   run_telinit
# Purpose:    Runs telinit after IP change occured. This function is no 
#             longer used, but has been retained for historical reasons
# Parameters: none
# Return:     none
############################################################################
run_telinit()
{
    typeset PS4_FUNC="run_telinit"
    [[ "$VERBOSE_LOGGING" = "high" ]] && set -x

    # these variable control the telinit wait loop
    # by default produces a wait of 3 minutes for telinit to complete
    typeset -i telinit_wait_count=36
    DELAY=5

    #
    SAVED_XTAB="/usr/es/sbin/cluster/etc/.xtab"
    #
    # Set hostname to first public service address
    #
    if [ ! -f "${TELINIT_FILE}" ]
    then
        #
        # In /etc/inittab, there is an entry to touch "${TELINIT_FILE}"
        # after tcp/ip is functionally up.
        #

        ##
        #
        #  Save NFS exports list from /etc/xtab.  telinit will 
        #  cause rc.nfs to be run which blows away the entries
        #  in /etc/xtab
        ##

        if [ "$EMULATE" = "EMUL" ] 
        then 
            cl_echo 3020 "NOTICE >>>> The following command was not executed <<<< \n"
            echo "cp /etc/xtab ${SAVED_XTAB}"
        else
            cp /etc/xtab ${SAVED_XTAB}
            if [ $? -ne 0 ]
            then
                cl_echo 1051 "Could not save xtab file. Please export hacmp defined filesystems\n"
            fi
            #
            : Start tcp ip servers and network daemons
            #
            telinit a

            typeset -i count=$telinit_wait_count
            let count=count*DELAY
            cl_echo 9104 "$PROGNAME: Waiting up to $count seconds for telinit command to complete.\n" $PROGNAME $count
            let count=0

            #
            : And wait for completion
            #

            # no need to clog the log file with this
            set +x

            while [[ ! -f "${TELINIT_FILE}" && $count -lt $telinit_wait_count ]]
            do
                sleep $DELAY
                let count=count+1
            done

            if [ $count = $telinit_wait_count ]; then
                cl_echo 9105 "$PROGNAME: telinit command failed to complete after $count attempts.\n" $PROGNAME $count
            fi

            [[ "$VERBOSE_LOGGING" = "high" ]] && set -x
            #
            : telinit a completed
            : Append non-duplicate entries in /tmp/xtab to /etc/xtab
            #
            fgrep -vf /etc/xtab ${SAVED_XTAB} >> /etc/xtab
            rm -f ${SAVED_XTAB} > /dev/null 2>&1
        fi
    fi

    #
    : Activate iSCSI software support, now that the service addresses are up
    #
    typeset CFGFILE DEVICE STATUS
    LC_ALL=C lsdev -t iscsi -F'name status' |
    while read DEVICE STATUS
    do
        #
        : DEVICE $DEVICE.STATUS is $STATUS 
        #
        if [[ $DEVICE = @(iscsi[0-9]*) && $STATUS = "Available" ]]
        then
            # Parse the config-file for this ISCSI device
            CFGFILE="$(lsattr -EOl $DEVICE -a disc_filename | sed 1d)"
            # grep-out comments/blanks; anything remaining is an iscsi device
            if grep -Ev '^[ 	]*#|^[ 	]*$' "$CFGFILE"
            then
                # config file has entries; configure ISCSI
                if [[ "$EMULATE" = "EMUL" ]]
                then
                    cl_echo 3020 "NOTICE >>>> The following command was not executed <<<< \n"
                    echo "cfgmgr -l $DEVICE"
                    # Allow for overriding the "cfgvg" part
                    if [[ ! -f $CFGVG_SKIP ]]
                    then
                        cl_echo 3020 "NOTICE >>>> The following command was not executed <<<< \n"
                        echo "cfgvg"
                    else
                        echo "$PROGNAME: File '$CFGVG_SKIP' exists: Skipping 'cfgvg'."
                    fi
                else
                    # Make any child devices (disks. etc.) available
                    cfgmgr -l $DEVICE
                    # Varyon *ALL* Volume Groups that have "AUTO ON:yes"
                    # Allow for overriding the "cfgvg" part
                    if [[ ! -f $CFGVG_SKIP ]]
                    then
                        cfgvg
                    else
                        echo "$PROGNAME: File '$CFGVG_SKIP' exists: Skipping 'cfgvg'."
                    fi
                fi
            else
                # config file is only comments and blank lines
                echo "$PROGNAME: Skipping device '$DEVICE' (not configured)."
            fi
        else
            echo "$PROGNAME: Skipping device '$DEVICE' (not Available)."
    fi
    done
}

#########################################################################
# Main Starts Here
#########################################################################

typeset PROGNAME=${0##*/}
export PATH="$(/usr/es/sbin/cluster/utilities/cl_get_path all)"
if [[ $VERBOSE_LOGGING == "high" ]]; then
    set -x
    version='%I%'
fi

# Determine telinit mode (disabled is default)
TELINIT_FILE="/usr/es/sbin/cluster/.telinit"
USE_TELINIT_FILE="/usr/es/sbin/cluster/.use_telinit"

if [[ -f $USE_TELINIT_FILE ]]
then
    USE_TELINIT=1
else
    USE_TELINIT=0
fi

# If the command arg is "-boot", then do boot-time processing only.
# This should only be done at boot (presumably by harc.net).
# NOTE: rc.cluster is always called with the "-boot" flag (and
# thus calls harc.net) even when not at boot.  Therefore, we will
# only null the xtab file when the NFS daemons are not active.

if [[ "$1" == "-boot" ]]
then
    rm -f ${TELINIT_FILE}

    # -boot actions will be written to clutils.log, not hacmp.out
    ODMDIR="/etc/es/objrepos"
    DEFAULTLOGDIR="/var/hacmp/log"

    STANZA=$(odmget -q"name = clutils.log" HACMPlogs)
    if [ "$STANZA" != "" ]
    then
        DESTDIR=`echo $STANZA | cut -d'"' -f8`
        CLUTILS_LOG_FILE="$DESTDIR/clutils.log"
    else
        CLUTILS_LOG_FILE="$DEFAULTLOGDIR/clutils.log"
    fi

    # Determine NFS daemon status
    nfsd_running=0
    if ps -eo comm | grep -w nfsd > /dev/null ; then
        nfsd_running=1
    fi

    mountd_running=0
    if ps -eo comm | grep -w rpc.mountd > /dev/null ; then
        mountd_running=1
    fi

    # Truncate xtab only if the nfsd and mountd daemons are not active
    if (( !nfsd_running && ! mountd_running )); then
        echo "$(date) - $PROGNAME: The NFS daemons are inactive, /etc/xtab will be cleared." >> $CLUTILS_LOG_FILE
        /usr/sbin/exportfs -ua
    else
        echo "$(date) - $PROGNAME: The NFS daemons are active, /etc/xtab will not be modified." >> $CLUTILS_LOG_FILE
    fi
    return 0
fi

if ! cl_lsitab clinit > /dev/null 2>&1
then
    #
    : telinit a disabled
    #
    return 0
fi

if [[ -f $TELINIT_FILE ]]
then
    #
    : $TELINIT_FILE exists
    #
    return 0
fi

run_telinit
