{ stdenv, fetchFromGitHub, pkgs, lib, nodejs, nodePackages, pkg-config, libjpeg, pixman, cairo, pango }:

let
  # No official version ever released
  src = fetchFromGitHub {
    owner = "Sorunome";
    repo = "mx-puppet-slack";
    rev = "691e53d2d703bd169e1f23a8d8dff3f008d8c4ef";
    sha256 = "1yczhfpa4qzvijcpgc2pr10s009qb6jwlfwpcbb17g2wsx6zj0c2";
  };

  myNodePackages = import ./node-composition.nix {
    inherit pkgs nodejs;
    inherit (stdenv.hostPlatform) system;
  };

in myNodePackages.package.override {
  pname = "mx-puppet-slack";

  inherit src;

  nativeBuildInputs = [ nodePackages.node-pre-gyp pkg-config ];
  buildInputs = [ libjpeg pixman cairo pango ];

  postInstall = ''
    # Patch shebangs in node_modules, otherwise the webpack build fails with interpreter problems
    patchShebangs --build "$out/lib/node_modules/mx-puppet-slack/node_modules/"
    # compile Typescript sources
    npm run build

    # Make an executable to run the server
    mkdir -p $out/bin
    cat <<EOF > $out/bin/mx-puppet-slack
    #!/bin/sh
    exec ${nodejs}/bin/node $out/lib/node_modules/mx-puppet-slack/build/index.js "\$@"
    EOF
    chmod +x $out/bin/mx-puppet-slack
  '';

  meta = with lib; {
    description = "A slack puppeting bridge for matrix";
    license = licenses.asl20;
    homepage = "https://github.com/Sorunome/mx-puppet-slack";
    maintainers = with maintainers; [ chickensoupwithrice ];
    platforms = platforms.unix;
  };
}

