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

source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib/paths.sh"

fail=0

require_file() {
  local path="$1"
  if [[ ! -f "$path" ]]; then
    echo "MISSING FILE: $path"
    fail=1
  fi
}

require_dir() {
  local path="$1"
  if [[ ! -d "$path" ]]; then
    echo "MISSING DIR: $path"
    fail=1
  fi
}

echo "Preflight check starting..."

require_dir "$PROJECT_ROOT/scripts"
require_file "$PROJECT_ROOT/scripts/create-dir.sh"
require_file "$PROJECT_ROOT/scripts/new-docs.sh"
require_file "$PROJECT_ROOT/scripts/new-app.sh"
require_file "$PROJECT_ROOT/scripts/runall.sh"
require_file "$PROJECT_ROOT/scripts/lib/paths.sh"

require_dir "$PROJECT_ROOT/common"
require_dir "$PROJECT_ROOT/common/php"
require_dir "$PROJECT_ROOT/common/js"
require_dir "$PROJECT_ROOT/defaults"
require_dir "$PROJECT_ROOT/defaults/templates"
require_dir "$PROJECT_ROOT/defaults/templates/tmpl-sys"
require_dir "$PROJECT_ROOT/defaults/templates/tmpl-games"
require_dir "$PROJECT_ROOT/apps"
require_dir "$PROJECT_ROOT/sys-apps"

if [[ "$fail" -ne 0 ]]; then
  echo "Preflight failed."
  exit 1
fi

echo "Preflight passed."
