#!/bin/sh
#
# bininfo - display data about a binary and its dependencies

usage() {
  cat <<EOF
Usage:
  bininfo <path>

Options:
  -h, --help  - Show this help text

Positionals:
  <path>      - Path to binary
EOF
}

if [ "$#" -eq 0 ]; then
  usage
  exit 1
fi

if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
  usage
  exit 0
fi

case "$(uname)" in
  Darwin)
    if [ -f "$1" ]; then
      file -L -i -- "$1"
      otool -L "$1" 2>/dev/null
    else
      printf '%s\n' "Sorry, couldn't find existing readable file."
      exit 1
    fi
    ;;
  *)
    if [ -f "$1" ]; then
      file --brief --dereference "$1"
      ldd "$1"
    else
      printf '%s\n' "Sorry, couldn't find existing readable file."
      exit 1
    fi
    ;;
esac
