#! /bin/sh
#set -x
#=========================================================================
# Copyright (C) VMware, Inc. 1986-2011.  All Rights Reserved..
#
# Name - removeextent.sh
#
# Purpose - 
#
# Note:  This script relies on other supporting scripts being in the
#        same directory as this script.
#
# $Id: removeextent.sh,v 1.6 2008-01-09 22:50:52 stever Exp $
#
#=========================================================================

if [ "a$GEMSTONE" = "a" ]; then
  echo "ERROR: GemStone scripts require a GEMSTONE environment variable."
  echo "       Please set it to the directory where GemStone resides."
  exit 1
fi

comId=`echo $0 | sed -e 's/\(.*\)\/\(.*\)$/\2/g'` # Basic `basename`
if [ "$comId" = "removedbf" ]; then
  syntaxMsg="removedbf <fileOrDevice> [-nnetLdiName] [-ppgsvrnetid] [-h]\n"
else
  syntaxMsg="removeexent <extent> [-nnetLdiName] [-ppgsvrnetid] [-h]\n"
fi

# make sure of minimum path
PATH=:/bin:/usr/bin:/usr/ucb:$PATH; export PATH

. $GEMSTONE/bin/misc.sh

defaultErrorControl

if [ "x$1" = "x" ]; then
  giveHelp "-h" "$syntaxMsg"
  exit 1
fi

giveHelp "$1" "$syntaxMsg"

pgsvr_status=1

# maintenance symbols - see above
destspec=""                   #   required arg

initConfig ""

# Process switches
getSwitches n:p:t "$*"
set -- $switches
while [ "x$1" != "x" ]
do
  switch="$1"
  shift
  case "$switch" in
    -n) netLdiSpec="$1" ; shift ;;
    -p) runpgsvrSpec="$1" ; shift ;;
    -t) timeout="$1" ; shift ;;
    --)  # Non-switch arguments
	if [ "x$1" = "x" ]; then
	  # extent name is missing
	  # say removeextent.ename
          echo "removeextent[Error]:    file or device name is missing."
	  exit 1
	fi
	destspec="$1"
	shift
	if [ "x$1" != "x" ]; then
	  # say shell.toomany $comId "$1"
          echo "${comId}[Error]:   Too many non-switch arguments   $1 "
	  exit 1
	fi
	break
	;;
    *)  # say shell.peculiar $comId "$switch"
        echo "${comId}[Error]:   Peculiar argument  $switch "
	exit 1
	;;
  esac
done

# Get ancillary defaults
appName=""
. $GEMSTONE/bin/setconfig.sh

# required files
requireExes $GEMSTONE/sys/pgsvr

# do the remove with pgsvr 
# say removeextent.removing $comId
echo "${comId}[Info]:     removing..."
pgsvroutput=`$GEMSTONE/sys/pgsvr "'"$destspec"'" remove hostexit` 
pgsvr_status=$?

# if error then echo
if [ "$pgsvr_status" != "0" ]; then
  echo "$pgsvroutput"
  exit $pgsvr_status;
fi
exit 0 				# all ok
