40990b
#!/bin/bash -eu
257a3a
257a3a
# If using normal root, avoid changing anything.
257a3a
if [ -z "$RPM_BUILD_ROOT" -o "$RPM_BUILD_ROOT" = "/" ]; then
257a3a
  exit 0
257a3a
fi
257a3a
f7e8f7
exclude_files=""
f7e8f7
exclude_files_from=""
f7e8f7
exclude_shebangs=""
f7e8f7
exclude_shebangs_from=""
f7e8f7
f7e8f7
usage() {
f7e8f7
  local verbose=$1 && shift
f7e8f7
  local outfile=$1 && shift
f7e8f7
  local status=$1 && shift
f7e8f7
f7e8f7
  (
f7e8f7
    echo 'usage: brp-mangle-shebangs [--files <regexp>] [--files-from <file>] [--shebangs <regexp>] [--shebangs-from <file>]'
f7e8f7
    if [ "${verbose}" == "yes" ]; then
f7e8f7
      echo '  --files: extended regexp of files to ignore'
f7e8f7
      echo '  --files-from: file containing a list of extended regexps of files to ignore'
f7e8f7
      echo '  --shebangs: extended regexp of shebangs to ignore'
f7e8f7
      echo '  --shebangs-from: file containing a list of extended regexps of shebangs to ignore'
f7e8f7
    fi
f7e8f7
  ) >>${outfile}
f7e8f7
  exit ${status}
f7e8f7
}
f7e8f7
f7e8f7
while [ $# -gt 0 ] ; do
f7e8f7
  case "$1" in
f7e8f7
    --files)
f7e8f7
      exclude_files="${2}"
f7e8f7
      shift
f7e8f7
      ;;
f7e8f7
    --files=*)
f7e8f7
      exclude_files="${1##--files=}"
f7e8f7
      ;;
f7e8f7
    --files-from)
f7e8f7
      exclude_files_from="${2}"
f7e8f7
      shift
f7e8f7
      ;;
f7e8f7
    --files-from=*)
f7e8f7
      exclude_files_from="${1##--files-from=}"
f7e8f7
      ;;
f7e8f7
    --shebangs)
f7e8f7
      exclude_shebangs="${2}"
f7e8f7
      shift
f7e8f7
      ;;
f7e8f7
    --shebangs=*)
f7e8f7
      exclude_shebangs="${1##--shebangs=}"
f7e8f7
      ;;
f7e8f7
    --shebangs-from)
f7e8f7
      exclude_shebangs_from="${2}"
f7e8f7
      shift
f7e8f7
      ;;
f7e8f7
    --shebangs-from=*)
f7e8f7
      exclude_shebangs_from="${1##--shebangs-from=}"
f7e8f7
      ;;
f7e8f7
    --help|--usage|"-?"|-h)
f7e8f7
      usage yes /dev/stdout 0
f7e8f7
      ;;
f7e8f7
    *)
f7e8f7
      echo "Unknown option \"${1}\"" 1>&2
f7e8f7
      usage no /dev/stderr 1
f7e8f7
      ;;
f7e8f7
  esac
f7e8f7
  shift
f7e8f7
done
fb05a2
257a3a
cd "$RPM_BUILD_ROOT"
257a3a
257a3a
trim() {
257a3a
  printf '%s' "$*"
257a3a
}
257a3a
257a3a
fail=0
40990b
while IFS= read -r -d $'\0' f; do
40990b
  file -N --mime-type "$f" | grep -q -P ".+(?=: text/)" || continue
257a3a
fb05a2
  # Remove the dot
fb05a2
  path="${f#.}"
fb05a2
fb05a2
  if [ -n "$exclude_files" ]; then
fb05a2
    echo "$path" | grep -q -E "$exclude_files" && continue
fb05a2
  fi
f7e8f7
  if [ -n "$exclude_files_from" ]; then
f7e8f7
    echo "$path" | grep -q -E -f "$exclude_files_from" && continue
f7e8f7
  fi
fb05a2
257a3a
  ts=$(stat -c %y "$f")
257a3a
8f59b7
  read shebang_line < "$f" || :
8f59b7
  orig_shebang=$(trim $(echo "$shebang_line" | grep -Po "#!\K.*" || echo))
8f59b7
  shebang="$orig_shebang"
fb05a2
  if [ -n "$exclude_shebangs" ]; then
fb05a2
    echo "$shebang" | grep -q -E "$exclude_shebangs" && continue
fb05a2
  fi
f7e8f7
  if [ -n "$exclude_shebangs_from" ]; then
f7e8f7
    echo "$shebang" | grep -q -E -f "$exclude_shebangs_from" && continue
f7e8f7
  fi
fb05a2
257a3a
  if [ -z "$shebang" ]; then
257a3a
    echo >&2 "*** WARNING: $f is executable but has empty or no shebang, removing executable bit"
257a3a
    chmod -x "$f"
257a3a
    touch -d "$ts" "$f"
257a3a
    continue
51f1c6
  elif [ -n "${shebang##/*}" ]; then
257a3a
    echo >&2 "*** ERROR: $f has shebang which doesn't start with '/' ($shebang)"
257a3a
    fail=1
257a3a
    continue
257a3a
  fi
257a3a
257a3a
  if ! { echo "$shebang" | grep -q -P "^/(?:usr/)?(?:bin|sbin)/"; }; then
257a3a
    continue
257a3a
  fi
257a3a
257a3a
  # Replace "special" env shebang:
35f718
  # /whatsoever/env /whatever/foo → /whatever/foo
35f718
  shebang=$(echo "$shebang" | sed -r -e 's@^(.+)/env /(.+)$@/\2@')
257a3a
  # /whatsoever/env foo → /whatsoever/foo
257a3a
  shebang=$(echo "$shebang" | sed -r -e 's@^(.+/)env (.+)$@\1\2@')
257a3a
973e5c
  # If the shebang now starts with /bin, change it to /usr/bin
973e5c
  # https://bugzilla.redhat.com/show_bug.cgi?id=1581757
973e5c
  shebang=$(echo "$shebang" | sed -r -e 's@^/bin/@/usr/bin/@')
973e5c
257a3a
  # Replace ambiguous python with python2
257a3a
  py_shebang=$(echo "$shebang" | sed -r -e 's@/usr/bin/python(\s|$)@/usr/bin/python2\1@')
257a3a
257a3a
  if [ "$shebang" != "$py_shebang" ]; then
b852a7
    echo >&2 "*** ERROR: ambiguous python shebang in $path: #!$orig_shebang. Change it to python3 (or python2) explicitly."
b852a7
    fail=1
8f59b7
  elif [ "#!$shebang" != "#!$orig_shebang" ]; then
257a3a
    sed -i -e "1c #!$shebang" "$f"
fb05a2
    echo "mangling shebang in $path from $orig_shebang to #!$shebang"
257a3a
  fi
257a3a
257a3a
  touch -d "$ts" "$f"
40990b
done < <(find -executable -type f -print0)
257a3a
257a3a
exit $fail