#!/bin/sh
# Usage: deletedrives.sh <VTL Name> <New Drive Count>
#set -x

if [ $# -ne 2 ]; then
        echo "ERROR: Invalid number of arguments $#"
        echo "Usage: deletedrives.sh <VTL Name> <New Drive Count>"
        exit 1
fi

export PATH="/quadstorvtl/bin:/quadstorvtl/quadstor/scctl:$PATH"
vtl=$1

dcount=`vtconfig -l -v $vtl  | grep ^drive | wc -l`

if [ "$dcount" = "0" ]; then
	echo "Cannot get drive count $vtl"
	exit 1
fi

echo "Got drive count $dcount for VTL $vtl"

if [ $dcount -lt $2 ]; then
	echo "Current drive count $dcount less than new drive count $2"
	exit 0
fi

vtlid=`echo "select vtlid from VTLS WHERE VTLNAME='${vtl}'" | /quadstorvtl/pgsql/bin/psql -A -t -U vtdbuser qsdb`
echo "Got VTL id $vtlid for name $vtl"

echo "delete from vdrives where tlid=$vtlid and targetid > $2" | /quadstorvtl/pgsql/bin/psql -U vtdbuser qsdb
