#!/bin/sh
#######################################################################
#  Copyright (c) 2002-2023, Dell Inc. or its subsidiaries. All Rights Reserved.
#
#  PowerPath Dynamic Lun addition and removal scanning Linux script
#######################################################################


#-----------------------------------------------------
##    Variables
#-----------------------------------------------------

REDHAT=/etc/redhat-release
SLES=/etc/SuSE-release
DRIVERS_LOC=/sys/bus/pci/drivers
SCSI_HOSTS=/sys/class/scsi_host
FC_HOSTS=/sys/class/fc_host
PROC_SCSI=/proc/scsi/scsi
SCRIPT_NAME=$(basename $0)
TMP=/etc/opt/emcpower/.tmp/${SCRIPT_NAME}_before_scan.$$
TMP1=/etc/opt/emcpower/.tmp/${SCRIPT_NAME}_scan_done.$$
TMP2=/etc/opt/emcpower/.tmp/${SCRIPT_NAME}_after_scan.$$
TMP3=/etc/opt/emcpower/.tmp/${SCRIPT_NAME}_new_device.$$
TMP4=/etc/opt/emcpower/.tmp/${SCRIPT_NAME}_user_tgt.$$
TMP5=/etc/opt/emcpower/.tmp/${SCRIPT_NAME}_sort_before_scan.$$
TMP6=/etc/opt/emcpower/.tmp/${SCRIPT_NAME}_before_scan_pseudo.$$
MAX_LUN=20
MAX_TGT=64
NDU_LOCK_FILE=/etc/opt/emcpower/.ndu_lock

#-----------------------------------------------------
##    HEADER
#-----------------------------------------------------
HEADER()
{
    echo "$1" | awk '{
    i=70;k=1
    for(k=1;k<i;k++)printf("-")
    printf("\n")}'
}

#-----------------------------------------------------
##    Exit
#-----------------------------------------------------
EXIT()
{

if [[ $menu == 0 ]]; then
    echo "Good Bye"
    exit 0
elif [[ $cli == 0 || $cli == "" ]]; then
    echo "Good Bye"
    exit 1
fi

}

#-----------------------------------------------------
##   Messages to System log
#-----------------------------------------------------
PP_LOGGER()
{
msg=$*
type logger > /dev/null 2>&1

if [ $? -eq 0 ]; then
    logger -t $SCRIPT_NAME -- ${msg}
fi

}


#-----------------------------------------------------
##    Audit Messages
#-----------------------------------------------------
AUDIT_MESSAGE()
{

if [[ ${ACTION} != "" ]]; then
    PP_LOGGER info: cmd=${SCRIPT_NAME}: ${ACTION} \
    \(user ID real=$(id -u) effective=$(id -g)\)
fi

}
#-----------------------------------------------------
##   Cleanup function  
#-----------------------------------------------------
CLEANUP_TMP()
{

# Cleanup temp files from the system.
for tmp_files in $TMP $TMP1 $TMP2 $TMP3 $TMP4 $TMP5 $TMP6
do

    if [ -f ${tmp_files} ]; then
        rm -f ${tmp_files}
    fi

done

}

#-----------------------------------------------------
##   udev settle  
#-----------------------------------------------------
UDEV_SETTLE()
{    

if [[ -f /sbin/udevadm ]]; then

    # SLES 11 uses udevadm settle
    /sbin/udevadm settle --timeout=240  > /dev/null 2>&1
    sleep 1

elif [[ -f /sbin/udevsettle ]]; then

    # SLES 10 and RHEL 5 uses udevsettle
    /sbin/udevsettle --timeout=240  > /dev/null 2>&1
    sleep 1
    
else
    sleep 6

fi

}

#-----------------------------------------------------
##    HBA SCAN Sub-function
#-----------------------------------------------------
FC_SCAN_SUB()
{

ls -1 -d $DRIVERS_LOC/${hba}*/*/${scsi_host_id} > /dev/null 2>&1
driver_locate=$?

if [[ ${driver_locate} == 0 ]]; then
    hba_ports=$(ls -1 -d ${DRIVERS_LOC}/${hba}*/*/${scsi_host_id} |\
                awk -F "host" '{ print $NF }') > /dev/null 2>&1

    if [[ ${hba_ports} == "" ]]; then
        echo "ERROR: Incorrect HBA SCSI host number $(echo ${scsi_host_id} | awk -F "host" '{ print $NF }')"
        PP_LOGGER error: Incorrect HBA SCSI host number $(echo ${scsi_host_id} | awk -F "host" '{ print $NF }')
        fc_error=1
    fi

    for scsi_num in ${hba_ports} ; do

        if [[ ${hba} == qla ]]; then
            ls -1 -d ${DRIVERS_LOC}/${hba}4x*/*/host${scsi_num} > /dev/null 2>&1
            ihba=$?

            if [[ ${ihba} == 0 ]]; then
                echo "Scanning $hba iSCSI HBA SCSI host number: ${scsi_num}"
                PP_LOGGER info: Scanning $hba iSCSI HBA SCSI host number: ${scsi_num}
            else
                echo "Scanning $hba SCSI host number: ${scsi_num}"
                PP_LOGGER info: Scanning $hba SCSI host number: ${scsi_num}
            fi

        else
            echo "Scanning $hba SCSI host number: ${scsi_num}"
            PP_LOGGER info: Scanning $hba SCSI host number: ${scsi_num}
        fi

        if [[ -f  ${SCSI_HOSTS}/host${scsi_num}/scan ]]; then
            echo "- - -" > ${SCSI_HOSTS}/host${scsi_num}/scan
            UDEV_SETTLE
            fc_info=0
        else
            echo "ERROR: SCSI host number ${scsi_num} scan failed"
            PP_LOGGER error: SCSI host number ${scsi_num} scan failed
            fc_error=1
        fi

    done

else
    echo "ERROR: ${hba} SCSI host number not found"
    PP_LOGGER error: ${hba} SCSI host number not found
    fc_error=1
fi

}


#-----------------------------------------------------
##    hba SCAN 
#-----------------------------------------------------
FC_SCAN()
{

ls -1 -d ${DRIVERS_LOC}/qla* > /dev/null 2>&1
qla=$?
ls -d ${DRIVERS_LOC}/lpfc > /dev/null 2>&1
lpfc=$?
ls -d ${DRIVERS_LOC}/bfa > /dev/null 2>&1
bfa=$?
ls -d ${DRIVERS_LOC}/fnic > /dev/null 2>&1
fnic=$?
ls -d ${DRIVERS_LOC}/emcctd > /dev/null 2>&1
emcctd=$?

if [[ $# == 0 ]]; then

    if [[ ${qla} == 0 ]]; then
        echo "INFO: Qlogic HBAs available on the host"
        PP_LOGGER info: Qlogic HBAs available on the host
        hba=qla
        scsi_host_id="host*"    
        FC_SCAN_SUB
    fi

    if [[ ${lpfc} == 0 ]]; then
        echo "INFO: Emulex HBAs available on the host"
        PP_LOGGER info: Emulex HBAs available on the host
        hba=lpfc
        scsi_host_id="host*"    
        FC_SCAN_SUB 
    fi

    if [[ ${bfa} == 0 ]]; then
        echo "INFO: Brocade HBAs available on the host"
        PP_LOGGER info: Brocade HBAs available on the host
        hba=bfa
        scsi_host_id="host*"    
        FC_SCAN_SUB 
    fi
    if [[ ${fnic} == 0 ]]; then
        echo "INFO: Cisco HBAs available on the host"
        PP_LOGGER info: Cisco HBAs available on the host
        hba=fnic
        scsi_host_id="host*"    
        FC_SCAN_SUB 
    fi
    if [[ ${emcctd} == 0 ]]; then
        echo "INFO: EMC CTD HBAs available on the host"
        PP_LOGGER info: EMC CTD HBAs available on the host
        hba=emcctd
        scsi_host_id="host*"    
        FC_SCAN_SUB 
    fi

else

    for hba_ports in $@ ; do

        if [ -d ${DRIVERS_LOC}/qla*/*/host${hba_ports} ]; then
            hba=qla
            scsi_host_id=host${hba_ports}
            FC_SCAN_SUB         
        elif [ -d ${DRIVERS_LOC}/lpfc/*/host${hba_ports} ]; then
            hba=lpfc
            scsi_host_id=host${hba_ports}
            FC_SCAN_SUB 
        elif [ -d ${DRIVERS_LOC}/bfa/*/host${hba_ports} ]; then
            hba=bfa
            scsi_host_id=host${hba_ports}
            FC_SCAN_SUB 
        elif [ -d ${DRIVERS_LOC}/fnic/*/host${hba_ports} ]; then
            hba=fnic
            scsi_host_id=host${hba_ports}
            FC_SCAN_SUB 
        elif [ -d ${DRIVERS_LOC}/emcctd/*/host${hba_ports} ]; then
            hba=emcctd
            scsi_host_id=host${hba_ports}
            FC_SCAN_SUB 
        elif [ -f ${SCSI_HOSTS}/host${hba_ports}/proc_name ]; then
            iscsi_procname=$(cat ${SCSI_HOSTS}/host${hba_ports}/proc_name | cut -c 1-5)

            if [[ ${iscsi_procname} == "iscsi" || ${iscsi_procname} == "fcoe" ]]; then
                echo "Scanning ${iscsi_procname} host number: ${hba_ports}"
                PP_LOGGER info: Scanning ${iscsi_procname} host number: ${hba_ports}
                echo "- - -" > ${SCSI_HOSTS}/host${hba_ports}/scan
                UDEV_SETTLE
                iscsi_fcoe_info=0
            else
                echo "ERROR: Incorrect SCSI host number ${hba_ports}"
                PP_LOGGER error: Incorrect SCSI host number ${hba_ports}
                fc_error=1
            fi

        else
            echo "ERROR: Incorrect SCSI host number ${hba_ports}"
            PP_LOGGER error: Incorrect SCSI host number ${hba_ports}
            fc_error=1
        fi

    done

fi

if [[ ${qla} != "0" && ${lpfc} != "0" && ${bfa} != "0" && ${iscsi_fcoe_info} != "0" && ${fnic} != "0" && ${emcctd} != "0" ]]; then
    echo "ERROR: No supported HBAs found on the host."
    PP_LOGGER error: No supported HBAs found on the host.
    fc_error=1
fi

}

#-----------------------------------------------------
##    iSCSI Sessions SCAN 
#-----------------------------------------------------
iSCSI_SCAN()
{

echo "INFO: Scanning iSCSI available sessions"
PP_LOGGER info: Scanning iSCSI available sessions
iscsi_ports=$(ls -1 ${SCSI_HOSTS}/ | awk -F "host" '{ print $NF }') > /dev/null 2>&1

for scsi_num in ${iscsi_ports} ; do

    if [[ -f ${SCSI_HOSTS}/host${scsi_num}/proc_name ]]; then
        iscsi_procname=$(cat ${SCSI_HOSTS}/host${scsi_num}/proc_name | cut -c 1-5)

        if [[ ${iscsi_procname} == "iscsi"  ]]; then
            echo "Scanning ${iscsi_procname} host number: ${scsi_num}"
            PP_LOGGER info: Scanning ${iscsi_procname} host number: ${scsi_num}
            echo "- - -" > ${SCSI_HOSTS}/host${scsi_num}/scan
            UDEV_SETTLE
            iscsi_info=0
        fi

    fi

    if [[ ${scsi_num} == "iscsi" ]]; then
        host_no=$(cat ${SCSI_HOSTS}/${scsi_num}/host_no)
        echo "Scanning ${scsi_num} host number: ${host_no}"
        PP_LOGGER info: Scanning ${scsi_num} host number: ${host_no}
        echo "- - -" > ${SCSI_HOSTS}/${scsi_num}/scan 
        UDEV_SETTLE
        iscsi_info=0
    fi

done

if [[ ${iscsi_info} != "0"  ]]; then
    echo "ERROR: iSCSI Sessions NOT found on the host"
    PP_LOGGER error: iSCSI Sessions NOT found on the host
    iscsi_error=1
fi    

}
#-----------------------------------------------------
##    Open-FCoE Sessions SCAN 
#-----------------------------------------------------

OPEN_FCOE_SCAN()
{

echo "INFO: Scanning Open-FCoE available sessions"
PP_LOGGER info: Scanning Open-FCoE available sessions
fcoe_ports=$(ls -1 ${SCSI_HOSTS}/ | awk -F "host" '{ print $NF }') > /dev/null 2>&1

for scsi_num in ${fcoe_ports} ; do

    if [[ -f ${SCSI_HOSTS}/host${scsi_num}/proc_name ]]; then
        fcoe_procname=$(cat ${SCSI_HOSTS}/host${scsi_num}/proc_name | cut -c 1-5)

       if [[ ${fcoe_procname} == "fcoe" ]]; then
            echo "Scanning ${fcoe_procname} host number: ${scsi_num}"
            PP_LOGGER info: Scanning ${fcoe_procname} host number: ${scsi_num}
            echo "- - -" > ${SCSI_HOSTS}/host${scsi_num}/scan
            UDEV_SETTLE
            fcoe_info=0
       fi

    fi

done

if [[ ${fcoe_info} != "0"  ]]; then
    echo "ERROR: Open-FCoE Sessions NOT found on the host"
    PP_LOGGER error: Open-FCoE Sessions NOT found on the host
    fcoe_error=1
fi    

}
#-----------------------------------------------------
##    Discover new device 
# This function will be used when we scan entire bus
#-----------------------------------------------------
SCAN_LOG()
{

if [[ $1 == before ]]; then
    powermt display dev=all 2>/dev/null | grep Pseudo | awk -F = '{ print $NF }' >> $TMP6
elif [[ $1 == after ]]; then
    device_info=0
    powermt display dev=all 2>/dev/null | grep Pseudo | awk -F = '{ print $NF }' >> $TMP2

    while read h 
    do
        cat $TMP6 |  grep "\<${h}\>" > /dev/null 2>&1

        if [[ $? != 0 ]]; then

            echo $h >> $TMP3 

        fi

    done < $TMP2

    if [ -f $TMP3 ]; then

        for Pseudo in $(cat $TMP3 | sort -u)
        do

            if [[ $device_info != 1 ]]; then
                device_info=1
                echo "INFO: Following new device(s) discovered"
               #PP_LOGGER "info: Following new device\(s\) discovered"
                #HEADER
            fi

            FC_iSCSI_NATIVE
        done

        HEADER
    else
        echo "INFO: No new device(s) discovered"
        HEADER

    fi

fi

}

#-----------------------------------------------------
##    Discover new device 
# This function will be used when we scan Array ID and HBA
#-----------------------------------------------------
VERIFY_ARRAY_ID_SCAN()
{

if [ -f $TMP1 ]; then

    while read h c t l
    do

        #ls -l /sys/block/sd* |  grep "\<${h}\>:\<${c}\>:\<${t}\>:\<${l}\>" > /dev/null 2>&1
        ls -l /sys/class/scsi_disk/ |  grep "\<${h}\>:\<${c}\>:\<${t}\>:\<${l}\>" > /dev/null 2>&1

        if [[ $? != 0 ]]; then

            if [[ $l -le 9 && ${t} -le 9 ]]; then
                cat ${PROC_SCSI} | grep "scsi${h} Channel: 0${c} Id: 0${t} Lun: 0${l}" > /dev/null 2>&1

                if [[ $? == 0 ]]; then
                    echo "scsi remove-single-device $h $c $t $l" > $PROC_SCSI 
                fi

            elif [[ $l -gt 9 && ${t} -le 9 ]]; then
                cat ${PROC_SCSI} | grep "scsi${h} Channel: 0${c} Id: 0${t} Lun: ${l}" > /dev/null 2>&1

                if [[ $? == 0 ]]; then
                    echo "scsi remove-single-device $h $c $t $l" > $PROC_SCSI  
                fi

            elif [[ $l -le 9 && ${t} -gt 9 ]]; then
                cat ${PROC_SCSI} | grep "scsi${h} Channel: 0${c} Id: ${t} Lun: 0${l}" > /dev/null 2>&1

                if [[ $? == 0 ]]; then
                    echo "scsi remove-single-device $h $c $t $l" > $PROC_SCSI  
                fi

            elif [[ $l -gt 9 && ${t} -gt 9 ]]; then
                cat ${PROC_SCSI} | grep "scsi${h} Channel: 0${c} Id: ${t} Lun: ${l}" > /dev/null 2>&1

                if [[ $? == 0 ]]; then
                    echo "scsi remove-single-device $h $c $t $l" > $PROC_SCSI  
                fi

            fi

        fi

    done < ${TMP1}
fi

}


#-----------------------------------------------------
##    Scaning the bus using Array ID and HBA input 
#-----------------------------------------------------
ARRAY_ID_SCAN()
{

if [[ $hlu_userdef == "" ]]; then
    hlu_scan=0
else

    if [[ $hlu_userdef -le $MAX_LUN ]]; then
        hlu_scan=$hlu_userdef
    else
        echo "ERROR: Hlu scan is limited upto $MAX_LUN"
        PP_LOGGER error: Hlu scan is limited upto $MAX_LUN
        EXIT
    fi

fi

while [[ $hlu_scan -le $MAX_LUN ]]; do

     cat $TMP | sort -u >> $TMP5
     mv $TMP5 $TMP

     while read h c t
     do
        ls -1 /sys/class/scsi_disk/ | grep "${h}:${c}:${t}:${hlu_scan}"  > /dev/null 2>&1
        hlu_info=$?
        scsi_port=$h
        channel=$c
        target=$t

        if [[ $hlu_info != 0 ]]; then
            echo "Scanning Host:${scsi_port} Channel:${channel} Target:${target} Hlu:${hlu_scan}"
            PP_LOGGER info: Scanning Host:${scsi_port} Channel:${channel} Target:${target} Hlu:${hlu_scan}
            echo "${channel} ${target} ${hlu_scan}" > ${SCSI_HOSTS}/host${scsi_port}/scan 2>/dev/null
            if [[ $? == 0 ]]; then
               echo "${scsi_port} ${channel} ${target} ${hlu_scan}" >> $TMP1
               udev_info=0
               aid_info=0
            else
                echo "ERROR: The hlu value ${hlu_scan} is out of range, please check HBA parameter"
                PP_LOGGER error: The hlu value ${hlu_scan} is out of range, please check HBA parameter
                EXIT
            fi
        else
            echo "Already available, skipping Host:${scsi_port} Channel:${channel} Target:${target} Hlu:${hlu_scan}"
            PP_LOGGER info: Already available, skipping Host:${scsi_port} Channel:${channel} Target:${target} Hlu:${hlu_scan}
        fi

     done < $TMP

    if [[ $hlu_userdef == "" ]]; then
        (( hlu_scan = hlu_scan + 1 ))
    
        if [[ ${udev_info} == 0 ]]; then
            unset udev_info
            UDEV_SETTLE
        fi
    
    else
        hlu_scan=1025
    fi

done

}

ARRAY_ID_SCAN_MAIN()
{

ls -1 -d ${DRIVERS_LOC}/qla* > /dev/null 2>&1
qla=$?

HEADER

if [[ ${noprompt} != "yes" ]]; then
    CLI_QUESTION
fi

if [[ ${adut_message} != "0" ]];then
    adut_message=0
    AUDIT_MESSAGE 
fi   

if [[ ${hba} == "" ]]; then
    ctd_pseudo=$(powermt display dev=all | grep -B 1 -m 1 $array_id |\
    awk -F "=" '/emcpower/ { print $NF }')
    native=$(powermt display dev=$ctd_pseudo | egrep 'active|standby|unlic|iopf|prox|fpin' |\
    grep -v unknown | sed -ne 's:\(.*\) \(sd[a-z]*\) \(.*\):\2:p')
elif [[ ${hba} != "" ]]; then
    
    for hba_userdef in $(echo $hba | sed 's/,/ /g')
    do
        echo ${hba_userdef} | grep "[0-9]-[0-9]"  > /dev/null 2>&1
        
        if [[ $? == 0 ]]; then
            hbanu_1=${hba_userdef%%-*}
            hbanu_2=${hba_userdef##*-}

            if [[ ${hbanu_1} -le ${hbanu_2} ]]; then
                hbarange1=${hbanu_1}
                hbarange2=${hbanu_2}
            else
                hbarange1=${hbanu_2}
                hbarange2=${hbanu_1}
            fi

            for hba_userdef in $(seq $hbarange1 $hbarange2)
            do
               hba_found=no
               if [ -d ${DRIVERS_LOC}/qla*/*/host${hba_userdef} ]; then
                   hba_found=yes
               elif [ -d ${DRIVERS_LOC}/lpfc/*/host${hba_userdef} ]; then
                   hba_found=yes
               elif [ -d ${DRIVERS_LOC}/bfa/*/host${hba_userdef} ]; then
                   hba_found=yes
               elif [ -d ${DRIVERS_LOC}/fnic/*/host${hba_userdef} ]; then
                   hba_found=yes
               elif [ -d ${DRIVERS_LOC}/emcctd/*/host${hba_userdef} ]; then
                   hba_found=yes
               elif [ -f ${SCSI_HOSTS}/host${hba_userdef}/proc_name ]; then
                   iscsi_procname=$(cat ${SCSI_HOSTS}/host${hba_userdef}/proc_name | cut -c 1-5)

                   if [[ ${iscsi_procname} == "iscsi" || ${iscsi_procname} == "fcoe" ]]; then
                       hba_found=yes
                   else 
                       echo "ERROR: HBA ${hba_userdef} not found"
                   fi

                else
                    echo "ERROR: HBA ${hba_userdef} not found"
                fi

                if [[ $hba_found == yes ]]; then
                      echo "${hba_userdef} 0" >> $TMP4
                fi

            done
        
        else
            echo ${hba_userdef} | grep "[0-9]"  > /dev/null 2>&1

            if [[ $? == 0 ]]; then
               hba_found=no
    
                if [ -d ${DRIVERS_LOC}/qla*/*/host${hba_userdef} ]; then
                    hba_found=yes
                elif [ -d ${DRIVERS_LOC}/lpfc/*/host${hba_userdef} ]; then
                    hba_found=yes
                elif [ -d ${DRIVERS_LOC}/bfa/*/host${hba_userdef} ]; then
                    hba_found=yes
                elif [ -d ${DRIVERS_LOC}/fnic/*/host${hba_userdef} ]; then
                    hba_found=yes
                elif [ -d ${DRIVERS_LOC}/emcctd/*/host${hba_userdef} ]; then
                    hba_found=yes
                elif [ -f ${SCSI_HOSTS}/host${hba_userdef}/proc_name ]; then
                    iscsi_procname=$(cat ${SCSI_HOSTS}/host${hba_userdef}/proc_name | cut -c 1-5)

                    if [[ ${iscsi_procname} == "iscsi" || ${iscsi_procname} == "fcoe" ]]; then
                        hba_found=yes
                    else
                        echo "ERROR: HBA ${hba_userdef} not found"
                    fi

                else
                    echo "ERROR: HBA ${hba_userdef} not found"
                fi

                if [[ $hba_found == yes ]]; then
                      echo "${hba_userdef} 0" >> $TMP4
                fi

            else
                echo "ERROR: Incorrect HBA ${hba_userdef} number"
                PP_LOGGER error: Incorrect HBA ${hba_userdef} number
            fi
        
        fi
    
    done
#    cp $TMP4 $TMP
fi
    if [[ $native != "" ]]; then

        for nativectd in $(echo $native)
        do
            if [[ ${tgt} == "" ]]; then
                hctd_native=$(ls -l /sys/block/${nativectd} |  grep -v virtual |  sed s%/block/$nativectd%%g |\
                awk -F '/' '/device/ { print $NF }' | awk -F ":" '{ print $1" "$2" "$3 }')
                if [[ $hctd_native == "" ]];then
                   hctd_native=$(ls -l /sys/block/${nativectd} |  sed s%/block/$nativectd%%g |\
                   awk -F '/' '/device/ { print $NF }' | awk -F ":" '{ print $1" "$2" "$3 }')
                fi
                echo ${hctd_native} >> $TMP
            elif [[ ${tgt} != "" ]]; then
                tgt_nu=${tgt##*=}
                hctd_native=$(ls -l /sys/block/${nativectd} | grep -v virtual | sed s%/block/$nativectd%%g |\
                awk -F '/' '/device/ { print $NF }' | awk -F ":" '{ print $1" "$2 }')
                echo ${hctd_native} >> $TMP4
            fi
        done

    fi

    for tgt_userdef in $(echo ${tgt##*=} | sed 's/,/ /g')
    do
        echo ${tgt_userdef} | grep "[0-9]-[0-9]"  > /dev/null 2>&1

        if [[ $? == 0 ]]; then
            tgtnu_1=${tgt_userdef%%-*}
            tgtnu_2=${tgt_userdef##*-}

            if [[ ${tgtnu_1} -le ${tgtnu_2} ]]; then
                tgtrange1=${tgtnu_1}
                tgtrange2=${tgtnu_2}
            else 
                tgtrange1=${tgtnu_2}
                tgtrange2=${tgtnu_1}
            fi


            for tgt_userdef in $(seq $tgtrange1 $tgtrange2)
            do
                if [[ -f $TMP4 ]]; then

                    if [[ ${tgt_userdef} -le ${MAX_TGT} ]];then
                        while read h c
                        do
                            echo "${h} ${c} ${tgt_userdef}" >> $TMP
                        done < $TMP4
                    else
                        echo "ERROR: Skipping ${tgt_userdef}, tgt is limited upto ${MAX_TGT}"
                    fi

                fi

            done

        else

            echo ${tgt_userdef} | grep "[0-9]"  > /dev/null 2>&1
            if [[ $? == 0 ]]; then

                if [[ -f $TMP4 ]]; then
        
                    if [[ ${tgt_userdef} -le ${MAX_TGT} ]];then
                        while read h c
                        do
                            echo "${h} ${c} ${tgt_userdef}" >> $TMP
                        done < $TMP4
                    else
                        echo "ERROR: Skipping ${tgt_userdef}, tgt is limited upto ${MAX_TGT}"
                    fi

                fi

            else
                echo "ERROR: Incorrect tgt ${tgt_userdef} number"    
                PP_LOGGER error: Incorrect tgt ${tgt_userdef} number
            fi
        fi

    done 
if [[ ! -f $TMP ]]; then
    EXIT
fi

if [[ ${hlu} == "" ]]; then
    unset udev_info
    echo "INFO: Bus scanning limited to 0-20 Hlu"
    HEADER
    ARRAY_ID_SCAN 

    if [[ ${udev_info} == 0 ]]; then
        UDEV_SETTLE
    fi

elif [[ ${hlu%%=*} == "hlu" ]]; then
    lun_nu=${hlu##*=}

    if [[ "$lun_nu" != "" ]]; then
        MAX_LUN=1024

        for hlu_userdef in $(echo ${lun_nu} |  sed 's/,/ /g')
        do
            echo ${hlu_userdef} | grep "[0-9]-[0-9]"  > /dev/null 2>&1

            if [[ $? == 0 ]]; then
                nu_1=${hlu_userdef%%-*}
                nu_2=${hlu_userdef##*-}

                if [[ ${nu_1} -le ${nu_2} ]]; then
                    range1=${nu_1}
                    range2=${nu_2}
                else 
                    range1=${nu_2}
                    range2=${nu_1}
                fi


                for hlu_userdef in $(seq $range1 $range2)
                do
                    unset udev_info
                    ARRAY_ID_SCAN 

                    if [[ ${udev_info} == 0 ]]; then
                        UDEV_SETTLE
                    fi

                done

            else
                echo ${hlu_userdef} | grep "[0-9]"  > /dev/null 2>&1

                if [[ $? == 0 ]]; then
                    unset udev_info 
                    ARRAY_ID_SCAN  

                    if [[ ${udev_info} == 0 ]]; then
                        UDEV_SETTLE
                    fi

                else
                    echo "ERROR: Incorrect hlu ${hlu_userdef} number"
                    PP_LOGGER error: Incorrect hlu ${hlu_userdef} number
                    EXIT
                fi
            fi
    
        done

    fi

else
    USAGE
fi

if [[ ${aid_info} == 0 ]]; then
    PP_ADD_REMOVE add
fi

HEADER

if [[ ${verify_new_lun} == 0 ]];then
    VERIFY_ARRAY_ID_SCAN
fi

#CLEANUP_TMP

if [[ ${fcoe_error} == 1 ]]; then
    unset fcoe_error
    EXIT
fi

}

#-----------------------------------------------------
##    Remove Native Devices
#-----------------------------------------------------
FC_iSCSI_REMOVE()
{

for device_nu in $@
do    
    device_nu=$(basename ${device_nu}) > /dev/null 2>&1
    powermt display dev=${device_nu} > /dev/null 2>&1

    if [[ $? != 0 ]]; then

        if [[ -f /sys/block/${device_nu}/device/delete ]]; then
            vender_name=$(cat /sys/block/${device_nu}/device/vendor)
            vender_name=$(echo ${vender_name})

            case ${vender_name} in
            EMC|DellEMC|DGC|HP|IBM|DEC|COMPAQ|HPQ|HITACHI|NETAPP|XtremIO|COMPELNT)
                device_menu="${device_menu} ${device_nu}"
                echo 1 > /sys/block/${device_nu}/device/delete
            ;;
            *)
                echo "ERROR: ${device_nu} device is not managed by PowerPath or may be dead."
                PP_LOGGER error: The ${device_nu} device is not managed by PowerPath or may be dead
                HEADER
                return 1
            ;;
            esac

        else
            echo "ERROR: Native device ${device_nu} does not exist on the host."
#            echo "Pls use the correct device name"
            PP_LOGGER error: The Native device ${device_nu} does not exist on the host.
            HEADER
            return 1
        fi
     
    else
         echo "ERROR: Native device ${device_nu} is managed by PowerPath, hence cannot be removed."
         return 1
    fi

done

if [[ ${device_menu} != "" ]]; then            
    echo "Deleting native devices: $(echo ${device_menu})"
    PP_LOGGER info: Deleting native devices: $(echo ${device_menu})
fi
unset device_menu

}

#-----------------------------------------------------
##    Add or Remove the device 
#-----------------------------------------------------
PP_ADD_REMOVE()
{

if [[ $1 == add ]]; then

#    if (( ${PP_BASE} == 5 && ${PP_BASE_VER} >=3 )); then

        if [[ $noprompt != yes ]]; then

            while (( 1 ))
            do
                HEADER
                echo "INFO: 'powermt config' is required to recognize the new LUN(s) under PowerPath"
                echo -n "Continue operation? [y,n,q->quit]: "
                read add_action

                case ${add_action} in
                n|no)
                    HEADER
                    echo "INFO: Please execute 'powermt config' command manually to recognize the new LUN(s) under PowerPath"
                    PP_LOGGER info: Please execute 'powermt config' command manually to recognize the new LUN\(s\) under PowerPath
                    break
                ;;
                y|yes)
                    verify_new_lun=0 
                    HEADER
                    echo "INFO: Executing 'powermt config', please wait ..."
                    PP_LOGGER "info: Executing 'powermt config', please wait ..."
                    UDEV_SETTLE
                    powermt config
                    UDEV_SETTLE
                    sleep 2
                    HEADER
                    echo "INFO: Executing 'powermt save'"
                    PP_LOGGER info: Executing 'powermt save'
                    powermt save
                    break
                ;;
                q|quit)
                    EXIT
                ;;
                *)
                    HEADER
                    echo "WARNING: Incorrect input $remove_action"
                ;;
                esac

            done

        else
            verify_new_lun=0 
            HEADER
            echo "INFO: Executing 'powermt config', please wait ..."
            PP_LOGGER "info: Executing 'powermt config', please wait ..."
            UDEV_SETTLE
            powermt config
            UDEV_SETTLE
            sleep 2
            HEADER
            echo "INFO: Executing 'powermt save'"
            PP_LOGGER info: Executing 'powermt save'
            powermt save
        fi

#    else
#
#        HEADER
#        echo "INFO: 'powermt config' will be called automatically, hence not required to run"
#        PP_LOGGER "info: 'powermt config' will be called automatically, hence not required to run"
#
#    fi

elif [[ $1 == remove ]]; then
    unset pp_relese

    Err=0
    AUDIT_MESSAGE    
    if [[ $noprompt != "yes" ]]; then

        for Pseudo in ${remove_dev_name}
        do
            Pseudo=$(basename $Pseudo) > /dev/null 2>&1

            if [[ ${Pseudo} != "" ]]; then
                powermt display dev=${Pseudo} > /dev/null 2>&1

                if [[ $? == 0 ]]; then
                    Pseudo1=$(powermt display dev=$Pseudo | grep Pseudo | awk -F "=" '{ print $NF }')

                    for Pseudo in $Pseudo1; do
                        Pseudo3="$Pseudo3 $Pseudo"
                    
                      #  if [[ $remove_info != 1 ]]; then
                      #      HEADER
                      #      echo "INFO: Following device(s) selected"
                      #      PP_LOGGER info: Following device\(s\) selected
                            remove_info=1
                      #  fi

                      #  FC_iSCSI_NATIVE
                    
                    done

                else
                    HEADER
                    echo "ERROR: The ${Pseudo} device is not under PowerPath control, ignoring device"
                    PP_LOGGER error: The ${Pseudo} device is not under PowerPath control, ignoring device
                    EXIT
                fi

            fi

        done
        
        if [[ $remove_info == 1 ]]; then

            for Pseudo in $Pseudo3; do

                if [[ $remove_info1 != 1 ]]; then
                    HEADER
                    echo "INFO: Following device(s) selected"
                    #PP_LOGGER info: Following device\(s\) selected
                    remove_info1=1
                fi

            FC_iSCSI_NATIVE

            done
        fi

        if [[ $Pseudo3 != "" ]]; then
           remove_dev_name=$Pseudo3
        fi

        if [[ $remove_info == 1 ]]; then 
            while (( 1 )); do
                HEADER
                echo "WARNING: This option invokes 'powermt remove dev=<pseudo>' command. Please proceed with Caution!!!"
                echo -n "Continue operation? [y,q->quit]: "
                read remove_action

                if [[ $remove_action == q || $remove_action == quit ]]; then
                    EXIT 
                fi

                case $remove_action in
                y|yes)
                    break
                ;;
                q|quit)
                    EXIT
                ;;
                *)
                    HEADER
                    echo "WARNING: Incorrect input $remove_action"
                ;;
                esac
            done
        fi
    fi

 #   AUDIT_MESSAGE    
    for Pseudo in ${remove_dev_name}
    do
        Pseudo=$(basename ${Pseudo}) > /dev/null 2>&1

        if [[ ${Pseudo} != all ]]; then
            display_info=1
            powermt display dev=${Pseudo} > /dev/null 2>&1

            if [[ $? == 0 ]]; then
                HEADER
                Pseudo=$(powermt display dev=${Pseudo} | awk -F "=" '/^Pseudo/ { print $NF }')
                FC_iSCSI_NATIVE
                powermt disable_autoconfig
                echo "Executing powermt remove dev=${Pseudo}"
                PP_LOGGER info: Executing powermt remove dev=${Pseudo}
                powermt remove dev=${Pseudo} 

                if [[ $? == 0 ]]; then
                    UDEV_SETTLE
                    FC_iSCSI_REMOVE ${natives}
                    if [[ $? != 0 ]]; then
                        Err=1
                    fi
                    pp_relese=0
                else
                    Err=1
                fi
                powermt enable_autoconfig

            else
                if [[ $noprompt == "yes" ]]; then
                   HEADER
                   echo "ERROR: The ${Pseudo} device is not under PowerPath control, ignoring device"
                   PP_LOGGER error: The ${Pseudo} device is not under PowerPath control, ignoring device
                   HEADER
                   EXIT
                fi
            fi

        else  
            Pseudo=$(powermt display dev=$Pseudo | grep Pseudo | awk -F "=" '{ print $NF }')

            for Pseudo in ${Pseudo}
            do

                HEADER
                FC_iSCSI_NATIVE
                powermt disable_autoconfig
                echo "Executing powermt remove dev=${Pseudo}"
                PP_LOGGER info: Executing powermt remove dev=${Pseudo}
                powermt remove dev=${Pseudo} 

                if [[ $? == 0 ]]; then
                    UDEV_SETTLE
                    FC_iSCSI_REMOVE ${natives}
                    if [[ $? != 0 ]]; then
                        Err=1
                    fi
                    UDEV_SETTLE
                    pp_relese=0    
                else
                    Err=1
                fi
                powermt enable_autoconfig

            done

       fi

    done

    if [[ ${pp_relese} == 0 ]]; then
        HEADER
        echo "INFO: Executing 'powermt release'"
        PP_LOGGER info: Executing 'powermt release'
        powermt release 
        sleep 3
        HEADER
        echo "INFO: Executing 'powermt save'"
        PP_LOGGER info: Executing 'powermt save'
        powermt save
    fi

    if [[ $Err == 1 ]]; then
        HEADER
        EXIT
    fi

fi

}


#-----------------------------------------------------
##  Get the Native Devices Sub-function
#-----------------------------------------------------
FC_iSCSI_NATIVE()
{
    
unset Native
Pseudo=$(echo /dev/${Pseudo} | grep dev | awk -F "/" '{ print $NF }')
powermt display dev=${Pseudo} > /dev/null 2>&1

if [[ $? == 0 ]]; then
    powermt display dev=${Pseudo} | egrep '^VNX|^CLARiiON|^Ess|^XtremIO|^VPLEX|^Unity|^SC' > /dev/null 2>&1
    
    if [[ $? == 0 ]]; then
        dev_name1=$(powermt display dev=${Pseudo} | awk -F "[" '/^Logical/ { print $2 }' |\
                    sed -e 's/\]/ /g')
        length=${#dev_name1}

        if (( ${length} <= 6 )); then
            dev_name="${dev_name1}\t"
        else
            dev_name="${dev_name1}"
        fi
#        dev_name=$(powermt display dev=${Pseudo} | awk -F "[" '/^Logical/ { print $2 }' |\
#                      sed -e 's/\]//g')
        arrayid=$(powermt display dev=${Pseudo} | egrep '^VNX|^CLARiiON|^Ess|^XtremIO|^VPLEX|^Unity|^SC' | awk -F ' |=' '/.*ID/ { print $3 }')
        Array_Device="${arrayid}\t:${dev_name}"
    else
        arrayid=$(powermt display dev=${Pseudo}| grep -B 1 -m 1 ID | awk -F "=" '/.*ID/ { print $NF }')
#        dev_name=$(powermt display dev=${Pseudo}|  awk -F "=" '/^Logical/ { print $NF }')
        dev_name1=$(powermt display dev=${Pseudo} | awk -F "=" '/^Logical/ { print $2 }')
        length=${#dev_name1}

        if (( ${length} <= 10 )); then
            dev_name="${dev_name1}"
        else
            dev_name="${dev_name1:(-10)}"
        fi

        Array_Device="${arrayid}\t:${dev_name}\t"
    fi
    
    natives=$(powermt display dev=${Pseudo} | egrep 'active|standby|unlic|iopf|prox|fpin' |\
    grep -v unknown | sed -ne 's:\(.*\) \(sd[a-z]*\) \(.*\):\2:p')

    for native in $natives
    do
        hctd_native=$(ls -l /sys/block/$native |  grep -v virtual | sed s%/block/$native%%g |\
        awk -F '/' '/device/ { print $NF }' | awk -F ":" '{ print "="$1":"$2":"$3":"$4 }')
        if [[ $hctd_native == "" ]]; then
            hctd_native=$(ls -l /sys/block/$native | sed s%/block/$native%%g |\
            awk -F '/' '/device/ { print $NF }' | awk -F ":" '{ print "="$1":"$2":"$3":"$4 }')
        fi
    
        if [[ $Native != "" ]];then
            Native="${Native} ${native}${hctd_native}"
        else
            Native="${native}${hctd_native}"
        
        fi

    done
    
    if [[ $display_info != 1 ]]; then
        display_info=1
        echo "------------------------------------------------------------------------------------------------"
        printf "Pseudo Dev\t:Array ID\t:Logical Dev\t:Native Dev(s)=SCSI Address\n"
        echo "------------------------------------------------------------------------------------------------"
    fi

    printf "${Pseudo}\t:${Array_Device}\t$(printf ":%-16s%-16s%-16s%-16s\n\t\t\t\t\t\t" ${Native})\n" | sed '/^[ \t]*$/d'
#        printf "${Pseudo}\t Array ID:${Array_Device}\t Native Device(s):${Native}\n"

else
#    echo "ERROR: The ${Pseudo} device is not under PowerPath control, ignoring device"
#    PP_LOGGER error: The ${Pseudo} device is not under PowerPath control
    HEADER
fi

}

#-----------------------------------------------------
##    Get the Native Devices
#-----------------------------------------------------
GET_FC_iSCSI_NATIVE()
{

for Pseudo in $@
do
    Pseudo=$(basename $Pseudo) > /dev/null 2>&1

    if [[ ${Pseudo} != all ]]; then
        Pseudo=$(powermt display dev=${Pseudo} | grep Pseudo | awk -F "=" '{ print $NF }')
        FC_iSCSI_NATIVE
    else 
        echo "ERROR: Invalid option 'all'"
        PP_LOGGER "error: Incorrect usage for the device name 'all'"
        EXIT
    fi

done

}

#-----------------------------------------------------
##    List the all the Devices which is under PowerPath control
#-----------------------------------------------------
DEVICE_LIST()
{
unset count

for arrayid in $(powermt display ports | grep "[ \t].*" | egrep -v "Storage|=|Interface" | awk '{ print $1 }' | sort -u)
do
    echo ""
    HEADER
    powermt display dev=all | grep -m 1 $arrayid
    count=0

    for Pseudo in $(powermt display dev=all | grep -B 1  $arrayid | grep emcpower | awk -F "=" '{ print $NF }')
    do
        (( count = count + 1 ))
    done
    
    powermt display | grep $(powermt display dev=all | grep -m 1 $arrayid | awk '{ print $1 }')| sed "s%count=.*%count=${count}%g"
    powermt display dev=${Pseudo} | egrep '^VNX|^CLARiiON|^Ess|^XtremIO|^VPLEX|^Unity|^SC' > /dev/null 2>&1

    if [[ $? == 0 ]]; then
        device_type="Nice Name"
    else
        device_type="Device ID"
    fi

    echo "----------------------------------------------------------------------------------------"
    printf "Pseudo Dev\t:${device_type}\t:Native Device(s)=SCSI Address\n"
    echo "----------------------------------------------------------------------------------------"
    
    for Pseudo in $(powermt display dev=all | grep -B 1  $arrayid | grep emcpower | awk -F "=" '{ print $NF }')
    do
        natives=$(powermt display dev=${Pseudo} | egrep 'active|standby|unlic|iopf|prox|fpin' |\
        grep -v unknown | sed -ne 's:\(.*\) \(sd[a-z]*\) \(.*\):\2:p')
        unset Native
        
        for native in $natives
        do
            hctd_native=$(ls -l /sys/block/$native | grep -v virtual | sed s%/block/$native%%g |\
            awk -F '/' '/device/ { print $NF }' | awk -F ":" '{ print "="$1":"$2":"$3":"$4 }')
            if [[ $hctd_native == "" ]];then
               hctd_native=$(ls -l /sys/block/$native | sed s%/block/$native%%g |\
               awk -F '/' '/device/ { print $NF }' | awk -F ":" '{ print "="$1":"$2":"$3":"$4 }')
            fi 
            if [[ $Native != "" ]];then
               #Native="${Native}\t${native}${hctd_native}"
                Native="${Native} ${native}${hctd_native}"
            else
                Native="${native}${hctd_native}"
            fi
        done
        
        unset dev_name
        powermt display dev=${Pseudo} | egrep '^VNX|^CLARiiON|^Ess|^XtremIO|^VPLEX|^Unity|^SC' > /dev/null 2>&1
    
        if [[ $? == 0 ]]; then
            dev_name1=$(powermt display dev=${Pseudo} | awk -F "[" '/^Logical/ { print $2 }' |\
                      sed -e 's/\]/ /g')
            length=${#dev_name1}

            if (( ${length} <= 6 )); then
                dev_name="${dev_name1}\t"
            else
               dev_name="${dev_name1}"
            fi 

        else
           dev_name1=$(powermt display dev=${Pseudo} | awk -F "=" '/^Logical/ { print $2 }')
           length=${#dev_name1}

           if (( ${length} <= 10 )); then
               dev_name="${dev_name1}\t"
           else
               dev_name="${dev_name1:(-10)}"
           fi

           # dev_name="$(powermt display dev=${Pseudo}|  awk -F "=" '/^Logical/ { print $NF }')\t"
        fi
        
        if [[ ${Native} == "" ]]; then
            printf "%-5s${Pseudo}\t\t :${dev_name}\t :N/A\n"
        else
            printf "${Pseudo}\t:${dev_name}\t$(printf ":%-16s%-16s%-16s%-16s\n\t\t\t\t" ${Native})\n" | sed '/^[ \t]*$/d'
        fi
    
    done 
    
done

}

#-----------------------------------------------------
##    Usage of the script
#-----------------------------------------------------
USAGE()
{
    
HEADER
echo ""
echo "Usage: "
echo ""
echo "${SCRIPT_NAME} <operation>"
echo ""
echo "operations:"
echo "   (s)can hba [-noprompt]"
echo "   (s)can iscsi [-noprompt]"
echo "   (s)can open-fcoe [-noprompt]"
echo "   (s)can all [-noprompt]"
echo "   (s)can {<#> [<#>]} [-noprompt]"
echo "   (s)can aid=<Array ID#> [tgt=<#>] [hlu=<#>] [-noprompt]"
echo "   (s)can hba=<#> tgt=<#> [hlu=<#>] [-noprompt]"
echo "   (r)emove <pseudo-list>|all [-noprompt]"
echo "   (l)ist [<pseudo-list>] [default: all]"
echo "   (h)elp|-h [operation]"
echo ""
echo "If no operation is given, runs in interactive mode."
echo ""
HEADER

}

#-------------------------------------------------------
##    Check the OS VERSION and PowerPath Installation 
#-------------------------------------------------------
# During PowerPath Linux NDU, avoid any PowerPath user commands being 
# executed by checking NDU_LOCK_FILE marker file created.
if [[ -f ${NDU_LOCK_FILE} ]]; then
    echo "---------------------------------------------------------"
    echo "Warning: PowerPath non disruptive upgrade is in progress."
    echo "Please retry after the upgrade is complete."
    echo "---------------------------------------------------------"
    EXIT
fi
 
if [[ $(whoami) != root ]]; then
    echo "ERROR: Insufficient user privilege."
    PP_LOGGER error: Insufficient user privilege \(user ID real=$(id -u) effective=$(id -g)\)
    EXIT
fi

if [[ -f ${REDHAT} ]]; then
    cat ${REDHAT} | grep "XenServer release 6" > /dev/null 2>&1

    if [[ $? == 0 ]]; then
        VERSION=RHEL6
    else 
        VERSION=RHEL$(cat ${REDHAT} | sed 's/^.*release \([0-9]\)[0-9.]*.*/\1/') > /dev/null 2>&1
    fi

elif [[ -f ${SLES} ]]; then
    VERSION=SLES$(cat ${SLES} | grep VERSION | awk '{ print $NF }') > /dev/null 2>&1
fi

lsmod | grep -w emcp > /dev/null 2>&1
emcp_mod=$?

if [[ ${emcp_mod} != 0 ]]; then
    rpm -qa | grep EMCpower.LINUX > /dev/null 2>&1    
    pp_inst=$?

    if [[ ${pp_inst} != 0 ]]; then
        echo "ERROR: PowerPath is not installed"
        PP_LOGGER error: PowerPath is not installed
    else 
        echo "ERROR: PowerPath is not running"
        PP_LOGGER error: PowerPath is not running
    fi

    EXIT

fi

#PP_BASE=$(powermt version | cut -c 39)
#PP_BASE_VER=$(powermt version | cut -c 41)

CLEANUP_TMP

CLI_QUESTION()
{

while ((1)); do
    echo "WARNING: Dell recommends that all I/O on the SCSI devices should be quiesced prior to attempting to rescan the SCSI bus."
    echo -n "Continue operation? [y,q->quit]: "
    read scan_action

    case $scan_action in
    y|yes)
        HEADER
        break
    ;;
    q|quit)
        EXIT
    ;;
    *)
        HEADER
        echo "WARNING: Incorrect input $scan_action"
    ;;
    esac

done

}
#-----------------------------------------------------
##    CLI MAIN
#-----------------------------------------------------
case $1 in
s|scan)

    cli=0
    case $2 in
    h|hba)

        for noprompt in $@
        do

            if [[ $noprompt == "-no" || $noprompt == "-noprompt" ]]; then
                noprompt=yes
            fi

        done
        HEADER    

        if [[ $noprompt != "yes" ]]; then
            CLI_QUESTION
        fi

        ACTION=$*
        AUDIT_MESSAGE    
        SCAN_LOG before 
        FC_SCAN

        if [[ ${fc_info} == 0 ]]; then
            PP_ADD_REMOVE add
        fi

        HEADER    

        if [[ ${verify_new_lun} == 0 ]];then
            SCAN_LOG after
        fi

        CLEANUP_TMP

        if [[ ${fc_error} == 1 ]]; then
            EXIT
        fi

    ;;
    i|iscsi)

        for noprompt in $@
        do

            if [[ ${noprompt} == "-no" || ${noprompt} == "-noprompt" ]]; then
                noprompt=yes
            fi

        done

        HEADER    
        
        if [[ ${noprompt} != "yes" ]]; then
            CLI_QUESTION
        fi
        
        ACTION=$*
        AUDIT_MESSAGE    

        SCAN_LOG before
        iSCSI_SCAN

        if [[ ${iscsi_info} == 0 ]]; then
            PP_ADD_REMOVE add
        fi

        HEADER    

        if [[ ${verify_new_lun} == 0 ]];then
            SCAN_LOG after
        fi
        
        CLEANUP_TMP

        if [[ ${iscsi_error} == 1 ]]; then
            EXIT
        fi

    ;;
    o|"open-fcoe"|open)

        for noprompt in $@
        do

            if [[ ${noprompt} == "-no" || ${noprompt} == "-noprompt" ]]; then
                noprompt=yes
            fi

        done

        HEADER    
        
        if [[ ${noprompt} != "yes" ]]; then
            CLI_QUESTION
        fi
        
        ACTION=$*
        AUDIT_MESSAGE    
        SCAN_LOG before
        OPEN_FCOE_SCAN

        if [[ ${fcoe_info} == 0 ]]; then
            PP_ADD_REMOVE add
        fi

        HEADER    

        if [[ ${verify_new_lun} == 0 ]];then
            SCAN_LOG after
        fi

        CLEANUP_TMP

        if [[ ${fcoe_error} == 1 ]]; then
            EXIT
        fi

    ;;
    hba=*)
    
        for noprompt in $@
        do

            if [[ ${noprompt} == "-no" || ${noprompt} == "-noprompt" ]]; then
                noprompt=yes
            fi

        done

        if [[ ${2##*=} != "" && ${2%%=*} == hba ]]; then
        
            hba=${2##*=}

        else
            HEADER
            echo "ERROR: hba=<#> is required." 
            EXIT
        fi
            
        if [[ ${3%%=*} == "tgt" && ${3##*=} != "" && ${4%%=*} == "hlu" ]]; then
            tgt=${3}
            hlu=${4}
        elif [[ ${3%%=*} == "hlu" && ${4%%=*} == "tgt" && ${4##*=} != "" ]]; then
            tgt=${4}
            hlu=${3}
        elif [[ ${3%%=*} == "tgt" && ${3##*=} != "" ]]; then
            tgt=${3}
        elif [[ ${3%%=*} != "tgt" ]]; then
            HEADER
            echo "ERROR: tgt=<#> is required."
            EXIT
        else
            HEADER
            echo "ERROR: tgt=<#> is required."
            EXIT
        fi

        if [[ $tgt == "tgt" || $tgt == "" ]]; then
            HEADER
            echo "ERROR: tgt=<#> is required."
            EXIT
        fi
        
        ACTION=$*
        SCAN_LOG before
        ARRAY_ID_SCAN_MAIN
        HEADER
        SCAN_LOG after
        CLEANUP_TMP

    ;;
    aid=*)
    
        for noprompt in $@
        do

            if [[ ${noprompt} == "-no" || ${noprompt} == "-noprompt" ]]; then
                noprompt=yes
            fi

        done

        array_id=${2##*=}
        
        if [[ ${3%%=*} == tgt && ${4%%=*} == hlu ]]; then
            tgt=${3}
            hlu=${4}
        elif [[ ${3%%=*} == hlu && ${4%%=*} == tgt ]]; then
            tgt=${4}
            hlu=${3}
        elif [[ ${3%%=*} == hlu ]]; then
            hlu=${3}
        elif [[ ${3%%=*} == tgt ]]; then
            tgt=${3}
        fi
        
        ACTION=$*
        
        if [[ $array_id == "" ]]; then
            echo "ERROR: Please provide Array ID"
            PP_LOGGER error: Please provide Array ID
            EXIT
        fi

        powermt display ports | egrep -v "Storage|=|Interface" | awk '{ print $1 }' |\
        grep "^[ \t]*${array_id}$"  > /dev/null 2>&1

        if [[ $? != 0 ]]; then
            echo "ERROR: Array id:${array_id} is not managed by PowerPath"
            PP_LOGGER error: Array id:${array_id} is not managed by PowerPath
            EXIT
        fi

        SCAN_LOG before
        ARRAY_ID_SCAN_MAIN
        HEADER
        SCAN_LOG after
        CLEANUP_TMP

    ;;
    a|all)

        for noprompt in $@
        do

            if [[ ${noprompt} == "-no" || ${noprompt} == "-noprompt" ]]; then
                noprompt=yes
            fi

        done

        HEADER    

        if [[ ${noprompt} != "yes" ]]; then
            CLI_QUESTION
        fi

        ACTION=$*
        AUDIT_MESSAGE    
        SCAN_LOG before
        FC_SCAN
        HEADER
        iSCSI_SCAN
        HEADER    
        OPEN_FCOE_SCAN

        if [[ ${fc_info} == 0 || ${iscsi_info} == 0 || ${fcoe_info} == 0 ]]; then
            PP_ADD_REMOVE add
        fi

        HEADER    

        if [[ ${verify_new_lun} == 0 ]];then
            SCAN_LOG after
        fi
        
        CLEANUP_TMP

        if [[ ${fc_error} == 1 || ${iscsi_error} == 1 || ${fcoe_error} == 1 ]]; then
            EXIT
        fi

    ;;
    *)
        if [[ $(echo $2 | sed -e 's/\-noprompt//g' -e 's/\-no//g') != "" ]]; then
            
            for noprompt in $@
            do

                if [[ ${noprompt} == "-no" || ${noprompt} == "-noprompt" ]]; then
                    noprompt=yes
                fi

            done

            HEADER    

            if [[ ${noprompt} != "yes" ]]; then
                CLI_QUESTION
            fi

            ACTION=$*
            AUDIT_MESSAGE    

            shift
            SCAN_LOG before
            FC_SCAN $(echo $@ | sed -e 's/\-noprompt//g' -e 's/\-no//g')

            if [[ ${fc_info} == 0 ]]; then
                PP_ADD_REMOVE add
            fi
        
            HEADER    

            if [[ ${verify_new_lun} == 0 ]];then
                SCAN_LOG after
            fi

            CLEANUP_TMP

            if [[ ${fc_error} == 1 ]]; then
                EXIT
            fi

        else
            USAGE    
        fi
    ;;
    esac
;;
r|remove)

    cli=0
    
    for noprompt in $@
    do

        if [[ ${noprompt} == "-no" || ${noprompt} == "-noprompt" ]]; then
            noprompt=yes
        fi

    done
    
    ACTION=$*

    if [[ $noprompt == yes ]]; then

        if (( $# >= 3 )); then
           shift
           remove_dev_name=$(echo $@ | sed -e 's/\-noprompt//g' -e 's/\-no//g')
           PP_ADD_REMOVE remove 
           HEADER
        else
           USAGE
        fi
    else
        if (( $# >= 2 )); then
           shift
           remove_dev_name=$(echo $@ | sed -e 's/\-noprompt//g' -e 's/\-no//g')
           PP_ADD_REMOVE remove
           HEADER
        else
           USAGE
        fi
    fi

;;
l|list)
    cli=0
    ACTION=$*
    AUDIT_MESSAGE    

    case $2 in
    ""|a|all)
        DEVICE_LIST 
        HEADER
    ;;
    *)

        if (( $# >= 2 )); then    
            shift
            GET_FC_iSCSI_NATIVE $@
            HEADER
        else
            USAGE
        fi

    ;;
    esac
;;
"-h"|help)
   case $2 in
   s|scan)
       help_scan ()
       {
       HEADER
       echo ""
       echo "scan - Scan the bus and configures new device(s) under PowerPath."
       echo ""
       echo "Usage:"
       echo ""
       echo "${SCRIPT_NAME} (s)can hba [-noprompt]"
       echo ""
       echo "   hba"
       echo "       Performs the scanning on all available HBA(s)"
       echo "       Supported adapters are Qlogic, Emulex, Cisco and Brocade HBA(s)"
       echo "       Including Emulex and Qlogic FCoE CNA(s) and also Qlogic iSCSI HBA(s)"
       echo ""
       echo ""
       echo "${SCRIPT_NAME} (s)can iscsi [-noprompt]"
       echo ""
       echo "   iscsi"
       echo "       Performs the scanning on all available iSCSI Software initiator session(s)"
       echo ""
       echo ""
       echo "${SCRIPT_NAME} (s)can open-fcoe [-noprompt]"
       echo ""
       echo "   open-fcoe"
       echo "       Performs the scanning on all available Open-FCoE Software initiator session(s)"
       echo ""
       echo ""
       echo "${SCRIPT_NAME} (s)can all [-noprompt]"
       echo ""
       echo "   all"
       echo "       Performs the scanning on all available HBA(s) and Software initiator(s)"
       echo ""
       echo ""
       echo "${SCRIPT_NAME} (s)can {<#> [<#>]} [-noprompt]"
       echo ""
       echo "   <# [#]>"
       echo "       Performs the scanning on specific available SCSI Host number(s)"
       echo "       This option is not supported on Software initiator(s)"
       echo "       Example: 2 3 4"
       echo ""
       echo ""
       echo "${SCRIPT_NAME} (s)can aid=<Array ID#> [tgt=<#>] [hlu=<#>] [-noprompt]"
       echo ""
       echo "   aid=<Array ID#> [tgt=<#>] [hlu=<#>]"
       echo "       Limits the scanning to the specified Storage system(Array ID) and its SCSI Target(s)"
       echo "       Discovers and scans SCSI Host and Target number(s) for a Storage system (Array ID)"
       echo "       All other options are optional [SCSI Target number(s) and HLU(s)]"
       echo "       The default hlu value is 0-20 If hlu option is not specified"
       echo "       Note: To use this option at least one LUN from <Array ID#> should be in PowerPath control"
       echo "       Example: aid=FCNCH090201457 [tgt=3,7-9,2] [hlu=3,7-9]"  
       echo ""
       echo ""
       echo "${SCRIPT_NAME} (s)can hba=<#> tgt=<#> [hlu=<#>] [-noprompt]"
       echo ""
       echo "   hba=<#> tgt=<#> [hlu=<#>]"
       echo "       Limits the scanning to the specified SCSI Host number(s), Target(s), HLU(s)"
       echo "       Note: SCSI Target number(s) is mandatory and HLU number(s) is optional"
       echo "       The default hlu value is 0-20 If hlu option is not specified"
       echo "       Example: hba=1,2,4-7 tgt=3,7-9,2 [hlu=3,7-9]"
       echo ""
       echo ""
       echo "[-noprompt]"
       echo "   Suppresses the default confirmation that appears when you"
       echo "   run a command. The abbreviation for this argument is '-no'"
       echo ""
       HEADER
       }
       help_scan | more 
   ;;
   r|remove)
       help_remove ()
       {
       HEADER
       echo ""
       echo "remove - Remove Pseudo device(s)."
       echo ""
       echo "Usage:"
       echo ""
       echo "${SCRIPT_NAME} (r)emove <pseudo-list>|all [-noprompt]"
       echo ""
       echo "   <pseudo-list>"
       echo "      Dynamic removal of Pseudo device(s) and associated Native device(s)"
       echo "      This option removes the Pseudo device(s) only if Pseudo device is not in use"
       echo "      Example: emcpowerX emcpowerY ..."
       echo ""
       echo "   all"
       echo "      Remove all Pseudo devices and associated Native device(s)"
       echo "      This option removes the Pseudo device(s) only if Pseudo device is not in use"
       echo ""
       echo "   [-noprompt]"
       echo "      Suppresses the default confirmation that appears when you"
       echo "      run a command. The abbreviation for this argument is '-no'"
       echo ""
       HEADER
       }
       help_remove | more 
   ;;
   l|list)
       help_list ()
       { 
       HEADER
       echo ""
       echo "list - List Pseudo device(s)."
       echo ""
       echo "Usage:"
       echo ""
       echo "${SCRIPT_NAME} (l)ist [<pseudo-list>] [default: all]"
       echo ""
       echo "   list [pseudo-list]"
       echo "       List Pseudo device(s) and corresponding details [default: all]"
       echo "       Example: list emcpowerX emcpowerY . ."
       echo ""
       HEADER
       }
       help_list | more
   ;;
   *|-h|help)
       USAGE | more
   ;;
   esac
;;
*)
if [[ $# == 0 ]]; then
#-----------------------------------------------------
##    MENU DRIVEN      #INTERACTIVE MODE 
#-----------------------------------------------------
clear
while (( 1 ))
do
    menu=0
    unset main_menu_action
    unset main_menu_action1
    HEADER
    echo "(s)can               : Scan the bus"
    echo "(r)emove             : Remove Pseudo device(s)"
    echo "(l)ist [pseudo-list] : List Pseudo device(s) [default: all]"
    HEADER
    echo -n  "Main-Menu: Enter your choice: [s,r,l,help,q->quit] "
    read main_menu_action
    
    main_menu_action1=$(echo ${main_menu_action} | awk '{ print $1 }')

    case $main_menu_action1 in
    scan|s)

        while (( 1 ))
        do
            unset sub_menu_action
            unset sub_menu_action1
            unset sub_menu_action2
            unset fc_info
            unset verify_new_lun
            unset iscsi_info
            unset fcoe_info
            unset iscsi_fcoe_info
            unset display_info
            HEADER
            echo "(h)ba                : Scan all HBA(s)[Qlogic, Emulex, Cisco & Brocade adapters]"        
            echo "(i)scsi              : Scan all iSCSI Software initiator session(s)"
            echo "(o)pen-fcoe          : Scan all Open-FCoE Software initiator session(s)"
            echo "(a)ll                : Scan all HBA(s) and Software initiator(s)"
            echo "<# [#]>              : Scan specific SCSI Host number(s), e.g. 2 3 4"
            echo "aid=<Array ID#>      : Scan specific Array ID, Target(s), HLU(s)"
            echo "hba=<#>              : Scan specific SCSI Host number(s), Target(s), HLU(s)"
            echo "(l)ist [pseudo-list] : List Pseudo device(s) [default: all]"
            HEADER
            echo -n  "Sub-Menu: Enter your choice: [h,i,o,a,<# [#]>,aid=<#>,hba=<#>,l,help,m->Main-Menu,q->quit] "
            read sub_menu_action
            
            sub_menu_action1=$(echo $sub_menu_action | awk '{ print $1 }')

            case $sub_menu_action1 in
            hba|h)

                HEADER
                CLI_QUESTION
                ACTION="scan ${sub_menu_action}"
                AUDIT_MESSAGE    
                SCAN_LOG before
                FC_SCAN

                if [[ ${fc_info} == 0 ]]; then
                    PP_ADD_REMOVE add
                fi
                
                HEADER    

                if [[ ${verify_new_lun} == 0 ]];then
                    SCAN_LOG after
                fi
                
                CLEANUP_TMP
            ;;
            aid|aid=*)
                unset array_id
                unset adut_message
                array_id=${sub_menu_action1##*=}


                if [[ ${array_id} == q || ${array_id} == quit ]]; then
                    EXIT 
                fi

                if [[ $array_id != "" ]]; then
                powermt display ports | egrep -v "Storage|=|Interface" | sed '/^[ \t]*$/d' | awk '{ print $1 }' |\
                grep "^[ \t]*${array_id}$"  > /dev/null 2>&1
                array_found=$?
                
                fi

                if [[ ${array_found} != 0 ]]; then
                    HEADER
                    unset array_id
                    echo -n "Please provide the Array ID: [<Array ID#>,q->quit][press 'Enter' to display] "
                    read array_id
                
                    if [[ ${array_id} == "" ]]; then
                       HEADER
                       echo "INFO: Following Array ID found on the host"
                       powermt display ports | egrep -v "Storage|=|Interface" | sed '/^[ \t]*$/d' | awk '{ print $1 }' | sort -u
                       HEADER
                       echo -n "Please provide the Array ID: [<Array ID#>,q->quit] "
                       read array_id
                    fi

                    if [[ ${array_id} == q || ${array_id} == quit ]]; then
                        EXIT 
                    fi
                    powermt display ports | egrep -v "Storage|=|Interface" | sed '/^[ \t]*$/d' | awk '{ print $1 }' |\
                    grep "^[ \t]*${array_id}$"  > /dev/null 2>&1

                    if [[ $? != 0 ]]; then
                       HEADER
                       echo "ERROR: Array id:${array_id} is not managed by PowerPath"
                       continue
                    fi
                fi

                HEADER
                unset tgt
                echo -n "Please provide tgt number(s): [<#>|<#-#>|<#>,<#-#>, q->quit][default: Targets of ${array_id}][optional] "
                read tgt

                if [[ ${tgt} == q || ${tgt} == quit ]]; then
                    EXIT 
                fi

                HEADER
                unset hlu
                echo -n "Please provide Hlu number: [<#>|<#-#>|<#>,<#-#> ,q->quit][default: 0-20][optional] "
                read hlu
                if [[ ${hlu} == q || ${hlu} == quit ]]; then
                    EXIT 
                fi

                if [[ ${hlu} == "" && ${tgt} == "" ]]; then
                    ACTION="scan aid=${array_id}"
                elif [[ ${hlu} != "" && ${tgt} != "" ]]; then 
                    ACTION="scan aid=${array_id} tgt=${tgt} hlu=${hlu}"
                elif [[ ${hlu} != "" && ${tgt} == "" ]]; then
                    ACTION="scan aid=${array_id} hlu=${hlu}"
                elif [[ ${hlu} == "" && ${tgt} != "" ]]; then
                    ACTION="scan aid=${array_id} tgt=${tgt}"
                fi

                if [[ $tgt != "" ]]; then
                    tgt="tgt=$tgt"
                fi

                if [[ $hlu != "" ]]; then
                    hlu="hlu=$hlu"
                fi
                SCAN_LOG before
                ARRAY_ID_SCAN_MAIN
                HEADER
                SCAN_LOG after
                CLEANUP_TMP

            ;;
            hba=*)
                 unset hba
                 hba=${sub_menu_action1##*=}
                 if [[ ${hba} != "" ]]; then
                    HEADER
                    unset tgt
                    echo -n "Please provide tgt number(s): [<#>|<#-#>|<#>,<#-#>, q->quit] "
                    read tgt
                    if [[ ${tgt} == q || ${tgt} == quit ]]; then
                        EXIT 
                    fi
                    if [[ $tgt == "" ]]; then
                        HEADER
                        echo "ERROR: Invalid option"
                        continue
                    fi
                    HEADER
                    unset hlu
                    echo -n "Please provide Hlu number: [<#>|<#-#>|<#>,<#-#> ,q->quit][default: 0-20][optional] "
                    read hlu
                    if [[ ${hlu} == q || ${hlu} == quit ]]; then
                        EXIT 
                    fi

                if [[ ${hlu} != "" && ${tgt} != "" ]]; then 
                    ACTION="scan hba=${hba} tgt=${tgt} hlu=${hlu}"
                elif [[ ${hlu} == "" && ${tgt} != "" ]]; then
                    ACTION="scan hba=${hba} tgt=${tgt} hlu=0-20"
                fi
                tgt="tgt=$tgt"
                if [[ $hlu != "" ]]; then
                    hlu="hlu=$hlu"
                fi
                SCAN_LOG before
                ARRAY_ID_SCAN_MAIN
                HEADER
                SCAN_LOG after
                else
                    HEADER
                    echo "ERROR: Invalid option"
                fi

                CLEANUP_TMP
            ;;
            iscsi|i)
      #          unset display_info
                HEADER
                CLI_QUESTION
                ACTION="scan ${sub_menu_action}"
                AUDIT_MESSAGE    
                SCAN_LOG before
                iSCSI_SCAN

                if [[ ${iscsi_info} == 0 ]]; then
                    PP_ADD_REMOVE add
                fi

                HEADER    

                if [[ ${verify_new_lun} == 0 ]];then
                    SCAN_LOG after
                fi
                
                CLEANUP_TMP

            ;;
            "open-fcoe"|o|open)
#                unset display_info
                HEADER
                CLI_QUESTION
                ACTION="scan ${sub_menu_action}"
                AUDIT_MESSAGE    
                SCAN_LOG before
                OPEN_FCOE_SCAN

                if [[ ${fcoe_info} == 0 ]]; then
                    PP_ADD_REMOVE add
                fi

                HEADER    

                if [[ ${verify_new_lun} == 0 ]];then
                    SCAN_LOG after
                fi

                CLEANUP_TMP

            ;;
            all|a)
#                unset display_info
                HEADER
                CLI_QUESTION
                ACTION="scan ${sub_menu_action}"
                AUDIT_MESSAGE    
                SCAN_LOG before
                FC_SCAN
                HEADER
                iSCSI_SCAN
                HEADER    
                OPEN_FCOE_SCAN

                if [[ ${fc_info} == 0 || ${iscsi_info} == 0 || ${fcoe_info} == 0 ]]; then
                    PP_ADD_REMOVE add
                fi
                
                HEADER    

                if [[ ${verify_new_lun} == 0 ]];then
                    SCAN_LOG after
                fi
                    
                CLEANUP_TMP

            ;;
            list|l)
      #          unset display_info
                ACTION="${sub_menu_action}"
                AUDIT_MESSAGE    
                sub_menu_action2=$(echo ${sub_menu_action} | sed -e 's/list//g' -e 's/\<l\>//g')

                if [[ ${sub_menu_action2} != "" ]]; then

                    case ${sub_menu_action2} in
                    all)
                        DEVICE_LIST 
                    ;;
                    *)
                        unset display_info
                        GET_FC_iSCSI_NATIVE ${sub_menu_action2}
                    ;;
                    esac
                    
                else
                    DEVICE_LIST
                fi

            ;;
            help)
            SubMenu_scan_help ()
            {
            HEADER
            echo "Sub-Menu scan help"
            echo ""
            echo "hba"
            echo "    Performs the scanning on all available HBA(s)"
            echo "    Supported adapters are Qlogic, Emulex, Cisco and Brocade HBA(s)"
            echo "    Including Emulex and Qlogic FCoE CNA(s) and also Qlogic iSCSI HBA(s)"
            echo ""
            echo "iscsi"
            echo "    Performs the scanning on all available iSCSI Software initiator session(s)"
            echo ""
            echo "open-fcoe"
            echo "    Performs the scanning on all available Open-FCoE Software initiator session(s)"
            echo ""
            echo "all"
            echo "    Performs the scanning on all available HBA(s) and Software initiator(s)"
            echo ""
            echo "<# [#]>"
            echo "    Performs the scanning on specific available SCSI Host number(s)"
            echo "    This option is not supported on Software initiator(s)"
            echo "    Example: 2 3 4"
            echo ""
            echo "aid=<Array ID#>"
            echo "    Limits the scanning to the specified Storage system(Array ID) and its SCSI Target(s)"
            echo "    Discovers and scans SCSI Host and Target number(s) for a Storage system (Array ID)"
            echo "    All other options are optional [SCSI Target number(s) and HLU(s)"
            echo "    The default hlu value is 0-20 If hlu option is not specified"
            echo "    Note: To use this option at least one LUN from <Array ID#> should be in PowerPath control"
            echo ""
            echo "hba=<#>"
            echo "    Limits the scanning to the specified SCSI Host number(s), Target(s), HLU(s)"
            echo "    Note: SCSI Target number(s) is mandatory and HLU number(s) is optional"
            echo "    The default hlu value is 0-20 If hlu option is not specified"
            echo "    Accepted values: hba=1,2,4-7"
            echo ""
            echo "list [pseudo-list]"
            echo "    List Pseudo device(s) and corresponding details [default: all]"
            echo "    Example: list emcpowerX emcpowerY . ."
            echo ""
            echo "help"
            echo "    Displays help menu"
            echo ""
            }
             SubMenu_scan_help | more 

            ;;
            q|quit)
                EXIT
            ;;
            m|Main-Menu)
                break
            ;;
            *)
                if [[ $sub_menu_action1 != "" ]]; then
                HEADER
                CLI_QUESTION
                ACTION="scan ${sub_menu_action}"
                AUDIT_MESSAGE    
                SCAN_LOG before

                for host_id in ${sub_menu_action} ; do

                    if [[ ${host_id} -le 199 ]]; then
                        FC_SCAN ${host_id}
                    fi

                done
                    
                if [[ ${fc_info} == 0 || ${iscsi_fcoe_info} == 0 ]]; then
                    PP_ADD_REMOVE add
                fi

                if [[ ${verify_new_lun} == 0 ]];then
                    SCAN_LOG after
                fi

                CLEANUP_TMP
            #    HEADER
             
                else
                    continue
                fi
            ;;
            esac
        done
    ;;
    remove|r)
        while (( 1 ))
        do
            unset sub_menu_action
            unset sub_menu_action1
            unset sub_menu_action2
            HEADER
            echo "<pseudo-list>        : Remove Pseudo device(s) e.g. emcpowerX [emcpowerY ......]"
            echo "all                  : Remove all Pseudo devices"
            echo "(l)ist [pseudo-list] : List Pseudo device(s) [default: all]"
            HEADER
            echo "WARNING: This option invokes 'powermt remove dev=<pseudo>' command. Please proceed with Caution!!!"
            HEADER
            echo -n  "Sub-Menu: Enter your choice: [<pseudo-list>,all,l,help,m->Main-Menu,q->quit] "
            read sub_menu_action

            if [[ ${sub_menu_action} == q || ${sub_menu_action} == quit ]]; then
                EXIT 
            fi

            DEVICE_NAME=$(echo /dev/${sub_menu_action} | grep dev | awk -F "/" '{ print $NF }' | cut -c 1-3) > /dev/null 2>&1

            if [[ ${DEVICE_NAME} == "emc" || ${DEVICE_NAME} == "all" ]]; then
                echo -n "Are you sure want to continue : [y,n] "
                read sub_menu_action1

                if [[ ${sub_menu_action1} == n ]]; then
                    continue
                elif [[ ${sub_menu_action1} == y ]]; then
                    ACTION="remove ${sub_menu_action}"
                    remove_dev_name=${sub_menu_action}
                    noprompt=yes
                    PP_ADD_REMOVE remove
                    unset noprompt
                else
                    HEADER
                    echo "WARNING: Incorrect input"
                fi

            elif [[ ${sub_menu_action%%[ ]*} == "l"  ||  ${sub_menu_action%%[ ]*} == "list" ]]; then
                sub_menu_action2=$(echo ${sub_menu_action} | sed -e 's/list//g' -e 's/\<l\>//g')
                ACTION="${sub_menu_action}"
                AUDIT_MESSAGE    

                if [[ ${sub_menu_action2} != "" ]]; then
                    
                    case $sub_menu_action2 in
                    all)
                        DEVICE_LIST 
                    ;;
                    *)
                #        HEADER
                        unset display_info
                        GET_FC_iSCSI_NATIVE ${sub_menu_action2}
                    ;;
                    esac

                else
                    DEVICE_LIST
                fi

            elif [[ ${sub_menu_action} == "m" || ${sub_menu_action} == "Main-Menu" ]]; then
                break
            elif [[ ${sub_menu_action} == "help" ]]; then
                SubMenu_remove_help ()
                {
                HEADER
                echo "Sub-Menu remove help"
                echo ""
                echo "<pseudo-list>"
                echo "   Dynamic removal of Pseudo device(s) and associated Native device(s)"
                echo "   This option removes the Pseudo device(s) only if Pseudo device is not in use" 
                echo "   Example: emcpowerX emcpowerY ..."
                echo ""
                echo "all"
                echo "   Remove all Pseudo devices and associated Native device(s)"
                echo "   This option removes the Pseudo device(s) only if Pseudo device is not in use" 
                echo ""
                echo "list [pseudo-list]"
                echo "   List Pseudo device(s) and corresponding details [default: all]"
                echo "   Example: list emcpowerX emcpowerY . ."
                echo ""
                echo "help"
                echo "   Displays help menu"
                echo ""
                }
                SubMenu_remove_help | more 

            else
                HEADER
                echo "WARNING: Incorrect input"
            fi

        done
    ;;
    list|l)
        main_menu_action1=$(echo ${main_menu_action} | sed -e 's/list//g' -e 's/\<l\>//g')
        ACTION="${main_menu_action}"
        AUDIT_MESSAGE    

        if [[ ${main_menu_action1} != "" ]]; then
            
            case ${main_menu_action1} in
            all)
                DEVICE_LIST 
            ;;
            *)
                unset display_info
                GET_FC_iSCSI_NATIVE ${main_menu_action1}
            ;;
            esac

        else
            DEVICE_LIST
        fi

    ;;
    q|quit)
        EXIT
    ;;
    help)
        MainMenu_help ()
        {
        HEADER
        echo "Main-Menu help"
        echo ""
        echo "scan"
        echo "    Bus scanning for Dynamic LUNs Addition"
        echo ""
        echo "remove"
        echo "    Dynamic removal of Pseudo device(s) and associated Native device(s)"
        echo ""
        echo "list [pseudo-list]"
        echo "    List Pseudo device(s) and corresponding details [default: all]"
        echo "    Example: list emcpowerX emcpowerY . ."
        echo ""
        echo "help"
        echo "    Displays help menu"
        echo ""
        }
        MainMenu_help | more 
    ;;
    esac

done

else
    USAGE
fi
;;
esac
#-----------------------------------------------------
##    END
#-----------------------------------------------------
##########################
