#
# Fixes signald to work with new Signal API changes.
#
# vim: et:ts=2:sw=2:
#
{ ... }:
let
  overlay = final': prev': {
    signald = prev'.signald.overrideAttrs (prev: rec {

      # Use a date, since we're building off git.
      version = "2024-06-12";

      # Use a WIP version with current fixes.
      src = prev'.fetchgit {
        rev = "7863854ec8d8dcaa8b2b79e4ed2867a9ee43ca82";
        url = "https://gitlab.com/signald/signald.git";
        hash = "sha256-nZo0pjgbaKLLUPekdoKXpFgvB7ZM2CJgeA85xXaC3LA=";

        # Leave enough git info for their local script to `git describe`.
        deepClone = true;
      };

      # fake build to pre-download deps into fixed-output derivation
      gradleWithJdk = prev'.gradle.override { java = prev'.jdk17_headless; };
      deps = prev'.stdenv.mkDerivation {
        pname = "${prev.pname}-deps";
        inherit src version;

        nativeBuildInputs = with prev'; [
          git
          gradleWithJdk
          perl
        ];

        patches = [ ./0001-fetch-buildconfig-during-gradle-build-inside-nix-fod.patch ];
        buildphase = ''
          export GRADLE_USER_HOME=$(mktemp -d)
          gradle --no-daemon build
        '';
        installPhase = ''
          find $GRADLE_USER_HOME/caches/modules-2 -type f -regex '.*\.\(jar\|pom\)' \
            | perl -pe 's#(.*/([^/]+)/([^/]+)/([^/]+)/[0-9a-f]{30,40}/([^/\s]+))$# ($x = $2) =~ tr|\.|/|; "install -Dm444 $1 \$out/$x/$3/$4/$5" #e' \
            | sh -x

          # WARNING: don't try this at home and wear safety-goggles while working with this!
          # We patch around in the dependency tree to resolve some spurious dependency resolution errors.
          # Whenever this package gets updated, please check if some of these hacks are obsolete!

          # Mimic existence of okio-3.2.0.jar. Originally known as okio-jvm-3.2.0 (and renamed),
          # but gradle doesn't detect such renames, only fetches the latter and then fails
          # in `signald.buildPhase` because it cannot find `okio-3.2.0.jar`.
          pushd $out/com/squareup/okio/okio/3.2.0 &>/dev/null
            cp -v ../../okio-jvm/3.2.0/okio-jvm-3.2.0.jar okio-3.2.0.jar
          popd &>/dev/null

          # For some reason gradle fetches 2.14.1 instead of 2.14.0 here even though 2.14.0 is required
          # according to `./gradlew -q dependencies`, so we pretend to have 2.14.0 available here.
          # According to the diff in https://github.com/FasterXML/jackson-dataformats-text/compare/jackson-dataformats-text-2.14.0...jackson-dataformats-text-2.14.1
          # the only relevant change is in the code itself (and in the tests/docs), so this seems
          # binary-compatible.
          cp -v \
            $out/com/fasterxml/jackson/dataformat/jackson-dataformat-toml/2.14.1/jackson-dataformat-toml-2.14.1.jar \
            $out/com/fasterxml/jackson/dataformat/jackson-dataformat-toml/2.14.0/jackson-dataformat-toml-2.14.0.jar
        '';
        # Don't move info to share/
        forceShare = [ "dummy" ];
        outputHashAlgo = "sha256";
        outputHashMode = "recursive";
        # Downloaded jars differ by platform
        outputHash =
          {
            x86_64-linux = "sha256-9DHykkvazVBN2kfw1Pbejizk/R18v5w8lRBHZ4aXL5Q=";
            aarch64-linux = "sha256-RgAiRbUojBc+9RN/HpAzzpTjkjZ6q+jebDsqvah5XBw=";
          }
          .${prev'.stdenv.system} or (throw "Unsupported system: ${prev'.stdenv.system}");
      };
    });
  };
in
{
  nixpkgs.overlays = [ overlay ];
}
