#!/usr/bin/env bash

MODULE_NFS_SERVER_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$MODULE_NFS_SERVER_DIR/../../.." && pwd)"

# shellcheck source=lib/ui.sh
source "$PROJECT_ROOT/lib/ui.sh"
# shellcheck source=lib/log.sh
source "$PROJECT_ROOT/lib/log.sh"
# shellcheck source=lib/package.sh
source "$PROJECT_ROOT/lib/package.sh"
# shellcheck source=core/runtime.sh
source "$PROJECT_ROOT/core/runtime.sh"
# shellcheck source=modules/network/nfs-server/module.sh
source "$MODULE_NFS_SERVER_DIR/module.sh"

runtime_init "$PROJECT_ROOT"
log_init

if ! package_is_installed nfs-kernel-server; then
  printf 'nfs-server test SKIPPED: nfs-kernel-server not installed\n'
  exit 0
fi

if ! test -f "$PROJECT_ROOT/config/nfs-server.exports.yaml"; then
  printf 'nfs-server test FAILED: missing repository config\n' >&2
  exit 1
fi

if ! test -f /etc/exports.d/postinstall.exports; then
  printf 'nfs-server test SKIPPED: module configuration not applied\n'
  exit 0
fi

if module_nfs_server_test; then
  printf 'nfs-server test OK\n'
else
  printf 'nfs-server test FAILED\n' >&2
  exit 1
fi
