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

let
  version = "19.05";
  tarball = fetchurl {
    url = "https://github.com/genodelabs/genode/archive/19.05.tar.gz";
    sha256 = "1mr81885ysjff7plfvav6cr3lx5yhlcyq63q8c8bfvgi1fcarbww";
  };
in
stdenv.mkDerivation {
  name = "genode-sdk-${version}";
  inherit tarball version;

  phases = "mainPhase";

  toolchain_dir = toolchain.out;
  tool_prefix = "genode-x86-";

  spec_includes="-I\${includedir}/spec/x86_64 -I\${includedir}/spec/x86 -I\${includedir}/spec/64bit";

  mainPhase = ''
    export includedir=$out/include
    export libdir=$out/lib
    pkg_config_dir=$out/pkg-config
    mkdir -p $includedir $pkg_config_dir

    for i in ${./pkg-config}/*.pc; do
       substituteAll "$i" "$out/pkg-config/`basename $i`"
     done

    tar xfv ${tarball} \
      --transform='s|genode-${version}/repos/base/include|genode-base/|' \
      --transform='s|genode-${version}/repos/os/include|genode-os/|' \
      --transform='s|genode-${version}/repos/gems/include|genode-gems/|' \
      -C $includedir \
      genode-${version}/repos/base/include \
      genode-${version}/repos/os/include \
      genode-${version}/repos/gems/include \
  '';
}
