#!/usr/bin/env bash

MODULE_SSH_SERVER_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$MODULE_SSH_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/ssh-server/module.sh
source "$MODULE_SSH_SERVER_DIR/module.sh"

runtime_init "$PROJECT_ROOT"
log_init

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

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

if ! systemctl status ssh >/dev/null 2>&1; then
  printf 'ssh-server test SKIPPED: systemd status unavailable in this environment\n'
  exit 0
fi

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

if module_ssh_server_test "${1:-22}"; then
  printf 'ssh-server test OK\n'
else
  printf 'ssh-server test FAILED\n' >&2
  exit 1
fi
