scripts/build_version.sh

#!/bin/bash
# Script to create a git tag and update the version in main.go

set -e

if [ -z "$1" ]; then
    echo "Usage: $0 <version>"
    echo "Example: $0 v1.0.0"
    exit 1
fi

TAG_NAME="$1"
# Remove 'v' prefix if present (e.g., v1.0.0 -> 1.0.0)
VERSION="${TAG_NAME#v}"

# Path to main.go
MAIN_GO="main.go"

# Check if main.go exists
if [ ! -f "$MAIN_GO" ]; then
    echo "Error: $MAIN_GO not found"
    exit 1
fi

# Check if we're in a git repo
if ! git rev-parse --git-dir > /dev/null 2>&1; then
    echo "Error: Not in a git repository"
    exit 1
fi

# Check if tag already exists
if git rev-parse "$TAG_NAME" > /dev/null 2>&1; then
    echo "Error: Tag $TAG_NAME already exists"
    exit 1
fi

# Update the version variable in main.go
sed -i "s/^var version = \".*\"/var version = \"$VERSION\"/" "$MAIN_GO"

# Update the version variable in the frontend package.json
jq ".version = \"$VERSION\"" "internal/dashboard/frontend/package.json" > "internal/dashboard/frontend/package.json.tmp"
mv "internal/dashboard/frontend/package.json.tmp" "internal/dashboard/frontend/package.json"

# Build the frontend
make build-frontend

echo "Version updated to $VERSION"