f9efe2
#!/bin/bash -eu
f9efe2
f9efe2
# If using normal root, avoid changing anything.
f9efe2
if [ -z "$RPM_BUILD_ROOT" -o "$RPM_BUILD_ROOT" = "/" ]; then
f9efe2
  exit 0
f9efe2
fi
f9efe2
f9efe2
exclude_files=""
f9efe2
exclude_files_from=""
f9efe2
exclude_shebangs=""
f9efe2
exclude_shebangs_from=""
f9efe2
f9efe2
usage() {
f9efe2
  local verbose=$1 && shift
f9efe2
  local outfile=$1 && shift
f9efe2
  local status=$1 && shift
f9efe2
f9efe2
  (
f9efe2
    echo 'usage: brp-mangle-shebangs [--files <regexp>] [--files-from <file>] [--shebangs <regexp>] [--shebangs-from <file>]'
f9efe2
    if [ "${verbose}" == "yes" ]; then
f9efe2
      echo '  --files: extended regexp of files to ignore'
f9efe2
      echo '  --files-from: file containing a list of extended regexps of files to ignore'
f9efe2
      echo '  --shebangs: extended regexp of shebangs to ignore'
f9efe2
      echo '  --shebangs-from: file containing a list of extended regexps of shebangs to ignore'
f9efe2
    fi
f9efe2
  ) >>${outfile}
f9efe2
  exit ${status}
f9efe2
}
f9efe2
f9efe2
while [ $# -gt 0 ] ; do
f9efe2
  case "$1" in
f9efe2
    --files)
f9efe2
      exclude_files="${2}"
f9efe2
      shift
f9efe2
      ;;
f9efe2
    --files=*)
f9efe2
      exclude_files="${1##--files=}"
f9efe2
      ;;
f9efe2
    --files-from)
f9efe2
      exclude_files_from="${2}"
f9efe2
      shift
f9efe2
      ;;
f9efe2
    --files-from=*)
f9efe2
      exclude_files_from="${1##--files-from=}"
f9efe2
      ;;
f9efe2
    --shebangs)
f9efe2
      exclude_shebangs="${2}"
f9efe2
      shift
f9efe2
      ;;
f9efe2
    --shebangs=*)
f9efe2
      exclude_shebangs="${1##--shebangs=}"
f9efe2
      ;;
f9efe2
    --shebangs-from)
f9efe2
      exclude_shebangs_from="${2}"
f9efe2
      shift
f9efe2
      ;;
f9efe2
    --shebangs-from=*)
f9efe2
      exclude_shebangs_from="${1##--shebangs-from=}"
f9efe2
      ;;
f9efe2
    --help|--usage|"-?"|-h)
f9efe2
      usage yes /dev/stdout 0
f9efe2
      ;;
f9efe2
    *)
f9efe2
      echo "Unknown option \"${1}\"" 1>&2
f9efe2
      usage no /dev/stderr 1
f9efe2
      ;;
f9efe2
  esac
f9efe2
  shift
f9efe2
done
f9efe2
f9efe2
cd "$RPM_BUILD_ROOT"
f9efe2
f9efe2
# Large packages such as kernel can have thousands of executable files.
f9efe2
# We take care to not fork/exec thousands of "file"s and "grep"s,
f9efe2
# but run just two of them.
f9efe2
# (Take care to exclude filenames which would mangle "file" output).
f9efe2
find -executable -type f ! -path '*:*' ! -path $'*\n*' \
f9efe2
| file -N --mime-type -f - \
f9efe2
| grep -P ".+(?=: (text/|application/javascript))" \
f9efe2
| {
f9efe2
fail=0
f9efe2
while IFS= read -r line; do
f9efe2
  f=${line%%:*}
f9efe2
f9efe2
  # Remove the dot
f9efe2
  path="${f#.}"
f9efe2
f9efe2
  if [ -n "$exclude_files" ]; then
f9efe2
    echo "$path" | grep -q -E "$exclude_files" && continue
f9efe2
  fi
f9efe2
  if [ -n "$exclude_files_from" ]; then
f9efe2
    echo "$path" | grep -q -E -f "$exclude_files_from" && continue
f9efe2
  fi
f9efe2
f9efe2
f9efe2
  if ! read shebang_line < "$f"; then
f9efe2
    echo >&2 "*** WARNING: Cannot read the first line from $f, removing executable bit"
f9efe2
    ts=$(stat -c %y "$f")
f9efe2
    chmod -x "$f"
f9efe2
    touch -d "$ts" "$f"
f9efe2
    continue
f9efe2
  fi
f9efe2
f9efe2
  orig_shebang="${shebang_line#\#!}"
f9efe2
  if [ "$orig_shebang" = "$shebang_line" ]; then
f9efe2
    echo >&2 "*** WARNING: $f is executable but has no shebang, removing executable bit"
f9efe2
    ts=$(stat -c %y "$f")
f9efe2
    chmod -x "$f"
f9efe2
    touch -d "$ts" "$f"
f9efe2
    continue
f9efe2
  fi
f9efe2
f9efe2
  # Trim spaces
f9efe2
  while shebang="${orig_shebang//  / }"; [ "$shebang" != "$orig_shebang" ]; do
f9efe2
    orig_shebang="$shebang"
f9efe2
  done
f9efe2
  # Treat "#! /path/to " as "#!/path/to"
f9efe2
  orig_shebang="${orig_shebang# }"
f9efe2
f9efe2
  shebang="$orig_shebang"
f9efe2
f9efe2
  if [ -z "$shebang" ]; then
f9efe2
    echo >&2 "*** WARNING: $f is executable but has empty shebang, removing executable bit"
f9efe2
    ts=$(stat -c %y "$f")
f9efe2
    chmod -x "$f"
f9efe2
    touch -d "$ts" "$f"
f9efe2
    continue
f9efe2
  fi
f9efe2
  if [ -n "${shebang##/*}" ]; then
f9efe2
    echo >&2 "*** ERROR: $f has shebang which doesn't start with '/' ($shebang)"
f9efe2
    fail=1
f9efe2
    continue
f9efe2
  fi
f9efe2
f9efe2
  if ! { echo "$shebang" | grep -q -P "^/(?:usr/)?(?:bin|sbin)/"; }; then
f9efe2
    continue
f9efe2
  fi
f9efe2
f9efe2
  # Replace "special" env shebang:
f9efe2
  # /whatsoever/env /whatever/foo → /whatever/foo
f9efe2
  shebang=$(echo "$shebang" | sed -r -e 's@^(.+)/env /(.+)$@/\2@')
f9efe2
  # /whatsoever/env foo → /whatsoever/foo
f9efe2
  shebang=$(echo "$shebang" | sed -r -e 's@^(.+/)env (.+)$@\1\2@')
f9efe2
f9efe2
  # If the shebang now starts with /bin, change it to /usr/bin
f9efe2
  # https://bugzilla.redhat.com/show_bug.cgi?id=1581757
f9efe2
  shebang=$(echo "$shebang" | sed -r -e 's@^/bin/@/usr/bin/@')
f9efe2
f9efe2
  # Replace ambiguous python with python2
f9efe2
  py_shebang=$(echo "$shebang" | sed -r -e 's@/usr/bin/python(\s|$)@/usr/bin/python2\1@')
f9efe2
f9efe2
  if [ "$shebang" != "$py_shebang" ]; then
f9efe2
    echo >&2 "*** ERROR: ambiguous python shebang in $path: #!$orig_shebang. Change it to python3 (or python2) explicitly."
f9efe2
    fail=1
f9efe2
  elif [ "#!$shebang" != "#!$orig_shebang" ]; then
f9efe2
    echo "mangling shebang in $path from $orig_shebang to #!$shebang"
f9efe2
    ts=$(stat -c %y "$f")
f9efe2
    sed -i -e "1c #!$shebang" "$f"
f9efe2
    touch -d "$ts" "$f"
f9efe2
  fi
f9efe2
f9efe2
done
f9efe2
f9efe2
exit $fail
f9efe2
}