{ config, pkgs, ... }:

# Setup:
# Maxwell runs the web UI (magneticow) but doesn't
# run the crawler (magneticod) because it's too
# network intensive. The latter is run by Wigfrid,
# which periodically uploads a sqlite database.
# Once received, Maxwell merges it with the local one.

{
  ### Reverse proxy location
  services.nginx.virtualHosts."${config.var.hostname}" =
    { locations."/dht/" = {
        proxyPass  = "http://localhost:8082/";
        # Rewrite all absolute paths, magneticow
        # was not designed to work behind a proxy.
        extraConfig = ''
          sub_filter_once off;
          sub_filter_types *;
          sub_filter 'action="/'     'action="/dht/';
          sub_filter 'href="/'       'href="/dht/';
          sub_filter 'src="/'        'src="/dht/';
          sub_filter '/api/'         '/dht/api/';
          sub_filter '/feed?'        '/dht/feed?';
          sub_filter 'split("/")[2]' 'split("/").pop()';
        '';
      };
    };

  ### Magneticow
  services.magnetico = {
    enable = true;
    web.port = 8082;
    web.credentialsFile = config.secrets.passwords.magnetico;
  };

  # Disable the crawler: it's run by wigfrid
  systemd.services.magneticod.enable = false;

  # Start the database merge as soon
  # as a new one is uploaded.
  systemd.paths.merge-magnetico = {
    pathConfig.PathExists = "/var/lib/magnetico/update.sqlite3";
    wantedBy = [ "multi-user.target" ];
  };

  # Merge wigfrid update database with
  # the current one and restart magneticow.
  systemd.services.merge-magnetico = {
    path = [ pkgs.python3 ];
    script = ''
      set -e
      systemctl stop magneticow
      cd /var/lib/magnetico
      python3 ${./assets/magnetico-merge.py} database.sqlite3 update.sqlite3
      rm update.sqlite3
      systemctl start magneticow
    '';
  };

  # SSH access to allow uploading 
  # the magnetico database.
  users.users.magnetico = {
    useDefaultShell = true;
    openssh.authorizedKeys.keyFiles = [ config.secrets.publicKeys.magnetico ];
  };

}