#!/bin/sh
set -eu

START_DIR="$(pwd)"
echo "[bump-version] invoked from ${START_DIR}"

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
REPO_ROOT=""
if command -v git >/dev/null 2>&1; then
  REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null || true)"
fi
if [ -z "${REPO_ROOT}" ]; then
  REPO_ROOT="$(cd "${SCRIPT_DIR}/../../.." && pwd)"
fi
echo "[bump-version] repo root resolved to ${REPO_ROOT}"
cd "${REPO_ROOT}"

VERSION_FILE="./version"
if [ ! -f "${VERSION_FILE}" ]; then
  if git cat-file -e "HEAD:version" 2>/dev/null; then
    echo "[bump-version] version file missing in workspace, restoring from HEAD" >&2
    git show "HEAD:version" > "${VERSION_FILE}"
  else
    echo "[bump-version] version file not found: ${VERSION_FILE}" >&2
    exit 1
  fi
fi

CURRENT_VERSION="$(cat "${VERSION_FILE}")"
NEXT_VERSION="$(printf '%s' "${CURRENT_VERSION}" | awk -F. -v OFS=. '
  function pad(value, width, result, i) {
    result=value ""
    if (length(result) >= width) {
      return result
    }
    i = width - length(result)
    while (i-- > 0) {
      result = "0" result
    }
    return result
  }
  NF==1 { print ++$NF; next }
  {
    last = $NF + 1
    $NF = pad(last, length($NF))
    print
  }')"

printf '%s\n' "${NEXT_VERSION}" > "${VERSION_FILE}"
echo "[bump-version] ${CURRENT_VERSION} -> ${NEXT_VERSION}"

git add "${VERSION_FILE}"
if git diff --cached --quiet; then
  echo "[bump-version] no changes staged, skipping commit"
  exit 0
fi

AUTHOR_NAME="${GIT_AUTHOR_NAME:-woodpecker}"
AUTHOR_EMAIL="${GIT_AUTHOR_EMAIL:-ci@sendico.io}"
git config user.name "${AUTHOR_NAME}"
git config user.email "${AUTHOR_EMAIL}"

git commit -m "chore(ci): bump version to ${NEXT_VERSION}"

BRANCH="${WOODPECKER_BRANCH:-}"
if [ -z "${BRANCH}" ] || [ "${BRANCH}" = "HEAD" ]; then
  BRANCH="$(git rev-parse --abbrev-ref HEAD)"
fi

REMOTE_URL="${CI_REPO_REMOTE:-${WOODPECKER_GIT_REMOTE:-${DRONE_REMOTE_URL:-}}}"
if [ -z "${REMOTE_URL}" ]; then
  REMOTE_URL="$(git config --get remote.origin.url 2>/dev/null || true)"
fi

# Normalize machine to a bare hostname so .netrc matches HTTPS requests.
normalize_machine() {
  value="$1"
  if [ -z "${value}" ]; then
    printf '%s' ""
    return
  fi
  printf '%s' "${value}" | sed -E '
    s#^[[:alpha:]][[:alnum:]+.-]*://##;
    s#^[^@]*@##;
    s#/.*$##;
  '
}

NETRC_MACHINE="${CI_NETRC_MACHINE:-${WOODPECKER_NETRC_MACHINE:-${DRONE_NETRC_MACHINE:-}}}"
NETRC_USERNAME="${CI_NETRC_USERNAME:-${WOODPECKER_NETRC_USERNAME:-${DRONE_NETRC_USERNAME:-${CI_NETRC_LOGIN:-${WOODPECKER_NETRC_LOGIN:-${DRONE_NETRC_LOGIN:-}}}}}}"
NETRC_PASSWORD="${CI_NETRC_PASSWORD:-${WOODPECKER_NETRC_PASSWORD:-${DRONE_NETRC_PASSWORD:-}}}"
NETRC_MACHINE="$(normalize_machine "${NETRC_MACHINE}")"
if [ -z "${NETRC_MACHINE}" ] && [ -n "${REMOTE_URL}" ]; then
  NETRC_MACHINE="$(normalize_machine "${REMOTE_URL}")"
fi

if [ -n "${NETRC_MACHINE}" ] && [ -n "${NETRC_USERNAME}" ] && [ -n "${NETRC_PASSWORD}" ]; then
  NETRC_FILE="${HOME:-/root}/.netrc"
  {
    printf 'machine %s\n' "${NETRC_MACHINE}"
    printf 'login %s\n' "${NETRC_USERNAME}"
    printf 'password %s\n' "${NETRC_PASSWORD}"
  } > "${NETRC_FILE}"
  chmod 600 "${NETRC_FILE}"
  echo "[bump-version] configured credentials for ${NETRC_MACHINE}"
fi

if [ -n "${REMOTE_URL}" ]; then
  git remote set-url origin "${REMOTE_URL}"
fi

git push origin "HEAD:${BRANCH}"
