#!/usr/bin/env bash
#
# Build and optionally upload octamem to PyPI.
# Usage:
#   ./scripts/release.sh           # build only
#   ./scripts/release.sh --upload  # build and upload to PyPI
#   ./scripts/release.sh --test    # build and upload to Test PyPI
#

set -e
cd "$(dirname "$0")/.."
ROOT="$(pwd)"

# Colors for output (optional)
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

echo "Project root: $ROOT"

# Ensure build and twine are installed for the current Python
echo -e "${YELLOW}Checking build tools...${NC}"
python3 -m pip install -q build twine

# Remove previous builds
echo -e "${YELLOW}Removing old dist/...${NC}"
rm -rf dist/

# Build
echo -e "${YELLOW}Building sdist and wheel...${NC}"
python3 -m build

echo -e "${GREEN}Build complete. Artifacts in dist/:${NC}"
ls -la dist/

if [[ "$1" == "--upload" ]]; then
  echo -e "${YELLOW}Uploading to PyPI...${NC}"
  python3 -m twine upload dist/*
  echo -e "${GREEN}Upload to PyPI complete.${NC}"
elif [[ "$1" == "--test" ]]; then
  echo -e "${YELLOW}Uploading to Test PyPI...${NC}"
  python3 -m twine upload --repository testpypi dist/*
  echo -e "${GREEN}Upload to Test PyPI complete.${NC}"
else
  echo ""
  echo "To upload to PyPI:        ./scripts/release.sh --upload"
  echo "To upload to Test PyPI:   ./scripts/release.sh --test"
fi
