#!/bin/bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
MAIN_GO="$PROJECT_DIR/cmd/server/main.go"
MAKEFILE="$PROJECT_DIR/Makefile"

CURRENT=$(grep -oE 'var version = "[0-9]+\.[0-9]+\.[0-9]+"' "$MAIN_GO" | grep -oE '[0-9]+\.[0-9]+\.[0-9]+')
if [[ -z "$CURRENT" ]]; then
    echo "ERROR: cannot parse version from $MAIN_GO" >&2
    exit 1
fi

IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT"
NEW_PATCH=$((PATCH + 1))
NEW="${MAJOR}.${MINOR}.${NEW_PATCH}"

echo "Bumping version: $CURRENT → $NEW"

sed -i '' "s/var version = \"${CURRENT}\"/var version = \"${NEW}\"/" "$MAIN_GO"
sed -i '' "s/VERSION?=${CURRENT}/VERSION?=${NEW}/" "$MAKEFILE"

echo "Done. Updated to $NEW"
echo ""
echo "Next steps:"
echo "  git add cmd/server/main.go Makefile"
echo "  git commit -m 'Bump version to ${NEW}'"
echo "  git push"
