#!/bin/bash
# Bash script to set artifact naming based on version

# Get the directory where this script is located
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Read the package.json to get the version
PACKAGE_JSON_PATH="$SCRIPT_DIR/../package.json"
VERSION=$(node -p "require('$PACKAGE_JSON_PATH').version")

echo "Current version: $VERSION"

# Determine the artifact suffix based on the version
ARTIFACT_SUFFIX=""

if [[ $VERSION == *"alpha"* ]]; then
    ARTIFACT_SUFFIX="alpha-${VERSION}-"
    echo "Detected alpha version, setting suffix to: $ARTIFACT_SUFFIX"
elif [[ $VERSION == *"beta"* ]]; then
    ARTIFACT_SUFFIX="beta-${VERSION}-"
    echo "Detected beta version, setting suffix to: $ARTIFACT_SUFFIX"
else
    ARTIFACT_SUFFIX=""
    echo "Detected release version, no suffix will be added"
fi

# Export the environment variable
export ARTIFACT_SUFFIX="$ARTIFACT_SUFFIX"

echo "ARTIFACT_SUFFIX set to: '$ARTIFACT_SUFFIX'"

# Also write to a temporary file for sourcing in other scripts
echo "export ARTIFACT_SUFFIX='$ARTIFACT_SUFFIX'" > "$SCRIPT_DIR/.artifact-suffix.env"
