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

ROOT="${1:-/home/ondemandtasks/public_html/on.ondemandtasks.com}"

if [[ ! -d "$ROOT" ]]; then
  echo "ERROR: Root path not found: $ROOT"
  exit 1
fi

echo "LiveSmart layout check"
echo "Root: $ROOT"
echo "Time: $(date)"
echo "----------------------------------------"

FILES=(
  "agent.html"
  "agent.php"
  "client.html"
  "client.php"
  "config/config.json"
)

DIRS=(
  "pages"
  "js"
  "css"
  "extensions"
  "locales"
  "media"
  "server"
  "ws"
  "admin"
  "dash"
)

KEYS=(
  "pages/r.html"
  "dash/loginform.php"
)

check_path () {
  local rel="$1"
  local full="$ROOT/$rel"
  if [[ -e "$full" ]]; then
    echo "OK   : $rel"
  else
    echo "MISS : $rel"
  fi
}

echo "Checking expected files:"
for f in "${FILES[@]}"; do
  check_path "$f"
done
echo

echo "Checking expected directories:"
for d in "${DIRS[@]}"; do
  check_path "$d"
done
echo

echo "Checking key internal paths:"
for k in "${KEYS[@]}"; do
  check_path "$k"
done
echo

echo "----------------------------------------"
echo "If items are missing, checking common alternate docroot: public/"
ALT="$ROOT/public"
if [[ -d "$ALT" ]]; then
  echo "Found $ALT — running the same checks there:"
  ROOT="$ALT"
  echo "Alt Root: $ROOT"
  echo

  echo "Checking expected files (under public/):"
  for f in "${FILES[@]}"; do
    check_path "$f"
  done
  echo

  echo "Checking expected directories (under public/):"
  for d in "${DIRS[@]}"; do
    check_path "$d"
  done
  echo

  echo "Checking key internal paths (under public/):"
  for k in "${KEYS[@]}"; do
    check_path "$k"
  done
  echo
else
  echo "No public/ folder at $ALT"
fi

echo "----------------------------------------"
echo "Search hints:"
echo "Searching for loginform.php and r.html within 5 levels from the original root..."
find "${1:-/home/ondemandtasks/public_html/on.ondemandtasks.com}" -maxdepth 5 -type f \( -name "loginform.php" -o -name "r.html" \) -print 2>/dev/null || true

echo
echo "Done."
