#! /bin/bash
#=========================================================================
# Copyright (C) GemTalk Systems 1986-2024.  All Rights Reserved..
#
# Name - rmstone
# Installed as - rmstone
#
# Written By: Martin McClure and Norm Green
#
# Purpose - Removes files for all hosts and users for the given stone name.
#
# Requirements -
#
# The following environment variables must be defined:
#
# GEMSTONE_CERT_DIR - A directory where newly created certificates and
#                     subdirectories will be placed.
#
#=========================================================================

cmd=`basename $0`

usage(){
    echo "Usage: $cmd [-f] <stoneName>" >&2
    echo "      -h - show this help screen" >&2
    echo "      -f - force. Do not prompt for confirmation" >&2    
    exit 1
}

force=0
ARGC=$#

while getopts "hf" opt; do
    case $opt in
	h)
	    usage
	    ;;
        f)
          force=1
          ;;
        \?)
          usage
          ;;
    esac
done
shift $((OPTIND-1))
stoneName=$1

# 48065
if [ $OPTIND -ne $ARGC ]; then
    echo "[Error]: stoneName must be the last argument."
    usage
fi

if [ "$stoneName" = ""  -o "$2" != "" -o "$stoneName" = "-h" ]; then
    usage
fi

# 47501 - handle symlinks
fullPath=`readlink -e -n $0`
scriptDir=`dirname $fullPath`
if [ ! -f $scriptDir/environment.sh ]; then
    echo "[Error]: Cannot find environment.sh setup script"
    exit 1
fi
. ${scriptDir}/environment.sh

verifyStoneExistsForRm ${stoneName}

if [ $force -eq 0 ]; then
  echo "[Warning]: All files for stone '$stoneName' will be deleted"
  confirmAction     
fi

dorm ${thisStonesDir}
exit $?
