#!/bin/sh -
# OCaml-specific "find-requires" for RPM.
# By Richard W.M. Jones <rjones@redhat.com>

#set -x

while read file; do
    # Get the list of modules exported by the file.
    modules=`ocamlobjinfo $file |
    grep 'Unit name: ' |
    awk '{print $3}'`

    # Turn list of modules into a regexp that matches the module names.
    modules_re=`echo $modules | sed 's/ /|/g'`

    # Get a list of the modules this file depends on.
    ocamlobjinfo $file |
    grep -Eo '[0-9a-f]{32}[[:space:]]+[A-Za-z0-9_]+' |
    grep -Ev $modules_re |
    while read md5sum module; do
	echo ocaml-$module-$md5sum
    done
done |
sort -u

# Every OCaml program depends on the precise version of the
# compiler which was used to compile it.
rpm -q --qf '%{NAME} = %{VERSION}-%{RELEASE}\n' ocaml