#!/bin/sh -ex

# pbis-open patch script

# Remove static libraries and other unused bits
find . -name '*.la' -delete
rm -r \
  etc \
  lib32 \
  opt/pbis/bin \
  opt/pbis/lib/apache2.* \
  opt/pbis/lib32 \
  opt/pbis/sbin \
  opt/pbis/share/fedora \
  opt/pbis/share/rhel \
  usr \
  var

# Move libnss_lsass.so somewhere that ZRTE's ld.so will look for it
mkdir lib/x86_64-linux-gnu
mv lib/libnss_lsass.so.2 lib/x86_64-linux-gnu/libnss_lsass.so.2

# The libraries that PAM looks for use RPATH=/opt/pbis/lib to find their
# bundled libraries, so replace that with a relative path.
patchelf --set-rpath '$ORIGIN/../../opt/pbis/lib' lib/security/pam_lsass.so
patchelf --set-rpath '$ORIGIN/../../opt/pbis/lib' lib/x86_64-linux-gnu/libnss_lsass.so.2
find opt/pbis/lib -type f | xargs patchelf --set-rpath '$ORIGIN'
