# SPDX-FileCopyrightText: 2020 Luke Granger-Brown <depot@lukegb.com>
#
# SPDX-License-Identifier: Apache-2.0

{ stdenv
, lib
, fetchFromGitHub
, autoreconfHook
, makeWrapper
, coreutils
, gnugrep
, ghostscript
, poppler_utils
, netpbm
}:

stdenv.mkDerivation rec {
  pname = "intermec-cups-driver";
  version = "v1.2-56";

  src = fetchFromGitHub {
    owner = "lukegb";
    repo = "intermec-cupsdriver";
    rev = version;
    sha256 = "sha256:1pk3qj05w2y891n5hayaw9a9g5w83fbm2kz5f7v74dsg8kcx365i";

    # Downloaded from
    # https://support.honeywellaidc.com/s/article/OVERVIEW-The-Honeywell-InterDriver-package-for-CUPS-on-Linux-UNIX
    # (their SSL serving is broken and can't be used directly with fetchurl, and the URL itself doesn't look stable)

    #  sha256sum ./CUPS-driver_v1.2-56.tgz 
    #  70c7305ca791e40c0d8cf458d4cd8fed7a2ad0558039b9e118a3a82b12e55f05  ./CUPS-driver_v1.2-56.tgz
  };

  nativeBuildInputs = [ makeWrapper autoreconfHook ];

  postInstall = ''
    for f in $out/share/cups/model/intermec/*.ppd; do
      substituteInPlace "$f" \
        --replace /usr/bin/ "$out/bin/"
    done

    substituteInPlace $out/bin/intermec-dp-drv \
      --replace pbmtodp "$out/bin/pbmtodp"

    wrapProgram $out/bin/intermec-dp-drv \
      --prefix PATH ":" ${lib.makeBinPath [
        # cat cp cut head
        coreutils

        # grep
        gnugrep

        # ps2pdf
        ghostscript

        # pdf2ppm
        poppler_utils

        # pgmtopbm pnmtoplainpnm
        netpbm
      ]}
  '';
}