#!/bin/sh

CONF="$1"

if [ ! "$CONF" ] || [ ! -e "$CONF" ] ; then
    cat 1>&2 <<EOF
Usage: $0 <configuration filename>

List modules required by the given system/payload configuration. These modules
should be added to the configuration file so that the dynamic linker can find
them in the running system.
EOF
    exit 1
fi

BIN=mybuild/bin/mips_32/l4f
TMPLIBS=_libs.txt

echo -n > "$TMPLIBS"

for MODULE in `grep '^module ' "$CONF" | sed 's/module //'` ; do
    PROG=`basename "$MODULE" .cfg`
    if [ "$PROG" != "$MODULE" ] ; then
        continue
    fi
    PROG=`basename "$MODULE" .io`
    if [ "$PROG" != "$MODULE" ] ; then
        continue
    fi
    PROG=`basename "$MODULE" .so`
    if [ "$PROG" != "$MODULE" ] ; then
        continue
    fi
    if [ ! -e "$BIN/$PROG" ] ; then
        echo "Not found: $PROG" 1>&2
        exit 1
    fi
    mipsel-linux-gnu-readelf -d "$BIN/$PROG" | grep NEEDED | sed 's/.*\[//;s/\]$//' >> "$TMPLIBS"
done

# Explicitly add undetected module.

echo "libl4sys-direct.so" >> "$TMPLIBS"

sort -u "$TMPLIBS" | sed 's/^/module /'
rm "$TMPLIBS"
