#!/bin/sh

# Copyright 2008, 2009, 2010, 2011  Patrick J. Volkerding, Sebeka, MN, USA
# Adapted by Philip Langdale
#
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


PKGNAM=u-boot-tools
VERSION=${VERSION:-$(echo u-boot-*.tar.?z* | rev | cut -f 3- -d . | cut -f 1 -d - | rev)}
BUILD=${BUILD:-1}

# Automatically determine the architecture we're building on:
if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) export ARCH=i486 ;;
    arm*) export ARCH=arm ;;
    # Unless $ARCH is already set, use uname -m for all other archs:
       *) export ARCH=$( uname -m ) ;;
  esac
fi

CWD=$(pwd)
TMP=${TMP:-/tmp}
PKG=$TMP/package-$PKGNAM

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
else
  SLKCFLAGS="-O2"
fi

# If the variable PRINT_PACKAGE_NAME is set, then this script will report what
# the name of the created package would be, and then exit. This information
# could be useful to other scripts.
if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PKGNAM-$VERSION-$ARCH-$BUILD.txz"
  exit 0
fi

rm -rf $PKG
mkdir -p $TMP $PKG
cd $TMP
rm -rf u-boot*

tar xvf $CWD/u-boot-$VERSION.tar.?z*

cd u-boot* || exit 1

# Add some Debian source to obtain some default env configs and extra man pages:
tar xvf $CWD/u-boot*debian* || exit 1

chown -R root:root .
find . \
  \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \) \
  -exec chmod 755 {} \; -o \
  \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
  -exec chmod 644 {} \;

# Configure:
# We aren't building u-boot for a specific device (each device requires a specific version
# of u-boot with support for that device), we just need to configure it to be some sort of 'ARM' CPU
# so that we can build the u-boot tools.
make distclean
#make guruplug_config || exit 1
make tools-only_defconfig || exit 1

# Build:
echo "** Building tools **"
make $NUMJOBS tools || make tools || exit 1
echo "** Building Env tools **"
make $NUMJOBS env   || make env   || exit 1
make $NUMJOBS envtools || make envtools || exit 1

# Install into package:
mkdir -vpm755 $PKG/usr/doc/$PKGNAM-$VERSION/
mkdir -vpm755 $PKG/usr/man/man{1,8}
mkdir -vpm755 $PKG/etc
mkdir -vpm755 $PKG/usr/{bin,sbin}
( cd tools
  install -vpm755 \
           gen_eth_addr \
           img2srec \
           mkimage \
           $PKG/usr/bin
  cd env
  install -vpm755 fw_printenv $PKG/usr/sbin/
  ( cd $PKG/usr/sbin && ln -vfs fw_printenv fw_setenv )
  install -vpm644 README $PKG/usr/doc/$PKGNAM-$VERSION/README.envtools
)

#install -vpm644 fw_env.config $PKG/etc/fw_env.config.new
# I added the offsets for the {Guru,Sheeva}Plug into this copy;
# the package install script will take care of renaming it into place
# but you'll still have to manually uncomment the one line you need for
# the specific board you're using.  This could be done automatically by looking
# in /proc/cpuinfo -- if anyone wants to do this nicely (that does not involve
# including the data in the install script (instead all of the info is included
# in the sample file and sed is used to uncomment the correct one)) - let me know.
install -vpm644 $CWD/fw_env.config.new $PKG/etc/
install -vpm644 debian/manpages/*.8 $PKG/usr/man/man8/
install -vpm644 doc/mkimage.1 $PKG/usr/man/man1/
cp -fa debian/env-configs $PKG/usr/doc/$PKGNAM-$VERSION/

# Copy docs:
install -vpm644 \
        CHANGELOG COPYING CREDITS MAINTAINERS README \
        $PKG/usr/doc/$PKGNAM-$VERSION/

# Strip binaries:
( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

# Compress and link manpages, if any:
if [ -d $PKG/usr/man ]; then
  ( cd $PKG/usr/man
    for manpagedir in $(find . -type d -name "man*") ; do
      ( cd $manpagedir
        for eachpage in $( find . -type l -maxdepth 1 | grep -v '\.gz$') ; do
          ln -s $( readlink $eachpage ).gz $eachpage.gz
          rm $eachpage
        done
        gzip -9 *.?
      )
    done
  )
fi

# Copy docs:
mkdir -vpm755 $PKG/usr/doc/$PKGNAM-$VERSION
cp -a COPYING \
    $PKG/usr/doc/$PKGNAM-$VERSION/

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

# Build the package:
cd $PKG
/sbin/makepkg -l y -c n $TMP/$PKGNAM-$VERSION-$ARCH-$BUILD.txz