{
  config,
  lib,
  pkgs,
  ...
}: {
  sops.secrets = lib.listToAttrs (
    map
    (number:
      lib.nameValuePair "yate/accounts/easybell-${toString number}" {
        sopsFile = ../../secrets.yaml;
        owner = "yate";
      })
    (lib.lists.range 2 9)
  );

  environment.etc."yate/accfile.conf" = {
    mode = "symlink";
    source = "/var/run/yate/accfile.conf";
  };

  systemd.services.yate.serviceConfig = let
    easybellAccount = name: username: let
      title = "easybell-${toString name}";
      secretPath = config.sops.secrets."yate/accounts/${title}".path;
    in ''
      [${title}]
      enabled=yes
      protocol=sip
      username=${username}
      password=$(cat "${secretPath}")
      registrar=pbx.easybell.de
    '';
    accounts = [
      (easybellAccount 2 "CPBX-61tkfwsx-000004")
      (easybellAccount 3 "CPBX-61tkfwsx-000005")
      (easybellAccount 4 "CPBX-61tkfwsx-000006")
      (easybellAccount 5 "CPBX-61tkfwsx-000007")
      (easybellAccount 6 "CPBX-61tkfwsx-000008")
      (easybellAccount 7 "CPBX-61tkfwsx-000009")
      (easybellAccount 8 "CPBX-61tkfwsx-000010")
      (easybellAccount 9 "CPBX-61tkfwsx-000011")
    ];
  in {
    RuntimeDirectory = "yate";
    RuntimeDirectoryMode = lib.mkForce "2750";
    ExecStartPre = pkgs.writeShellScript "yate-pre-start" ''
      cat > "$RUNTIME_DIRECTORY/accfile.conf" << EOF
      ${lib.concatStringsSep "\n" accounts}
      EOF
    '';
  };

  services.yate.config = {
    yate.modules."regexroute.yate" = "enable";
    regexroute.default = let
      matchCalled = account: ''''${called}^${account}$'';
    in {
      "${matchCalled "CPBX-61tkfwsx-000004"}" = "sip/sip:1337@192.168.98.11";
      #"^.*$" = ''echo REGEXROUTE DEBUG called=''${called} address=''${address} callsource=''${callsource} formats=''${formats} id=''${id} peerid=''${peerid} ip_host=''${ip_host} ip_port=''${ip_port} overlapped=''${overlapped} rtp_forward=''${rtp_forward} type=''${type} username=''${username} line=''${line} account=''${account} caller=''${caller} called=''${called} module=''${module}'';
    };
  };
}
