#!/bin/sh

THISDIR=`dirname "$0"`
DIRNAME=`readlink -f "$THISDIR/.."`
PROGNAME=`basename "$0"`

if [ "$1" = '--help' ] || [ ! "$1" ] ; then
    cat 1>&2 <<EOF
Usage: $PROGNAME <platform> | <l4 directory>

Update platform-specific files for the indicated platform type. If a directory
is specified instead, try and obtain a configured platform type and use that
to update the files.

If a platform type is explicitly indicated, this program will update files in
the distribution. They must then be installed using the install program:

$THISDIR/install.sh

If a directory is specified instead, this program will update installed files,
leaving the distribution alone.
EOF
    exit 1
fi

# Check for a build directory and obtain a platform type.

if [ -e "$1" ] ; then

    # Look for the configuration in a subdirectory.

    for FILENAME in "$1/"*"/.config.platform" ; do

        # Test for a valid file since the expansion will yield the pattern
        # itself if no files match.

        if [ -e "$FILENAME" ] ; then

            # Set the platform and the installation as target.

            PLATFORM=`grep -e 'CONFIG_PLATFORM_TYPE=' "$FILENAME" | sed 's/^.*"\(.*\)".*$/\1/'`
            TARGETDIR="$1"
            break
        else
            cat 1>&2 <<EOF
The specified directory does not contain an existing, configured build
directory. Make sure that the configuration has been checked with the
appropriate tool specifying a platform as argument:

$DIRNAME/$PROGNAME <platform>
EOF
            exit 1
        fi
    done

# Check for a recognised platform type.

elif grep -q -F -e "$1" "$DIRNAME/conf/landfall-examples/platforms.txt" ; then

    # Set the platform and the distribution as target.

    PLATFORM="$1"
    TARGETDIR="$DIRNAME"

# Show an error for anything else.

else
    cat 1>&2 <<EOF
Platform not recognised: $1
EOF
    exit 1
fi

# Panel configuration file in the target area.

PANELCONF="$TARGETDIR/conf/landfall-examples/mips-jz4740-panel.txt"

# Determine the required value of the panel configuration.

PANELLIB="rom/libpanel_$PLATFORM.so"

# Write it to the configuration file.

echo -n "$PANELLIB" > "$PANELCONF"
