#!/usr/bin/env sh

if command -v realpath >/dev/null 2>&1; then
  # shellcheck disable=SC2155
  readonly BIN_DIR="$(dirname "$(realpath "$0")")"
else
  # shellcheck disable=SC2155
  readonly BIN_DIR="$(cd "$(dirname "$0")" && pwd -P)"
fi

REPO_DIR="$BIN_DIR/../.."

OLD_VERSION="$(cat "$REPO_DIR/version.txt")"
ESCAPED_OLD_VERSION=$(printf '%s\n' "$OLD_VERSION" | sed 's/\./\\./g')

NEW_VERSION="$1"
[ -z "$NEW_VERSION" ] && printf "<new-version> is a required argument eg release 0.8.8; current version is $OLD_VERSION" && exit 1

sed -i "s|$ESCAPED_OLD_VERSION|$NEW_VERSION|" "$REPO_DIR/version.txt"
sed -i "s|$ESCAPED_OLD_VERSION|$NEW_VERSION|" "$REPO_DIR/yt-x"
sed -i "s|$ESCAPED_OLD_VERSION|$NEW_VERSION|" "$REPO_DIR/README.md"

git add "$REPO_DIR/version.txt" "$REPO_DIR/yt-x" "$REPO_DIR/README.md"
git diff --staged

gum confirm "Do you want to commit the changes?" && git commit -m "chore: bump to version $NEW_VERSION" && git push && release_notes="$(git-cliff --unreleased --tag "v$NEW_VERSION")" && gum confirm "Would you like to create a release on GitHub?" && gh release create "v$NEW_VERSION" --title "v$NEW_VERSION" --notes "$release_notes"
