#!/usr/bin/env bash
set -euo pipefail

# cPanel's default `php` is often an old version. Set this explicitly.
# Check with: ls -d /opt/cpanel/ea-php*
PHP=/opt/cpanel/ea-php83/root/usr/bin/php

# composer.json requires php ^8.3, so bail out early rather than half-deploying
# with the wrong binary. This runs before the ERR trap below on purpose: if PHP
# is unusable, `artisan up` can't run either.
REQUIRED_PHP_MAJOR=8
REQUIRED_PHP_MINOR=3

if ! command -v "$PHP" >/dev/null 2>&1; then
  echo "ERROR: PHP binary not found at: $PHP" >&2
  echo "Available versions:" >&2
  ls -d /opt/cpanel/ea-php* 2>/dev/null >&2 || echo "  (none under /opt/cpanel)" >&2
  echo "Edit the PHP= line at the top of $0 to point at a usable binary." >&2
  exit 1
fi

PHP_VERSION="$("$PHP" -r 'echo PHP_VERSION;' 2>/dev/null || true)"
PHP_MAJOR="$("$PHP" -r 'echo PHP_MAJOR_VERSION;' 2>/dev/null || true)"
PHP_MINOR="$("$PHP" -r 'echo PHP_MINOR_VERSION;' 2>/dev/null || true)"

if [ -z "$PHP_MAJOR" ] || [ -z "$PHP_MINOR" ]; then
  echo "ERROR: could not determine the version of: $PHP" >&2
  exit 1
fi

if [ "$PHP_MAJOR" -ne "$REQUIRED_PHP_MAJOR" ] \
  || [ "$PHP_MINOR" -lt "$REQUIRED_PHP_MINOR" ]; then
  echo "ERROR: PHP ${PHP_VERSION} is too old/new for this app." >&2
  echo "Need >= ${REQUIRED_PHP_MAJOR}.${REQUIRED_PHP_MINOR} and < $((REQUIRED_PHP_MAJOR + 1)).0 (composer.json: php ^8.3)." >&2
  echo "Binary: $PHP" >&2
  echo "Available versions:" >&2
  ls -d /opt/cpanel/ea-php* 2>/dev/null >&2 || echo "  (none under /opt/cpanel)" >&2
  exit 1
fi

echo "==> PHP ${PHP_VERSION} ($PHP)"

cd "$(dirname "$0")"

# If anything below fails, make sure the site comes back up.
trap '$PHP artisan up || true' ERR

echo "==> Maintenance mode"
$PHP artisan down --retry=15 || true

echo "==> Fetching latest"
git fetch --prune origin
git reset --hard origin/main

echo "==> Clearing stale caches"
$PHP artisan optimize:clear

# NOTE: no `artisan migrate` here on purpose. Migrations are run by hand.

echo "==> Rebuilding caches"
$PHP artisan package:discover --ansi
$PHP artisan config:cache
$PHP artisan route:cache
$PHP artisan view:cache

echo "==> Storage link"
[ -L public/storage ] || $PHP artisan storage:link

echo "==> Restarting queue workers"
$PHP artisan queue:restart || true

# Supervisor isn't available on every host (cPanel shared hosting usually lacks
# it), so only poke it when the binary is actually there. Set SUPERVISOR_GROUP
# to the program/group name to restart, e.g. SUPERVISOR_GROUP=ccistage-worker:*
SUPERVISOR_GROUP="${SUPERVISOR_GROUP:-all}"
if command -v supervisorctl >/dev/null 2>&1; then
  echo "==> Restarting supervisor ($SUPERVISOR_GROUP)"
  SUPERVISORCTL=(supervisorctl)
  # Non-root usually can't talk to the supervisor socket directly.
  if [ "$(id -u)" -ne 0 ] && command -v sudo >/dev/null 2>&1; then
    SUPERVISORCTL=(sudo -n supervisorctl)
  fi
  "${SUPERVISORCTL[@]}" reread || true
  "${SUPERVISORCTL[@]}" update || true
  "${SUPERVISORCTL[@]}" restart "$SUPERVISOR_GROUP" || true
else
  echo "==> Supervisor not installed, skipping"
fi

trap - ERR
$PHP artisan up
echo "==> Done."
