#!/bin/sh
dir="/usr/lib/bibblelabs/bibblepro/libs/"
ldsoconf="/etc/ld.so.conf"

# is the line present?
if fgrep -qsx "$dir" "$ldsoconf"; then
  # are there any shared objects in the directory?
  if [ "$(echo "$dir"/lib*.so*)" = "$dir/lib*.so*" ]; then
    # glob expansion found nothing, so no shared libraries are present
    echo "removing $dir directory from $ldsoconf"
    # rewrite the file (very carefully)
    set +e
    fgrep -svx "$dir" "$ldsoconf" > "$ldsoconf.dpkg-tmp"
    fgrep_status=$?
    set -e
    case $fgrep_status in
      0|1) ;; # we don't actually care if any lines matched or not
      *) die "error reading \"$ldsoconf\"; fgrep exited with status" \
        "$fgrep_status" ;;
    esac
    set +e
    cmp -s "$ldsoconf.dpkg-tmp" "$ldsoconf"
    cmp_status=$?
    set -e
    case $cmp_status in
      0) rm "$ldsoconf.dpkg-tmp" ;; # files are identical
      1) mv "$ldsoconf.dpkg-tmp" "$ldsoconf" ;; # files differ
      *) die "error comparing \"$ldsoconf.dpkg-tmp\" to \"$ldsoconf\"; cmp" \
        "exited with status $cmp_status" ;;
    esac
    ldconfig
  fi
fi