#!/usr/bin/env bash
# Find which folder actually serves test.duiclick.com
set -euo pipefail

USER_HOME="${HOME:-/home/duiclick}"
CANDIDATES=(
  "${USER_HOME}/test.duiclick.com"
  "${USER_HOME}/public_html/test.duiclick.com"
  "${USER_HOME}/public_html"
)

echo "== Looking for Laravel app copies =="
for dir in "${CANDIDATES[@]}"; do
  echo ""
  echo "--- ${dir} ---"
  if [[ ! -d "${dir}" ]]; then
    echo "  (directory does not exist)"
    continue
  fi
  [[ -f "${dir}/artisan" ]] && echo "  artisan: yes" || echo "  artisan: no"
  [[ -d "${dir}/.git" ]] && echo "  git: yes ($(git -C "${dir}" rev-parse --short HEAD 2>/dev/null || echo '?'))" || echo "  git: no"
  if [[ -f "${dir}/resources/views/layouts/app.blade.php" ]]; then
    if grep -q "Opening Balance" "${dir}/resources/views/layouts/app.blade.php" 2>/dev/null; then
      echo "  app.blade.php: YES — has Opening Balance menu"
    else
      echo "  app.blade.php: yes — OLD (no Opening Balance)"
    fi
  else
    echo "  app.blade.php: missing"
  fi
  if [[ -f "${dir}/routes/web.php" ]] && grep -q "opening-balance" "${dir}/routes/web.php" 2>/dev/null; then
    echo "  opening-balance route: yes"
  else
    echo "  opening-balance route: no"
  fi
  if [[ -d "${dir}/bootstrap/cache" ]]; then
    if [[ -w "${dir}/bootstrap/cache" ]]; then
      echo "  bootstrap/cache: writable (SSH user)"
    else
      echo "  bootstrap/cache: NOT writable (SSH user) — likely 503 cause"
    fi
  else
    echo "  bootstrap/cache: missing"
  fi
  if [[ -f "${dir}/.env" ]]; then
    echo "  .env: yes"
  else
    echo "  .env: MISSING"
  fi
done

echo ""
echo "== Tip =="
echo "Run deploy commands in the folder that shows 'has Opening Balance menu'."
echo "Or set cPanel document root to: /home/USER/test.duiclick.com/public"
