# This script finds all system libraries on which something in the package
# tree is dependent on.

# Require $prefix, $lsst_home, $hscpipe_ver
find_requisites()
(
  set -e

  source "${lsst_home}"/loadLSST.bash
  setup hscPipe "${hscpipe_ver}"

  cd "$prefix"

  set +e
  LANG=C find "$prefix" -type f -exec ldd {} \; 2> /dev/null |
    sed -n -e '/=>/ { s/^.*=> *\([^ (]*\).*$/\1/g ; p }' |
    grep -v -F "$prefix" |
    grep "^/" |
    tee /dev/stderr |
    sort | uniq > "$prefix"/requisites.txt

  set -e
  echo "=== Found requisites ==="
  cat "$prefix"/requisites.txt

  while read -r lib
  do
    patchelf --add-needed "$(basename "$lib")" "${prefix}/bin/check-requisites-hscPipe-${hscpipe_ver}"
  done < "$prefix"/requisites.txt
)
