#!/bin/ksh
DEVTREE=/tmp/devtree.out
LPM_CLI_INFO=/tmp/lpm_cli_info
DEST_INFO=/tmp/lpm_dest_info
DEVSCAN=/usr/local/bin/devscan
ISVIOS=0   # Defaulting to AIX client LPAR type.
VER="Sep 1 2015"


if [ $# -ne 0 ]; then
    echo "========================================"
    echo "This script is not an official IBM script and support is limited."
    echo "It is used only for validating NPIV SAN Zoning information before attempting LPM."
    echo "Currently it only works with AIX Client LPAR and VIOS."
    echo "========================================"
    exit 0
fi

if [[ ! -x $DEVSCAN ]]; then
        echo "Download and install devscan from IBM website for the script to work."
        exit
fi

# Save device tree output in tmp directory
/usr/lib/boot/bin/dmpdt_chrp > $DEVTREE
ISVIOS=`grep -c -E "ibm,aix-hosting" $DEVTREE`

unameout=`uname -n`

if [[ $ISVIOS -eq 1  ]]; then    # VIOS VIOS VIOS VIOS

    if [[ ! -s $LPM_CLI_INFO ]]; then
        echo "File generated from Client LPAR does not exist."
        echo "Copy /tmp/lpm_cli_info from Client LPAR to be migrated to /tmp/lpm_cli_info on this VIOS."
        exit
    fi

    cliname=`grep hostname $LPM_CLI_INFO | cut -d ' ' -f 5`
    vioslist=`grep vfchost $LPM_CLI_INFO | cut -f 4 -d ' ' | sort -u`
    vioscount=`echo $vioslist | wc -w`
    wwpn_in=`grep Inactive $LPM_CLI_INFO | cut -f 6 -d ' '`

    echo "Version : $VER" > $DEST_INFO
    echo "Date : `date`" >> $DEST_INFO
    echo "on VIOS $unameout, targets zoned for inactive WWPNs from client $cliname" >> $DEST_INFO
    echo >>  $DEST_INFO

    print -n "Working"
    for wwpn in $wwpn_in; do
        echo "WWPN $wwpn" >> $DEST_INFO
        $DEVSCAN -t f -c 0 -n $wwpn --concise    | grep -v WWPN >> $DEST_INFO
        echo >> $DEST_INFO
        print -n "."
    done
    echo;echo
    cat $DEST_INFO
    echo "--------------------------------------"
    echo "There are $vioscount VIOS providing NPIV disks to $cliname"
    echo They are $vioslist
    echo "--------------------------------------"

    for Sour_VIOS in $vioslist; do
        Naaah=0
        echo;print -n "Checking Inactive wwpns .. "
        fcslist=`grep $Sour_VIOS $LPM_CLI_INFO | cut -d ' '  -f 1`
        for fcsxx in $fcslist; do
            wwpn_in=`grep -p $fcsxx $LPM_CLI_INFO | grep Inactive | cut -f 6 -d ' '`
            print -n "  "$wwpn_in
            fscsixx=`echo $fcsxx | awk 'gsub("fcs","fscsi")'`
            srctargets=`grep $fscsixx $LPM_CLI_INFO |cut -d "|" -f 3`
            dsttargets=`grep -p $wwpn_in $DEST_INFO`
            echo;
            for targ in $srctargets; do
                match=`grep -p $wwpn_in $DEST_INFO | grep -c $targ`
                if [[ $match -ne 0 ]]; then
                    echo "Target wwpn $targ can be reached using inactive wwpn $wwpn_in via $match adapters."
                else
                    Naaah=1
                fi
            done
        done
        echo
        if [[ $Naaah -ne 0 ]]; then
           echo "**** NO. VIOS $unameout can NOT host Client LPAR $cliname like Source VIOS $Sour_VIOS after LPM ****" 
        else
           echo "**** YES. VIOS $unameout CAN host Client LPAR $cliname like Source VIOS $Sour_VIOS after LPM ****" 
        fi
    done
    echo;echo " In order to host Client LPAR $cliname, VIOS $unameout should be able to virtualize FC adapters for at least one set of inactive WWPNS listed above. "
    echo " If not, verify zoning."
    echo

else    # Client Client Client Client

    echo "Version : $VER" > $LPM_CLI_INFO
    echo "Date : `date`" >> $LPM_CLI_INFO
    echo "Client LPAR hostname : $unameout" >> $LPM_CLI_INFO

    # echo; echo "Available Virtual Fibre Channel Client Adapters :"
    # lsdev -Cc adapter | grep -E "Virtual Fibre Channel Client Adapter"
    fcsx=`lsdev -Cc adapter | grep -E "Virtual Fibre Channel Client Adapter"|cut -f 1 -d ' '`
    if [[ $fcsx = "" ]]; then
            echo;echo "No Virtual Fiber Channel Adapters"
            exit
    fi

    echo
    for fcs in $fcsx; do
        fcstat_wwpn=`fcstat $fcs | grep "World Wide Port Name" | cut -f 5 -d ' '|cut -b 3-`
        lscfg_node=`lscfg -pl $fcs | grep -E "Node:"|cut -f 7 -d ' '` 

        devwwpn1=`grep -p= $lscfg_node $DEVTREE | awk '{
        if (/ibm,port-wwn-1/ ) {
            getline;
            wwpn=$1$2
            print toupper(wwpn)}
        }'`

        devwwpn2=`grep -p= $lscfg_node $DEVTREE | awk '{
        if (/ibm,port-wwn-2/ ) {
            getline;
            wwpn=$1$2
            print toupper(wwpn)}
        }'`

        viosdetails=`echo "vfcs $fcs" | kdb | grep host`

        if [[ $fcstat_wwpn = $devwwpn1 ]]; then 
            echo $fcs $devwwpn1 $viosdetails "--- Active" >> $LPM_CLI_INFO
        else
            echo "    " $devwwpn1 "--- Inactive" >> $LPM_CLI_INFO
        fi

        if [[ $fcstat_wwpn = $devwwpn2 ]]; then 
            echo $fcs $devwwpn2 $viosdetails "--- Active" >> $LPM_CLI_INFO
        else
            echo "    " $devwwpn2 "--- Inactive" >> $LPM_CLI_INFO
        fi

        echo "     List of Storage ports mapped to Client: $unameout on Active WWPN" >> $LPM_CLI_INFO
        $DEVSCAN -t f -c 0 --concise --dev=$fcs  | grep -v WWPN >> $LPM_CLI_INFO
        echo >> $LPM_CLI_INFO
    done

    cat $LPM_CLI_INFO 
    echo "Copy file /tmp/lpm_cli_info file on to destination VIOS at same location to verify NPIV SAN Zoning."
    echo "or copy paste above info into file /tmp/lpm_cli_info on destination VIOS."
    echo "Then, execute same script $0 on VIOS in oem_setup_env shell"
    echo
fi
