#!/usr/bin/env bash
# shellcheck shell=bash
set -euxo pipefail

released_versions="$(git tag --list | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+(-rc\.[0-9]+)?$')"
cur_version="$(cargo pkgid hk | cut -d# -f2 | cut -d@ -f2)"
if ! echo "$released_versions" | grep -q "^v$cur_version$"; then
  echo "Releasing $cur_version"
  if [ "${DRY_RUN:-}" == 0 ]; then
    # Temporarily replace path dependencies with version dependencies for publishing
    cp Cargo.toml Cargo.toml.bak
    
    # Get the current versions of the workspace members from their local Cargo.toml files
    xx_version="$(grep '^version' xx/Cargo.toml | cut -d'"' -f2)"
    clx_version="$(grep '^version' clx/Cargo.toml | cut -d'"' -f2)"
    ensembler_version="$(grep '^version' ensembler/Cargo.toml | cut -d'"' -f2)"
    
    # Replace path dependencies with version dependencies
    sed -i "s/clx = { path = \"clx\" }/clx = \"$clx_version\"/" Cargo.toml
    sed -i "s/ensembler = { path = \"ensembler\" }/ensembler = \"$ensembler_version\"/" Cargo.toml
    sed -i "s/xx = { path = \"xx\", features = \[\"http\", \"hash\", \"rustls\"\] }/xx = { version = \"$xx_version\", features = [\"http\", \"hash\", \"rustls\"] }/" Cargo.toml
    
    # Publish the crate
    cargo publish -p hk --allow-dirty || {
      # Restore original Cargo.toml on failure
      mv Cargo.toml.bak Cargo.toml
      exit 1
    }
    
    # Restore original Cargo.toml
    mv Cargo.toml.bak Cargo.toml
    
    changelog="$(git cliff --tag "v$cur_version" --strip all --unreleased)"
    git tag "v$cur_version"
    git push --tags
    gh release create "v$cur_version" --title "v$cur_version" --notes "$changelog"
  fi
  exit 0
fi

version="$(git cliff --bumped-version)"
changelog="$(git cliff --bump --unreleased --strip all)"

if [ "${DRY_RUN:-}" != 0 ]; then
  echo "version: $version"
  echo "changelog: $changelog"
  exit 0
fi

git cliff --bump -o CHANGELOG.md
cargo set-version --package hk "${version#v}"

git config user.name mise-en-dev
git config user.email 123107610+mise-en-dev@users.noreply.github.com
cargo update

# Regenerate rendered artifacts (CLI docs, usage, etc.) for the release
mise run render
git add \
  Cargo.* \
  CHANGELOG.md \
  docs \
  hk.usage.kdl \
  hk.pkl \
  src

git checkout -B release
git commit -m "chore: release $version"
git push origin release --force
gh pr create --title "chore: release $version" --body "$changelog" --label "release" ||
  gh pr edit --title "chore: release $version" --body "$changelog"

# Push subtree changes to respective repositories
if [ -d "xx/.git" ] || [ -d "clx/.git" ] || [ -d "ensembler/.git" ]; then
  echo "Error: Subtree directories should not contain .git folders"
  exit 1
fi

# Push xx subtree
if git ls-tree HEAD:xx > /dev/null 2>&1; then
  echo "Pushing xx subtree changes..."
  git subtree push --prefix=xx https://github.com/jdx/xx.git main || echo "No changes to push for xx"
fi

# Push clx subtree
if git ls-tree HEAD:clx > /dev/null 2>&1; then
  echo "Pushing clx subtree changes..."
  git subtree push --prefix=clx https://github.com/jdx/clx.git main || echo "No changes to push for clx"
fi

# Push ensembler subtree
if git ls-tree HEAD:ensembler > /dev/null 2>&1; then
  echo "Pushing ensembler subtree changes..."
  git subtree push --prefix=ensembler https://github.com/jdx/ensembler.git main || echo "No changes to push for ensembler"
fi 
