# SPDX-FileCopyrightText: 2025 toastal <toastal@posteo.net>
# SPDX-License-Identifier: MPL-2.0
{
  lib,
  stdenvNoCC,
  findutils,
  makeWrapper,
  parallel,
  dash,
  h2o,
  nickel,
  nixVersions,
  soupault,
  tup,
}:

let
  fs = lib.fileset;
in
stdenvNoCC.mkDerivation {
  pname = "w3m-site_scripts";
  version = "0-unstable";
  src = fs.toSource {
    root = ../../bin;
    fileset = fs.fileFilter (file: file.hasExt "sh") ../../bin;
  };
  nativeBuildInputs = [
    findutils
    makeWrapper
    parallel
  ];
  buildInputs = [ dash ];
  patchPhase = ''
    patchShebangs .
  '';
  dontBuild = true;
  installPhase = ''
    mkdir -p "$out/bin"
    export -f assertExecutable makeShellWrapper wrapProgram wrapProgramShell
    find . -type f -iname "*.sh" \
      | parallel ${
        lib.strings.escapeShellArgs [
          "--will-cite"
          "-j $NIX_BUILD_CORES"
          # Lazily, just assume all scripts need the same $PATH
          # sh
          ''
            mv {} "$out/bin/{/}"
            wrapProgram "$out/bin/{/}" --prefix PATH : ${
              lib.makeBinPath [
                h2o
                nickel
                nixVersions.stable
                soupault
                tup
              ]
            }
          ''
        ]
      }
  '';
}
