#!/usr/bin/env bash
#
# rpl - grep and sed to replace `$1` with `$2` recursively, print what is done

# shellcheck disable=SC2312

set -e

grep --null --recursive --files-with-matches --binary-files=without-match \
  --exclude-dir .sync --exclude-dir .stfolder --exclude-dir *build \
  --exclude-dir .git --exclude-dir .idea --exclude-dir env --exclude-dir out \
  --exclude-dir cache --exclude-dir Partitions --exclude-dir vendor/bundle \
  --exclude-dir log --exclude-dir .cache --exclude-dir .cpan \
  --exclude-dir *Cache --exclude-dir .pyenv --exclude-dir .local/cache \
  --exclude-dir share/cabal --exclude-dir share/cargo --exclude-dir share/gem \
  --exclude-dir share/tldr --exclude-dir share/syncthing \
  --exclude-dir share/virtualenv --exclude-dir vendor/cache \
  --exclude-dir generated --exclude-dir .gradle --exclude-dir cmake_build \
  --exclude-dir dist-newstyle --exclude-dir node_modules \
  --exclude-dir __pycache__ --exclude-dir .pytest_cache \
  --exclude-dir .nc_plugin "$1" -- "${@:3}" \
  | xargs -0 sed -i "\%${1}%{
    s||${2}|g
    w /dev/stdout
  }"
