#!/usr/bin/env bash
# =============================================================================
# V-Connect PBX — Publish Update
# =============================================================================
# Commits all current changes and pushes to Bitbucket.
# Automatically increments the patch version tag (v1.0.0 → v1.0.1).
# Run this on the DEV server after making changes.
#
# Usage:
#   bash /var/www/html/scripts/pbx-publish.sh
#   bash /var/www/html/scripts/pbx-publish.sh "fix queue ringinuse bug"
#   bash /var/www/html/scripts/pbx-publish.sh "add new feature" --minor   # bump minor (v1.0.x → v1.1.0)
#   bash /var/www/html/scripts/pbx-publish.sh "breaking change" --major   # bump major (v1.x.x → v2.0.0)
# =============================================================================

set -euo pipefail

APP_DIR="/var/www/html"

cd "$APP_DIR"

# ── Check we're in a git repo ─────────────────────────────────────────────────
if ! git rev-parse --git-dir > /dev/null 2>&1; then
    echo "ERROR: Not a git repository. Run from $APP_DIR."
    exit 1
fi

# ── Parse arguments ───────────────────────────────────────────────────────────
MSG=""
BUMP="patch"
for arg in "$@"; do
    case "$arg" in
        --minor) BUMP="minor" ;;
        --major) BUMP="major" ;;
        *)       MSG="$arg"   ;;
    esac
done

# ── Check there's something to commit ────────────────────────────────────────
if git diff --quiet && git diff --cached --quiet && [[ -z "$(git status --porcelain)" ]]; then
    echo "Nothing to commit — working tree is clean."
    echo "Current commit: $(git log --oneline -1)"
    exit 0
fi

# ── Show what changed ─────────────────────────────────────────────────────────
echo ""
echo "Changes to be published:"
echo "─────────────────────────────────────────────────────"
git diff --stat HEAD
echo "─────────────────────────────────────────────────────"
echo ""

# ── Get commit message ────────────────────────────────────────────────────────
if [[ -z "$MSG" ]]; then
    read -rp "Commit message: " MSG
fi

if [[ -z "$MSG" ]]; then
    echo "ERROR: Commit message cannot be empty."
    exit 1
fi

# ── Calculate next version tag ────────────────────────────────────────────────
LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0")
# Strip leading 'v' and split
VERSION="${LATEST_TAG#v}"
MAJOR=$(echo "$VERSION" | cut -d. -f1)
MINOR=$(echo "$VERSION" | cut -d. -f2)
PATCH=$(echo "$VERSION" | cut -d. -f3)

case "$BUMP" in
    major) MAJOR=$((MAJOR + 1)); MINOR=0; PATCH=0 ;;
    minor) MINOR=$((MINOR + 1)); PATCH=0 ;;
    patch) PATCH=$((PATCH + 1)) ;;
esac

NEW_TAG="v${MAJOR}.${MINOR}.${PATCH}"

# ── Stage, commit, tag, push ──────────────────────────────────────────────────
echo ""
git add -A
git commit -m "$MSG"
git tag -a "$NEW_TAG" -m "$NEW_TAG: $MSG"
git push origin main
git push origin "$NEW_TAG"

echo ""
echo "✓ Published: $(git log --oneline -1)"
echo "  Version:   $NEW_TAG  (was $LATEST_TAG)"
echo "  https://bitbucket.org/openvdev/vconnectpbx/src/main/"
echo ""
