{ config, lib, pkgs, ... }:

with lib;

let

  concatMapLines = f: l: lib.concatStringsSep "\n" (map f l);

  cfg = config.services.mlmmj;
  stateDir = "/var/lib/mlmmj";
  spoolDir = "/var/spool/mlmmj";
  listDir = domain: list: "${spoolDir}/${domain}/${list}";
  listCtl = domain: list: "${listDir domain list}/control";
  transport = domain: list: "${domain}--${list}@local.list.mlmmj mlmmj:${domain}/${list}";
  virtual = domain: list: "${list}@${domain} ${domain}--${list}@local.list.mlmmj";
  alias = domain: list: "${list}: \"|${pkgs.mlmmj}/bin/mlmmj-receive -L ${listDir domain list}/\"";
  subjectPrefix = list: "[${list}]";
  listAddress = domain: list: "${list}@${domain}";
  customHeaders = domain: list: [ "List-Id: ${list}" "Reply-To: ${list}@${domain}" ];
  footer = domain: list: "-- \nTo unsubscribe send a mail to ${list}+unsubscribe@${domain}";
  createList = d: l:
    let ctlDir = listCtl d l; in
    ''
      for DIR in incoming queue queue/discarded archive text subconf unsubconf \
                 bounce control moderation subscribers.d digesters.d requeue \
                 nomailsubs.d
      do
             mkdir -p '${listDir d l}'/"$DIR"
      done
      ${pkgs.coreutils}/bin/mkdir -p ${ctlDir}
      echo ${listAddress d l} > '${ctlDir}/listaddress'
      [ ! -e ${ctlDir}/customheaders ] && \
          echo "${lib.concatStringsSep "\n" (customHeaders d l)}" > '${ctlDir}/customheaders'
      [ ! -e ${ctlDir}/footer ] && \
          echo ${footer d l} > '${ctlDir}/footer'
      [ ! -e ${ctlDir}/prefix ] && \
          echo ${subjectPrefix l} > '${ctlDir}/prefix'
    '';
  customHeaderOptions = {
    name = lib.mkOption {
      type = types.str;
    };
    value = lib.mkOption {
      type = types.str;
    };
  }

  mailingListConfig = config: {
    settings = {
      use_template = lib.mkDefault config.useTemplate;
      recursive = lib.mkDefault config.recursive;
      process_children_only = lib.mkDefault config.processChildrenOnly;
    };
  };

in

{

  ###### interface

  options = {

    services.mlmmj = {

      enable = mkOption {
        type = types.bool;
        default = false;
        description = "Enable mlmmj";
      };

      user = mkOption {
        type = types.str;
        default = "mlmmj";
        description = "mailinglist local user";
      };

      group = mkOption {
        type = types.str;
        default = "mlmmj";
        description = "mailinglist local group";
      };

      listDomain = mkOption {
        type = types.str;
        default = "localhost";
        description = "Set the mailing list domain";
      };

      mailLists = mkOption {
        type = types.listOf types.str;
        default = [];
        description = "The collection of hosted maillists";
      };

      maillists = lib.mkOption {
        type = types.attrsOf (types.submodule (domain: {
          options = {
            type = types.attrsOf (types.submodule (list: {
              options = {
                customHeaders = lib.mkOption {
                  type = types.listOf customHeaderOptions;
                  default = [
                    "List-Id: ${list.name}"
                    "Reply-To: ${list.name}@${domain.name}"
                  ];
                  description = "Custom headers.";
                };
                footer = lib.mkOption {
                  type = types.lines;
                  default = "-- \nTo unsubscribe send a mail to ${list.name}+unsubscribe@${domain.name}";
                  description = "Text to be added as a footer to the mails' body.";
                };
                lang = lib.mkOption {
                  type = types.enum ["ast" "cs" "de" "en" "fi" "fr" "gr" "it" "pt" "sk" "zh-cn"];
                  default = "en";
                  description = "Language of the templates.";
                };
                subjectPrefix = lib.mkOption {
                  type = types.str;
                  default = "[${list.name}]";
                  description = "String added as a prefix to the Subject header.";
                };
              };
              config = mailingListConfig config;
            }))
          }
        }));
        default = {};
        description = "Mailing-lists.";
      };

      maintInterval = mkOption {
        type = types.str;
        default = "20min";
        description = ''
          Time interval between mlmmj-maintd runs, see
          <citerefentry><refentrytitle>systemd.time</refentrytitle>
          <manvolnum>7</manvolnum></citerefentry> for format information.
        '';
      };

      postfix = {
        enable = mkOption {
          type = types.bool;
          default = false;
          description = "Configure postfix to use mlmmj as a transport";
        };
      };

    };

  };

  ###### implementation

  config = mkIf cfg.enable {

    users.users.${cfg.user} = {
      description = "mlmmj user";
      home = stateDir;
      createHome = true;
      uid = config.ids.uids.mlmmj;
      group = cfg.group;
      useDefaultShell = true;
    };

    users.groups.${cfg.group} = {
      gid = config.ids.gids.mlmmj;
    };

    services.postfix = mkIf cfg.postfix.enable {
      enable = true;
      recipientDelimiter= "+";
      masterConfig.mlmmj = {
        type = "unix";
        private = true;
        privileged = true;
        chroot = false;
        wakeup = 0;
        command = "pipe";
        args = [
          "flags=ORhu"
          "user=${cfg.user}:${cfg.group}"
          "argv=${pkgs.mlmmj}/bin/mlmmj-receive"
          "-F"
          "-L"
          "${spoolDir}/$nexthop"
        ];
      };

      extraAliases = lib.concatMapStringsSep "\n" (alias cfg.listDomain) cfg.mailLists;

      config = {
        propagate_unmatched_extensions = ["virtual"];
        mlmmj_destination_recipient_limit = "1";
      };

      virtual = lib.concatMapStringsSep "\n" (virtual cfg.listDomain) cfg.mailLists;
      transport = lib.concatMapStringsSep "\n" (transport cfg.listDomain) cfg.mailLists;
    };

    environment.systemPackages = [ pkgs.mlmmj ];

    system.activationScripts.mlmmj = ''
          ${pkgs.coreutils}/bin/mkdir -p ${stateDir} ${spoolDir}/${cfg.listDomain}
          ${lib.concatMapStringsSep "\n" (createList cfg.listDomain) cfg.mailLists}
          ${pkgs.coreutils}/bin/chown -R ${cfg.user}:${cfg.group} ${spoolDir}
      '';

    systemd.services.mlmmj-maintd = {
      description = "mlmmj maintenance daemon";
      serviceConfig = {
        User = cfg.user;
        Group = cfg.group;
        ExecStart = "${pkgs.mlmmj}/bin/mlmmj-maintd -F -d ${spoolDir}/${cfg.listDomain}";
      };
    };

    systemd.timers.mlmmj-maintd = {
      description = "mlmmj maintenance timer";
      timerConfig.OnUnitActiveSec = cfg.maintInterval;
      wantedBy = [ "timers.target" ];
    };
  };

}