#!/bin/sh -e

# Prepare a build directory for the standard Tilera Linux.
#
# Please consult the System Programmer's Guide for more information
# on configuring and building Linux.
#
# This script provides the default Tilera config file (from
# arch/tile/configs/${ARCH}_defconfig) and sets up the default Tilera
# initramfs (from arch/tile/initramfs/contents_${BITS}.txt).
#
# If the "--strip-all" option is specified, it is passed to
# "arch/tile/initramfs/setup" instead of the usual "--strip-debug".
# This may be appropriate when building an mboot bootrom, or other
# kernel where saving space is very important.

# Verify the "install" directory.
if [ -n "$TILERA_ROOT" ]; then
    if [ ! -f "$TILERA_ROOT"/bin/tile-gcc ]; then
	echo "TILERA_ROOT must point to a per-chip hierarchy of an MDE install tree." >&2
	exit 1
    fi
    ARCH=`$TILERA_ROOT/bin/tile-gcc -dumpmachine | sed 's/-.*//'`
else
    ARCH=`uname -m`
    case "$ARCH" in
	tile*) ;;
	*) echo "TILERA_ROOT not set in environment." >&2; exit 1 ;;
    esac
fi

# Test for the one argument we support.
strip=--strip-debug
while [ "$#" -gt 0 ]; do
case "$1" in
--strip-all) strip=$1 ;;
*) echo "syntax: $0 [--strip-all]" >&2; exit 1 ;;
esac
shift
done

# Figure out which file versions we want to use.
case $ARCH in
tilegx) BITS=64 ;;
tile|tilepro) BITS=32 ;;
*) echo "$0: bad tile arch '$ARCH'" >&2; exit 1 ;;
esac

# Verify the "source" directory.
src=${0%/tile-prepare}
config=arch/tile/configs/${ARCH}_defconfig
product=Linux
if [ ! -f $src/$config ]; then
    echo "$0 is not in a $product source directory." >&2
    exit 1
fi

# Verify the "build" directory.
build=`pwd`
if [ -f $config ]; then
    echo "$0 must not be run in the $product source directory." >&2
    echo "Please create a new empty directory to build in, then run" >&2
    echo "  $build/tile-prepare" >&2
    echo "from within that directory." >&2
    exit 1
fi

if [ ! -f .config ]; then
    echo "Copying default $BITS-bit .config from $src"
    cat $src/$config >.config
else
    echo "Note: not overwriting pre-existing .config file."
fi

if [ ! -f usr/contents.txt ]; then
    echo "Copying default $BITS-bit usr/contents.txt from $src"
    mkdir -p usr
    cat $src/arch/tile/initramfs/contents_${BITS}.txt >usr/contents.txt
else
    echo "Note: not overwriting pre-existing usr/contents.txt file."
fi

echo "Running arch/tile/initramfs/setup"
$src/arch/tile/initramfs/setup $strip

echo "Running 'make ARCH=$ARCH oldconfig > make-oldconfig.out'"
make ARCH=$ARCH -C $src O=$build oldconfig > make-oldconfig.out < /dev/null

echo ""
echo "To build $product, just run 'make' in this directory."
