#!/bin/ksh
DEVTREE=/tmp/devtree.out
INACTIVE=/tmp/wwpn_inactive
DEVSCAN=/usr/local/bin/devscan


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

unameout=`uname -L`
echo "List of inactive WWPNs on this LPAR with LPAR ID and Name : $unameout"  > $INACTIVE

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_wwpn=`lscfg -vl $fcs | grep -E "Network Address"|cut -f 14 -d .`
        lscfg_node=`lscfg -pl $fcs | grep -E "Node:"|cut -f 7 -d ' '` 
        #echo " lscfg_wwpn=$lscfg_wwpn"
        #echo " fcstat_wwpn=$fcstat_wwpn"
        #echo " lscfg_node=$lscfg_node"

        echo "$fcs:"
        #echo "$fcs: $fcstat_wwpn (Active) "

        #echo Processing Device Tree
        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)}
        }'`

        if [[ $fcstat_wwpn = $devwwpn1 ]]; then 
                isactive="--- Active" 
        else
                isactive=""
                viosdetails=`echo "vfcs $fcs" | kdb | grep host`
                echo $devwwpn1 $viosdetails >> $INACTIVE
        fi
        echo "  WWPN1 = $devwwpn1  $isactive"

        if [[ $fcstat_wwpn = $devwwpn2 ]]; then 
                isactive="--- Active" 
        else
                isactive=""
                viosdetails=`echo "vfcs $fcs" | kdb | grep host`
                echo $devwwpn2 $viosdetails >> $INACTIVE
        fi
        echo "  WWPN2 = $devwwpn2  $isactive"
        echo
done

cat $INACTIVE

echo;echo "List of Storage ports mapped to this client :"
$DEVSCAN -t f -c 0 --concise

