#!/bin/bash
set -e

# Purpose: Bump version, create git tag, and optionally push to remotes
# Usage: ./bump-version <new-version> [--push]
# Example: ./bump-version 1.0.5
#          ./bump-version 1.0.5 --push
#
# This script:
#   1. Updates the VERSION file
#   2. Updates the spec file Version: line
#   3. Updates debian/changelog (if dch is available)
#   4. Commits the changes
#   5. Creates a git tag (vX.Y.Z format)
#   6. (with --push) Pushes commits and tags to origin and github

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
VERSION_FILE="$SCRIPT_DIR/VERSION"
SPEC_FILE="$SCRIPT_DIR/ionis-apps.spec"
DEB_CHANGELOG="$SCRIPT_DIR/debian/changelog"

usage() {
    printf "Usage: %s <new-version> [--push]\n" "$(basename "$0")"
    printf "\n"
    printf "Options:\n"
    printf "  <new-version>  Version in X.Y.Z format (e.g., 1.0.5)\n"
    printf "  --push         Push commits and tags to origin and github\n"
    printf "\n"
    printf "Examples:\n"
    printf "  %s 1.0.5          # Bump version only\n" "$(basename "$0")"
    printf "  %s 1.0.5 --push   # Bump version and push to remotes\n" "$(basename "$0")"
    printf "\n"
    printf "Current version: %s\n" "$(cat "$VERSION_FILE" 2>/dev/null || echo 'unknown')"
}

# Parse arguments
NEW_VERSION=""
DO_PUSH=false

for arg in "$@"; do
    case "$arg" in
        --push|-p)
            DO_PUSH=true
            ;;
        --help|-h)
            usage
            exit 0
            ;;
        *)
            if [[ -z "$NEW_VERSION" ]]; then
                NEW_VERSION="$arg"
            else
                printf "Error: Unexpected argument '%s'\n" "$arg" >&2
                usage
                exit 1
            fi
            ;;
    esac
done

if [[ -z "$NEW_VERSION" ]]; then
    usage
    exit 1
fi

# Validate version format (X.Y.Z)
if ! [[ "$NEW_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
    printf "Error: Version must be in X.Y.Z format (e.g., 1.0.5)\n" >&2
    exit 1
fi

# Update VERSION file
printf "%s\n" "$NEW_VERSION" > "$VERSION_FILE"
printf "Updated VERSION to %s\n" "$NEW_VERSION"

# Update spec file Version: line
if [[ -f "$SPEC_FILE" ]]; then
    sed -i "s/^Version:.*$/Version:        ${NEW_VERSION}/" "$SPEC_FILE"
    printf "Updated spec file to %s\n" "$NEW_VERSION"
fi

# Update debian/changelog (if dch is available)
if command -v dch >/dev/null 2>&1 && [[ -f "$DEB_CHANGELOG" ]]; then
    dch -v "${NEW_VERSION}-1" -D noble "New upstream release ${NEW_VERSION}"
    printf "Updated debian/changelog to %s-1\n" "$NEW_VERSION"
fi

# Stage and commit
git add "$VERSION_FILE" "$SPEC_FILE"
[[ -f "$DEB_CHANGELOG" ]] && git add "$DEB_CHANGELOG"
git commit -m "v${NEW_VERSION}: version bump"

# Create tag
git tag -a "v${NEW_VERSION}" -m "Release v${NEW_VERSION}"

printf "\nVersion bump complete!\n"
printf "  VERSION file: %s\n" "$NEW_VERSION"
printf "  Spec file:    %s\n" "$NEW_VERSION"
printf "  Git tag:      v%s\n" "$NEW_VERSION"

# Push if requested
if [[ "$DO_PUSH" == true ]]; then
    printf "\nPushing to remotes...\n"

    printf "  Pushing to origin main... "
    git push origin main && printf "OK\n" || { printf "FAILED\n"; exit 1; }

    printf "  Pushing to github main... "
    git push github main && printf "OK\n" || { printf "FAILED\n"; exit 1; }

    printf "  Pushing tags to origin... "
    git push origin --tags && printf "OK\n" || { printf "FAILED\n"; exit 1; }

    printf "  Pushing tags to github... "
    git push github --tags && printf "OK\n" || { printf "FAILED\n"; exit 1; }

    printf "\nRelease v%s pushed to both remotes!\n" "$NEW_VERSION"
else
    printf "\nTo push: git push origin main && git push github main && git push --tags\n"
    printf "Or run:  %s %s --push\n" "$(basename "$0")" "$NEW_VERSION"
fi
