#!/bin/bash
# 
# Copyright 2003-2016 Avago Technologies. All rights reserved.
# 

SUCCESS=0
is_fhs_build=0
LNX_32BIT="com.emulex.emulexapilibrary32 /usr/lib/libemulexhbaapi.so"
LNX_64BIT="com.emulex.emulexapilibrary64 /usr/lib64/libemulexhbaapi.so"
LNX_FHS_32BIT="com.emulex.emulexapilibrary32 /opt/emulex/ocmanager/lib/libemulexhbaapi.so"
LNX_FHS_64BIT="com.emulex.emulexapilibrary64 /opt/emulex/ocmanager/lib64/libemulexhbaapi.so"

# Function:  add_entries_hba_conf()
#
# Description:
#   add emulex hbaapi entries to /etc/hba.conf
#
add_entries_hba_conf()
{
    arch=`uname -m`
    if [ "$arch" = "ppc64le" ];then
        if [ $is_fhs_build -eq 1 ]; then
            echo "$LNX_FHS_64BIT" >> /etc/hba.conf
        else
            echo "$LNX_64BIT" >> /etc/hba.conf    
        fi
    else
        if [ "$arch" = "ppc64" ];then
            echo "$LNX_64BIT" >> /etc/hba.conf
        else
            if [ "$arch" = "x86_64" ];then
                echo "$LNX_64BIT" >> /etc/hba.conf
            fi
        fi
        echo "$LNX_32BIT" >> /etc/hba.conf
    fi
    return $SUCCESS
}

# determine if this invocation of the script is confirming /etc/hba.conf
if [ $# -gt 0 ]; then
    if [ "$1" = "-hba.conf" ]; then
        if [ ! -r /etc/hba.conf ]; then
            touch /etc/hba.conf
        fi
        if [ ! -w /etc/hba.conf ]; then
            # this is a non-root user (secure mgmnt) with no write privileges on this system file
            exit
        fi
        
        # determine if emulex entries currently exist in file
        NUM=`cat /etc/hba.conf | grep "emulexapilibrary" | wc -l`
        if [ $NUM -eq 0 ]; then
            add_entries_hba_conf
        fi
        exit
    fi
fi
exit

