#!/usr/bin/env bash
# Clone or update Chatwoot application source (Ruby + Vue).
set -euo pipefail
cd "$(dirname "$0")/.."

branch="${CHATWOOT_GIT_BRANCH:-develop}"
repo="${CHATWOOT_GIT_REPO:-https://github.com/chatwoot/chatwoot.git}"
dir="chatwoot"

if [[ -d "${dir}/.git" ]]; then
  echo "Updating ${dir} (${branch})..."
  git -C "${dir}" fetch origin "${branch}" --depth 1
  git -C "${dir}" checkout "${branch}"
  git -C "${dir}" pull --ff-only origin "${branch}" || true
else
  echo "Cloning ${repo} (branch ${branch}) into ${dir}/ ..."
  git clone --depth 1 --branch "${branch}" "${repo}" "${dir}"
fi

echo "Source ready: $(realpath "${dir}")"
echo "Next: ./scripts/build-local-image.sh   (first time, ~15–30 min)"
echo "      ./scripts/install-external-db.sh"
