#! /bin/bash
#set -xv
#=========================================================================
# Copyright (C) GemTalk Systems 1986-2024.  All Rights Reserved..
#
# Name - lshost
# Installed as - lshost
#
# Written By: Martin McClure and Norm Green
#
# Purpose -
#
# List the hosts configured for 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 | -s <stoneName>" >&2
    echo "      -h - show this help screen" >&2
    exit 1
}

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

# 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}
hostDir=${thisStonesDir}/hosts

if [ -d ${hostDir} ]; then
    numDirs=`ls -lR $hostDir | grep ^d | wc -l`
    if [ $numDirs -gt 0 ]; then
	ls -1 ${hostDir}
	exit $?
    fi
fi

echo "No hosts exist for stone '$stoneName'. Use 'newhost' to create one." >&2
exit 0

