#! /bin/bash
# set -xv
#=========================================================================
# Copyright (C) GemTalk Systems 1986-2024.  All Rights Reserved..
#
# Name - rmuser
# Installed as - rmuser
#
# Written By: Martin McClure and Norm Green
#
# Purpose -
#
# Remove certificates for a given user on a given stone.
#
# 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 -h | [-f] -s <stoneName> <userName>" >&2
    echo "      -h - show this help screen" >&2
    echo "      -f - force. Do not prompt for confirmation" >&2    
    exit 1
}

force=0
ARGC=$#

while getopts "fhs:" opt; do
    case $opt in
        h)
            usage
          ;;	
        s)
          stoneName=${OPTARG}
          ;;
	f)
          force=1
         ;;
        \?)
          usage
          ;;
        :)
          echo "Option -${OPTARG} requires an argument." >&2
          usage
          ;;
    esac
done
shift $((OPTIND-1))
userName=$1

# 48065
if [ $OPTIND -ne $ARGC ]; then
    echo "[Error]: userName must be the last argument."
    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 [ -z "$userName" ]; then
    echo "[Error]: Missing username argument" >&2
    usage
fi

userDir=${thisStonesDir}/users/${userName}

if [ ! -d ${userDir} ]; then
    echo "[Error]: No user named '$userName' exists for the stone named '$stoneName'" >&2
    exit 1
fi

if [ $force -eq 0 ]; then
  echo "[Warning]: All certificates for user '$userName' for use with stone '$stoneName' will be deleted."
  confirmAction     
fi

dorm ${userDir}
exit $?

