{
  lib,
  self,
  config,
  hostPkgs,
  ...
}:
let
  testDir = "/mnt/hdd/samba";
in
{
  name = "service-samba";
  result.update-vars =
    let
      relativeDir = lib.removePrefix "${self}/" (toString config.clan.directory);
    in
    hostPkgs.writeShellScriptBin "update-vars" ''
      set -x
      export PRJ_ROOT=$(git rev-parse --show-toplevel)
      ${
        self.inputs.clan-core.packages.${hostPkgs.system}.clan-cli
      }/bin/clan-generate-test-vars $PRJ_ROOT/${relativeDir} ${config.name}
    '';

  clan = {
    test.useContainers = false;
    directory = ./.;
    inventory = {
      machines.server = { };

      instances = {
        samba-test = {
          module.name = "@clan/samba";
          module.input = "self";
          roles.server.machines."server".settings = {
            dataDir = testDir;
            globalUsers = {
              guser1 = {
                readPerm = true;
                writePerm = true;
              };
            };
            sharedFolders = {
              "share1" = {
                users = {
                  "u1share1" = { };
                  "u2share1" = { };
                };
              };
              "share2" = {
                users = {
                  "u1share2" = { };
                  "u2share2" = { };
                };
              };
              "share3" = { };
            };
          };
        };
      };
    };
  };

  nodes = {
    server = { };
  };

  testScript = ''
    start_all()

    server.wait_for_unit("samba-smbd")

    server.succeed("systemctl status samba-smbd")

    server.succeed("test -d ${testDir}/guser1")
    server.succeed("test -d ${testDir}/u1share1")
    server.succeed("test -d ${testDir}/u2share1")
    server.succeed("test -d ${testDir}/u1share2")
    server.succeed("test -d ${testDir}/u2share2")
    server.succeed("test -d ${testDir}/share1")
    server.succeed("test -d ${testDir}/share2")

    server.succeed("runuser -u guser1 -- test -r ${testDir}/share1")
    server.succeed("runuser -u guser1 -- test -r ${testDir}/share1")
    server.succeed("runuser -u u1share1 -- test -r ${testDir}/share1")
  '';
}
