{ pkgs ? import <nixpkgs> {} }: with pkgs;

let
  genodeVersion = "19.05";
  toolchain = import /opt/genode/errata/toolchain.nix { inherit pkgs; };
  upstream = fetchFromGitHub {
    owner = "genodelabs";
    repo = "genode";
    rev = "19.05";
    sha256 = "01sgixyvsq5g7fd0nj9j67d9jma0bhlprwyjf21dql9hqn5k0ngf";
  };  
in
stdenv.mkDerivation {
  name = "genode-sdk-${genodeVersion}";
  version = genodeVersion;

  toolchain_dir = toolchain.out;

  inherit upstream;

  src = fetchurl {
    url = "https://github.com/ehmry/genode/releases/download/19.05-sdk/sdk.tar.xz";
    sha256 = "ab6f9c1856c535ed17960f66bf4fca0d82f49b7457ac21c2f22adb612d489aa9";
  };

  phases = [ "mainPhase" ];

  mainPhase = ''
    dest=$out/opt/genode
    mkdir -p $dest

    tar xf $src --strip-components=3 -C $dest

    for i in $dest/pkgconfig/*/*.pc; do
       substituteInPlace "$i" \
         --replace /opt/genode/sdk-${genodeVersion} $dest \
         --replace @TOOLCHAIN_DIR@ $toolchain_dir \
    ; done
  '';
}


