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