#!/usr/bin/env bash

MODULE_SAMBA_SHARE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$MODULE_SAMBA_SHARE_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/samba-share/module.sh
source "$MODULE_SAMBA_SHARE_DIR/module.sh"

runtime_init "$PROJECT_ROOT"
log_init

if ! package_is_installed samba; then
  printf 'samba-share test SKIPPED: samba not installed\n'
  exit 0
fi

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

if ! test -f /etc/samba/smb.conf.d/postinstall-home.conf; then
  printf 'samba-share test SKIPPED: module configuration not applied\n'
  exit 0
fi

if module_samba_share_test; then
  printf 'samba-share test OK\n'
else
  printf 'samba-share test FAILED\n' >&2
  exit 1
fi
