#!/usr/bin/env bash
# Map http://localhost/credence/ → Credence Laravel public + reload Apache.
# Run: sudo bash ~/Projects/credence/setup_apache.sh
set -euo pipefail

ROOT="/home/agbolase/Projects/credence"
CONF_SRC="$ROOT/apache-credence.conf"
CONF="/etc/apache2/conf-available/credence.conf"

if [[ "${EUID}" -ne 0 ]]; then
  echo "Needs root. Run: sudo bash $0"
  exit 1
fi

if [[ ! -f "$CONF_SRC" ]]; then
  echo "Missing $CONF_SRC" >&2
  exit 1
fi

install -m 644 "$CONF_SRC" "$CONF"

a2enmod rewrite >/dev/null
a2enconf credence >/dev/null
systemctl reload apache2

# Ensure storage is writable by the web server
chown -R www-data:www-data "$ROOT/storage" "$ROOT/bootstrap/cache" 2>/dev/null || true
chmod -R ug+rwx "$ROOT/storage" "$ROOT/bootstrap/cache" 2>/dev/null || true

CODE=$(curl -s -o /tmp/credence_test.html -w "%{http_code}" http://127.0.0.1/credence/ || true)
echo "http://localhost/credence/ → HTTP $CODE"
echo "APP_URL should be: http://localhost/credence"
