#!/bin/bash
set -o pipefail
help() {
  echo >&2 "Usage: $0 path/to/libc.so"
  echo >&2 "    OR $0 bid=<BUILD_ID>"
  echo >&2 "    OR $0 md5=<MD5>"
  echo >&2 "    OR $0 sha1=<SHA1>"
  echo >&2 "    OR $0 sha256=<SHA256>"
  exit 2
}

if [[ $# != 1 ]]; then
  help
fi

arg="$1"

if [ -f "$arg" ]; then
  libc="$(readlink -f "$arg")"
fi

cd "$(dirname "$0")"

if [[ -f "$libc" ]]; then
  arg="sha1=$(sha1sum "$libc" | awk '{print $1}')"
fi

case "$arg" in
  bid=*)
    hash="${arg#"bid="}"
    tool="file"
    regex="=$hash,"
    ;;
  md5=*)
    hash="${arg#"md5="}"
    tool="md5sum"
    regex="$hash "
    ;;
  sha1=*)
    hash="${arg#"sha1="}"
    tool="sha1sum"
    regex="$hash "
    ;;
  sha256=*)
    hash="${arg#"sha256="}"
    tool="sha256sum"
    regex="$hash "
    ;;
  *)
    help
esac

ls -1 db/*.so | xargs $tool | grep -- "$regex" | perl -n -e '/([^\/: ]+)\.so/&&print "$1\n"'
