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

let
  stateDir = "/var/lib/dnsmasq";
  blocklist = "${stateDir}/dnsmasq.blacklist.txt";

in {
  # PostgreSQL daily backups
  services.postgresqlBackup = {
    enable = true;
    backupAll = true;
    location = "/vault/backups/zion/databases";
    startAt = "*-*-* 05:15:00";
  };

  # Fetch hosts-blocklists daily
  systemd.services.download-dns-blocklist = {
    description = "Download hosts-blocklists";
    wantedBy = [ "default.target" ];
    path = with pkgs; [ curl ];
    script =
      "curl -L https://github.com/notracking/hosts-blocklists/raw/master/dnsmasq/dnsmasq.blacklist.txt -o ${blocklist}";
    serviceConfig.Type = "oneshot";
    postStop = ''
      chown -R dnsmasq ${stateDir}
      systemctl restart dnsmasq
    '';
    after = [ "wireguard-wg0.service" ];
    startAt = "02:00:00";
  };

  # Enable SATA HAT
  systemd.services.sata-hat = let
    overlay-directory =
      "/boot/nixos/4mamyanz1hlc4wz3c427qjh6rabngwvj-linux-5.10.17-1.20210303-dtbs/overlays/";
  in {
    description = "Enable software support for SATA Hat";
    wantedBy = [ "zfs-import.target" ];
    script = ''
      ${pkgs.bash}/bin/bash -c "/etc/nixos/scripts/SATA-hat.sh on"
    '';
    preStart = ''
      ${pkgs.libraspberrypi}/bin/dtoverlay -d ${overlay-directory} pwm-2chan pin=12 func=4 pin2=13 func2=4
      ${pkgs.libraspberrypi}/bin/dtoverlay -d ${overlay-directory} w1-gpio
    '';
    preStop = ''
      ${pkgs.libraspberrypi}/bin/dtoverlay -r -d ${overlay-directory} pwm-2chan
      ${pkgs.libraspberrypi}/bin/dtoverlay -r -d ${overlay-directory} w1-gpio
      ${pkgs.bash}/bin/bash -c "/etc/nixos/scripts/SATA-hat.sh off"
    '';
    serviceConfig = {
      Type = "oneshot";
      RemainAfterExit = "yes";
    };
    before = [ "zfs-import.target" "zfs-import-vault.service" "umount.target" ];
    requires = [ "systemd-udev-settle.service" ];
    after = [ "systemd-udev-settle.service" ];
    conflicts = [ "umount.target" ];
  };

  # HACK: restart services dependent on ZFS afer mount
  systemd.services.restart-services-mount = {
    description = "Restart services after the ZFS dataset is mounted";
    wantedBy = [ "default.target" ];
    script = ''
      sleep 5
      systemctl restart syncthing
      systemctl restart radicale
      systemctl restart gitea
    '';
    serviceConfig.Type = "oneshot";
    requires = [ "sata-hat.service" ];
    after = [ "vault.mount" ];
  };

  # Idle HDDs when not used
  systemd.services.hd-idle = {
    description = "Idle HDDs when not in use";
    wantedBy = [ "default.target" ];
    path = with pkgs; [ hd-idle ];
    script = "${pkgs.hd-idle}/bin/hd-idle";
    serviceConfig.Type = "simple";
    requires = [ "sata-hat.service" ];
    after = [ "vault.mount" ];
  };

  # Push zion changes to git daily
  systemd.user.services.zion-push = {
    description = "Push zion changes to git";
    path = with pkgs; [ git ];
    script = ''
      ${pkgs.git}/bin/git -C /home/coolneng/system pull
      ${pkgs.git}/bin/git -C /home/coolneng/system push
    '';
    serviceConfig.Type = "oneshot";
    startAt = "07:00:00";
    after = [ "network-online.target" ];
  };
}