#!/bin/bash

set -euo pipefail

usage() {
    cat <<'EOF'
Usage: daylily-return-fsx-filesystem-for-cluster <cluster_name> <region>

Print the FSx filesystem id and import/export S3 paths associated with a cluster.

Environment:
  AWS_PROFILE  AWS CLI profile to use (defaults to "default")
EOF
}

if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
    usage
    exit 0
fi

CLUSTER_NAME="${1:-}"
REGION="${2:-}"
AWS_PROFILE="${AWS_PROFILE:-default}"

if [[ -z "$CLUSTER_NAME" || -z "$REGION" ]]; then
    usage
    exit 1
fi

if ! command -v aws >/dev/null 2>&1; then
    echo "Error: required command 'aws' not found in PATH" >&2
    exit 1
fi

# Get FSx IDs associated with the cluster
FSX_IDS=$(aws fsx describe-file-systems \
    --profile "$AWS_PROFILE" \
    --region "$REGION" \
    --query "FileSystems[?contains(Tags[?Key=='parallelcluster:cluster-name'].Value | [0], '$CLUSTER_NAME')].FileSystemId" \
    --output text)
 

IMPORT_EXPORT=$(aws fsx describe-file-systems \
    --profile "$AWS_PROFILE" \
    --region "$REGION" \
    --query "FileSystems[].LustreConfiguration.DataRepositoryConfiguration.[ImportPath, ExportPath]" \
    --output text )



# Output the FSx IDs
echo "FSx IDs associated with cluster '$CLUSTER_NAME':" >&2
echo -e "$FSX_IDS\t"$IMPORT_EXPORT
