{ lib, ... }:
{
  services.xscreensaver = {
    enable = true;
  };

  home.activation.xscreensaver = lib.hm.dag.entryAfter ["writeBoundary"] ''
    conf=$HOME/.xscreensaver

    if grep -q '^fade:'$'\t' $conf; then
      run sed -i -e 's/^fade:\t.*$/fade:\t\tFalse/' $conf
    else
      run echo -e 'fade:\t\tFalse' >> $conf
    fi

    if grep -q '^mode:'$'\t' $conf; then
      run sed -i -e 's/^mode:\t.*$/mode:\t\trandom/' $conf
    else
      run echo -e 'mode:\t\trandom' >> $conf
    fi

    if grep -q '^splash:'$'\t' $conf; then
      run sed -i -e 's/^splash:\t.*$/splash:\t\tFalse/' $conf
    else
      run echo -e 'splash:\t\tFalse' >> $conf
    fi

    if grep -q '^timeout:'$'\t' $conf; then
      run sed -i -e 's/^timeout:\t.*$/timeout:\t0:10:00/' $conf
    else
      run echo -e 'timeout:\t0:10:00' >> $conf
    fi

    if /run/current-system/sw/bin/systemctl --user --quiet is-active xscreensaver.service; then
      /run/current-system/sw/bin/systemctl --user restart xscreensaver.service
    fi
  '';
}
