#!/bin/sh
set -euC

# Distros (or upstream git, I'm not sure) make it very annoying to actually use
# the shipped `diff-highlight`. The following things vary depending on distro
# and version:
# - Where it is located
# - If it has a file extension
# - Whether it's executable
# - Whether has a shebang
# Therefore, we are left with this shim which does its best to actually run the
# thing.

for file in \
  '/usr/share/doc/git/contrib/diff-highlight/diff-highlight'* \
  '/usr/share/git-core/contrib/diff-highlight'* \
  '/usr/share/git/diff-highlight/diff-highlight'*
do
  # Does it exist?
  [ -r "$file" ] || continue
  # Is it executable?
  [ -x "$file" ] && exec "$file" || true
  # Does it have a shebang?
  interp="$(sed -n '1s/^#!//p' "$file")"
  [ "$interp" ] && exec $interp "$file" "$@" || true
  # Does it have a file extension which matches a command?
  ext="${file##*.}"
  [ "$(command -v "$ext")" ] && exec "$ext" "$file" "$@" || true
done

# Fallback.
exec cat