#! /bin/sh
# install-arm-tools (C) Axis Communications 2005.
#
# Simple dispatcher script calling a pre-configured
# pre-downloaded "buildroot" tree and cleaning up afterwards.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#

ver=1.20050730.1
packname="ARM GCC compiler tools ver $ver"

fail()
{
  test -n "$1" && echo "$0: Sorry:" $*.
  exit 1
}

if test $# != 1
then
 cat << 'EOF'
 Usage:
  install-arm-tools <installroot>

 Example:
  install-arm-tools /usr/local/arm

 See also the file README.
EOF
 fail "wrong usage.  See above"
fi

installroot=$1

# Bail out if , in path (unusual, and not compatilble with our sed script below)
echo $installroot | grep ',' >/dev/null \
 && fail "no commas in the installation path"

# Make installroot absolute if it isn't already
echo $installroot | grep '^/' > /dev/null \
 || installroot=`pwd`/$installroot

# (or use this instead if we don't want to allow relative install paths)
# || fail "must specify an absolute install path"

# go to location of this script, then extract absolute path to working dir
cd `dirname $0`
packdir=`pwd`

# Note: from about buildroot 20051001, .tmpconfig.h no longer used

if sed -e "s,\(BR2_STAGING_DIR\).*$,\1=\"$installroot\",g" \
    < $packdir/buildroot.config > $packdir/buildroot/.config \
 && sed -e "s,\(BR2_STAGING_DIR\).*$,\1 \"$installroot\",g" \
    < $packdir/buildroot.tmpconfig.h > $packdir/buildroot/.tmpconfig.h
then :
else
 fail "setting installation-root-directory failed"
fi

# Remove any potential previously applied patches if rebuilding
# (the -name argument must be synced with the contents of the files in patches/)

find $packdir/buildroot -name '090-[aA]xis*' -exec rm {} ';'

if (cat $packdir/patches/*.patch | patch -p1 -d $packdir/buildroot)
then :
else fail "failed applying patches"
fi

cat <<EOF
------------------------------------------------------------------------------
*** Please wait while compiling and installing $packname.
------------------------------------------------------------------------------
EOF
sleep 1

PATH=$packdir/fakebin:$PATH
echo "\$PATH is now: " $PATH
export PATH

# We assume we can work around any interactive part by piping
# <NL>, which should sufficiently emulate hitting return.
if { yes '
' | (make -C $packdir/buildroot oldconfig && make -C $packdir/buildroot && touch succeeded) \
  2>&1 | tee buildlog; } && test -f succeeded && rm succeeded
then
 cat <<EOF
------------------------------------------------------------------------------
*** Build succeeded: $packname installed at $installroot.
*** Wait a moment while I remove temporary directories...
------------------------------------------------------------------------------
EOF
 rm buildlog
 else
cat <<EOF
------------------------------------------------------------------------------
*** Build of $packname failed.
------------------------------------------------------------------------------
EOF
 fail ""
fi
# Remove temporary directories
rm -rf $packdir/buildroot/toolchain_build_arm $packdir/buildroot/build_arm
#find buildroot -name '090-[aA]xis*' -exec rm {} ';' -print
