#!/system/bin/sh

# wrapper script for the ldd from Android
#
# History
#  18.12.2024 1.0.0 /bs
#   initial version
#
#  24.01.2025 2.0.0 /bs
#    script rewritten
#
#  02.08.2025 2.1.0 /bs
#    added the directory /system/usr/lib to he LD_LIBRARY_PATH if it exists
#
#  01.10.2026 2.2.0 /bs
#    myldd now uses readelf to list the libraries required or a binary if ldd failed
#
# Supported environment variables:
#  
#   READELF - readelf binary to use; use "none" to disable the use of readelf
#             default is to search readelf via PATH variable
#
if [ $# -eq 0 -o  "$1"x = "-h"x  -o "$1"x = "--help"x ] ; then
  echo "Usage $0 [binary] [...]"
  exit 1
fi

READELF="${READELF:=$( which readelf )}"
	
while [ $# -ne 0 ] ;  do
  echo ""	
  CUR_FILE="$1"
  shift
  if [[ "${CUR_FILE}" = /* ]] ; then
     :
  elif [ -r "${PWD}/${CUR_FILE}" ] ; then
    CUR_FILE="${PWD}/${CUR_FILE}"
  elif [[ ${CUR_FILE} != */* ]] ; then
	  if [[ "$( which ${CUR_FILE} )"  != "" ]] ; then
      CUR_FILE="$( which ${CUR_FILE} )"
    fi
  fi
  
  for CUR_DIR in /data/local/tmp/sysroot/usr/lib /data/local/tmp/develop/sysroot/usr/lib /system/usr/lib ; do
    if [ -d "${CUR_DIR}" ] ; then
      if [[ ":${LD_LIBRARY_PATH}:" != *:${CUR_DIR}:* ]] ; then
        LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${CUR_DIR}
      fi
    fi
  done
 
  [  !  -z ${VERBOSE} ] && echo "The LD_LIBRARY_PATH used for this command is ${LD_LIBRARY_PATH}"
  echo "# Executing ldd \"${CUR_FILE}\" "
  ldd "${CUR_FILE}"
  if [ $? -ne 0 -o "${VERBOSE}"x != ""x  ] ; then
     if [ "${READELF}"x != ""x -a "${READELF}"x != "none"x ] ; then
       echo 
       echo "Libraries required by \"${CUR_FILE}\" are:"
       ${READELF} -d "${CUR_FILE}"  | grep NEEDED | awk '{ print $NF  }' | tr -d "[]"  | sed "s/^/  /g"
     fi
   fi
done

