#!/bin/bash
##
## RPM: elf-provides for OSSTech vendor specific libraries
## Copyright (c) 2022 SATOH Fumiyasu @ OSSTech Corp., Japan
##               <https://www.OSSTech.co.jp/>
##
## License: GNU General Public License version 3 or later
##

set -u
set -o pipefail || exit $?		## bash 3.0+

vendor_id="${VENDOR_RPM_VENDOR_ID:-osstech}"

vendor_lib_dir="$1"; shift
dist_name="${vendor_lib_dir##*/}"

if [[ -z ${VENDOR_RPM_VENDOR_DEPENDENCIES-} ]]; then
  ## Discard the file list, but provide the commands and shared libraries
  ## file list for DNF 4.19, which does not download file list metadata
  ## from the repository by default.
  if [[ -n $RPM_BUILD_ROOT ]]; then
     root_re="^$(sed 's|[^-_/A-Za-z0-9]|\\&|g' <<<"$RPM_BUILD_ROOT")/"
   else
     root_re=""
  fi
  sed -E -n \
    ${root_re+-e "s|$root_re|/|"} \
    -e '/\/debug\//d' \
    -e '/\/bin\/[^\/]+$/p' \
    -e 's|^(.*/lib[^/]*\.so\.[0-9]+)\.[^/]*$|\1|p' \
  ;
  exit $?
fi

"$@" \
|sed \
  -e 's/^lib.*\.so[^(]*(/&'"${vendor_id//\//\\\/}-${dist_name//\//\\\/}"'\//' \
;
