# this package was called gimp-print in the past
{
  stdenv,
  lib,
  fetchurl,
  makeWrapper,
  pkg-config,
  ijs,
  zlib,
  gimp,
  cupsSupport ? true,
  cups,
  libusb1,
  perl,
}:

stdenv.mkDerivation (finalAttrs: {
  pname = "gutenprint";
  version = "5.3.5";

  src = fetchurl {
    url = "mirror://sourceforge/gimp-print/gutenprint-${finalAttrs.version}.tar.xz";
    hash = "sha256-9an0feKFMLGuIGnPvGR6mmQbruq+gJuw7ys+xblmjXA=";
  };

  strictDeps = true;
  nativeBuildInputs = [
    makeWrapper
    pkg-config
  ]
  ++ lib.optionals cupsSupport [
    cups
    perl
  ]; # for cups-config
  buildInputs = [
    ijs
    zlib
  ]
  ++ lib.optionals cupsSupport [
    cups
    libusb1
    perl
  ];

  configureFlags = lib.optionals cupsSupport [
    "--disable-static-genppd" # should be harmless on NixOS
  ];

  # FIXME: hacky because we modify generated configure, but I haven't found a better way.
  # makeFlags doesn't change this everywhere (e.g. in cups-genppdupdate).
  preConfigure = lib.optionalString cupsSupport ''
    sed -i \
      -e "s,cups_conf_datadir=.*,cups_conf_datadir=\"$out/share/cups\",g" \
      -e "s,cups_conf_serverbin=.*,cups_conf_serverbin=\"$out/lib/cups\",g" \
      -e "s,cups_conf_serverroot=.*,cups_conf_serverroot=\"$out/etc/cups\",g" \
      configure
  '';

  enableParallelBuilding = true;

  # Testing is very, very long.
  # doCheck = true;

  meta = {
    description = "Ghostscript and cups printer drivers";
    homepage = "https://sourceforge.net/projects/gimp-print/";
    license = lib.licenses.gpl2Plus;
    platforms = lib.platforms.linux;
    isGutenprint = true;
  };
})
