#!/bin/sh

CONF="$1"

if [ ! "$CONF" ] || [ ! -e "$CONF" ] ; then
    cat 1>&2 <<EOF
Usage: $0 <module list filename> [ --ignore-missing ] [ <arch> ] [ <build directory> ]

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.

The optional build directory indicates where executable programs may be found,
having been built. An initial build must have been performed before running this
program.
EOF
    exit 1
fi

shift 1

# Get flag.

if [ "$1" ] && [ "$1" = '--ignore-missing' ] ; then
    IGNORE_MISSING="$1"
    shift 1
else
    IGNORE_MISSING=
fi

# Get other options.

ARCHDIR=
BUILDDIR=

# Look for an architecture or build directory.

if [ "$1" ] && [ ! -e "$1" ] ; then
    ARCH="$1"
    shift 1
else
    ARCH=
fi

# Look for any additional build directory.

if [ "$1" ] && [ -e "$1" ] ; then
    BUILDDIR="$1"
fi

# Use a default build directory if none specified or found.

if [ ! "$BUILDDIR" ] ; then
    BUILDDIR=mybuild
fi

if [ ! -e "$BUILDDIR" ] ; then
    cat 1>&2 <<EOF
Not found (build directory): $BUILDDIR
EOF
    exit 1
fi

# Test for architecture-specific program directories.

for FILENAME in "$BUILDDIR/bin/"* ; do
    BASENAME=`basename "$FILENAME"`

    if [ "$FILENAME" = "$BUILDDIR/bin/*" ] ; then
        cat 1>&2 <<EOF
No architecture-specific directories in the build directory hierarchy:

$BUILDDIR/bin
EOF
        exit 1
    elif [ ! "$ARCH" ] || [ "${ARCH}_gen" = "$BASENAME" ] ; then
        ARCHDIR="$BASENAME"
        break
    fi
done

# Exit if no architecture resources available.

if [ ! "$ARCHDIR" ] ; then
    cat 1>&2 <<EOF
No architecture specified or no appropriate program directory found.
EOF
    exit 1
fi

# Obtain module details.

BIN="$BUILDDIR/bin/$ARCHDIR/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
    PROG=`basename "$MODULE" .txt`
    if [ "$PROG" != "$MODULE" ] ; then
        continue
    fi
    if [ ! -e "$BIN/$PROG" ] && [ ! "$IGNORE_MISSING" ] ; then
        echo "Not found: $PROG" 1>&2
        exit 1
    fi

    # NOTE: Architecture needs fixing.

    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"
