#! /bin/sh
#set -x
#=========================================================================
# Copyright (C) GemTalk Systems 1986-2020.  All Rights Reserved.
#
# Name - removeextent.sh
#
# installed as $GEMSTONE/bin/removedbf
#
# Note:  This script relies on other supporting scripts being in the
#        same directory as this script.
#
# $Id$
#
#=========================================================================

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="Usage: removedbf <fileOrDevice> [-h]
  fileOrDevice -- The file name or device for the repository file to be removed.
  -h   --  print usage and exit.

  Remove a GemStone dbf file, primarily used to erase an extent, transaction log,
  or backup file from a raw partition, but also works on files located on local
  or NFS-mounted file systems.\n";

else
  syntaxMsg="removeexent <extent> [-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
  # no args given
  giveHelp "-h" "$syntaxMsg"
  exit 1
fi

# handle -h arg
giveHelp "$1" "$syntaxMsg"

pgsvr_status=1

# maintenance symbols - see above
destspec=$1                   #   required arg

initConfig ""

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

# required files
if [ ! -f "$GEMSTONE/sys/pgsvr" ]; then
  echo "${comid}[Error]:   cannot execute file $GEMSTONE/sys/pgsvr"
  exit 1
fi

# do the remove with pgsvr 
# say removeextent.removing $comId
echo "${comId}[Info]:     removing...  $destspec "
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
