#!/bin/bash

# Common authentication utility for canton-contracts scripts
# This script handles token parsing and retrieval for all scripts in the config folder
# Usage: source common_auth.sh "$@"

# Parse command line arguments
TOKEN=""
while [[ $# -gt 0 ]]; do
    case $1 in
        -t|--token)
            TOKEN="$2"
            shift 2
            ;;
        *)
            echo "Unknown option: $1"
            echo "Usage: $0 [-t|--token TOKEN]"
            exit 1
            ;;
    esac
done


# Use provided token or get one from get_token.sh (robust path)
if [ -n "$TOKEN" ]; then
    CANTON_TOKEN="$TOKEN"
else
    SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
    CANTON_TOKEN=$("$SCRIPT_DIR/get_token.sh")
fi

# Export the token so it's available to the calling script
export CANTON_TOKEN
