#!/bin/sh
#
# $Id$
#
# Script:	pgr_stat
#
# Description:	PGR3 reservation status for a pool.
#
# Platform:	Unix
#
# Author:	High-Availability.Com Ltd
#
. /opt/HAC/bin/rsf.sh

script="`basename $0`"

if [ $# -ne 1 ]; then
    echo "Usage: ${script} <pool>" >&2
    exit 1
fi

service=$1

# Function to take output from 'zpool import/status' and create output containing all drives
# associated with a pool ($1).
# Input taken from stdin
find_pool_drives(){
    service=$1
    CURRENT_POOL=
    DRIVE_LINES_REACHED=false
    while read firstword line;do                                         # Loop through each line of input
        if [ $DRIVE_LINES_REACHED = true ];then                         # Reached lines with drive names
            if [ "$firstword" = "" ];then                               # Reached blank line - end of drive lines
                var=${CURRENT_POOL}_DRIVES
                DRIVE_LINES_REACHED=false
                CURRENT_POOL=
            elif [ "$CURRENT_POOL" = "${service}" ];then
                if expr "${firstword}" : 'c..*t..*d..*' > /dev/null; then
                    echo "/dev/rdsk/${firstword}s0"
                fi
            fi
        elif [ "$firstword" = "pool:" ];then                            # Start of first/next pool (pool: poolname)
            if [ "$service" = "$line" ];then
                CURRENT_POOL="$line"
            fi
        elif [ "$firstword" = "$CURRENT_POOL" -a "$CURRENT_POOL" != "" ];then  # This is the line before the drive names
            DRIVE_LINES_REACHED=true
        fi
    done                                                                 # End of stdin
}

zpool status -x ${service} >/dev/null 2>&1
[ $? -eq 0 ] && ZPOOLCMD="status ${service}" || ZPOOLCMD='import'
POOL_DRIVES="`zpool ${ZPOOLCMD} | find_pool_drives $service`"

if [ -z "${POOL_DRIVES}" ]; then
    dated_echo "No non-hb drives available!"
    exit 0
fi

${PROG_MHDC} -d ${POOL_DRIVES} -c PGR3_STATUS