#!/bin/bash


if [ -z "$1" ]; then
  echo 'You must provide a Raid ID to query';
  exit;
fi

if [ -n "$2" ]; then
  RAW=$(storcli /c$1/e$2/s$3  show all);

  STATE=$(echo "$RAW"|grep "$2:$3"|awk '{print $3}'|sed -e s/'UGood'/'Unconfigured Good'/g|sed -e s/'GHS'/'Global Hotspare'/g| sed -e s/'UBad'/'Unconfigured Bad'/g|sed -e s/'Onln'/'Online'/g|sed -e s/'Offln'/'Offline'/g);
  echo 'Firmware state: '$STATE;
  IQD=$(echo "$RAW"|grep '^Manufacturer Id\|^Model Number'|cut -d'=' -f2|tr "\n" " ");
  echo Inquiry Data: $IQD;
  DP=$(echo "$RAW"|grep "Drive position"|sed -e s/'Drive position'/"Drive's position"/g|sed -e s/' = '/':'/g|sed -e s/'DriveGroup'/'DiskGroup'/g|sed -e s/'Row:'/'Arm:'/g|sed -e s/':'/': '/g);
  echo $DP;
#  echo "Drive's position: DiskGroup: $1, Span: $2, Arm: $3"
  exit;
fi

storcli /c$1 show all > /tmp/storcli.out

TOTAL=$(wc -l /tmp/storcli.out|awk '{print $1}' )
START=$(grep -n "TOPOLOGY :" /tmp/storcli.out|cut -d':' -f1);
NEXT=$(grep -n "^DG=Disk Group Index" /tmp/storcli.out|cut -d':' -f1);
DATA=$(tail -$(($TOTAL - $START -5 )) /tmp/storcli.out|head -$(($NEXT - $START -8)));

#echo "$DATA";

DRIVES=$(echo "$DATA"|grep 'DRIVE');


echo "$DRIVES"|while read d; do
  EID=$(echo "$d"|awk '{print $4}'|cut -d':' -f1)
  SLOT=$(echo "$d"|awk '{print $4}'|cut -d':' -f2)
  PD=$(echo "$d"|awk '{print $3}');
  if [ "$EID" = "-" ]; then
          continue;
  fi
  echo 'PD: '$PD;
  echo Slot Number: $SLOT
  echo Enclosure Device ID: $EID;
done
