#!/bin/sh

usage() {
    printf %s "\
  $(basename "$0")
    is a simple script filter for unified diffs

  $(basename "$0") [-CHT] [-pN|-a|-P PREFIX] [-s SUBST -r REPL] [-x EXCLUDE]

  commands:
    -C                   - Remove all non-diff lines.
    -H                   - Remove diff headers.
    -T                   - Remove file timestamps.
    -p [N]               - Strip N levels of directories from file names.
    -a                   - Add a/ b/ to file names.
    -P                   - Add PREFIX to file names.
    -s [SUBST] -r [REPL] - Replace regexp SUBST with REPL in filenames.
    -x [EXCLUDE]         - Exclude diffs touching files matching regexp EXCLUDE.
"
    exit 0
}

striptime=0
stripheader=0
stripprefix=0
addprefix=0
clean=0
subst=
repl=
exclude=
nl='
'

# shellcheck disable=SC2249
while getopts CHTap:P:s:r:x: name; do
  case ${name} in
  C) clean=1 ;;
  H) stripheader=1 ;;
  T) striptime=1 ;;
  a) addprefix=- ;;
  P) addprefix=${OPTARG%/} ;;
  p) stripprefix=$((0+OPTARG)) ;;
  s) subst=${OPTARG} ;;
  r) repl=${OPTARG} ;;
  x) exclude="${exclude}${nl}${OPTARG}" ;;
  ?) usage ;;
  esac
done

exec awk -v "striptime=${striptime}" \
   -v "stripheader=${stripheader}" \
   -v "stripprefix=${stripprefix}" \
   -v "addprefix=${addprefix}" \
   -v "subst=${subst}" \
   -v "repl=${repl}" \
   -v "excludes=${exclude}" \
   -v "clean=${clean}" '
BEGIN {
	excludes = substr(excludes, 2)
	split(excludes, exclude, "\n");
}

inpatch && /^[^ +-@]/ { inpatch = 0; strippatch = 0 }

/^diff |^Index: / { if (!inheader) header = $0; inheader = 1; next }

/^--- |\+\+\+ / {
	inheader = 0
	inpatch = 1
	s = substr($0, 1, 4)
	f = substr($0, 5)
	t = ""
	if (i = index(f, "\t")) {
		t = substr(f, i)
		f = substr(f, 0, i-1)
	}
	if (striptime)
		t = ""
	if (f != "/dev/null")
		for (i = 0; i < stripprefix; i++)
			sub("[^/]*\\/", "", f)
	if (subst && repl)
		gsub(subst, repl, f)
	if (addprefix == "-") {
		if (index($0, "---") == 1)
			f = "a/" f;
		else
			f = "b/" f;
	} else if (addprefix) {
		f = addprefix "/" f;
	}
	$0 = s f t
	for (e in exclude)
		if (match(f, exclude[e]))
			strippatch = 1
	if (!strippatch) {
		if (header && !stripheader)
			print header
		print $0
	}
	header = ""
	next
}

inheader { header = header "\n" $0; next }
inpatch && strippatch { next }
inpatch { print; next }

!clean { print }
'

