#!/bin/bash

# u-boot-tools.SlackBuild
# Build the tools for Das U-Boot
# by Stuart Winter
#

# Useful URLs:
# http://elinux.org/U-boot_environment_variables_in_linux
# http://www.openplug.org/plugwiki/index.php/Das_U-boot_plug_support

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PKGNAM
export PORTCWD=$PWD

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Versions:
SHEEVATOOLSVER=09.04.2010

# Determine the CFLAGS for the known architectures:
case $ARCH in
   arm)   export SLKCFLAGS="-O2 -march=armv4t" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Extract source:
tar xvvf $PORTCWD/sources/u-boot-$VERSION.tar.?z*
cd u-boot* || exit 1
slackhousekeeping

# Fix up:
touch include/config.mk
sed -i 's:linux/string.h:string.h:' lib_generic/sha1.c 
sed -i 's:linux/mtd/mtd.h:mtd/mtd-user.h:' tools/env/fw_env.c 
sed -i \
    -e '1i#include <string.h>' \
    -e '1i#include <stdlib.h>' \
    tools/easylogo/easylogo.c

# Configure:
# An older ARM board - 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 lart_config || failconfig

# Build:
make tools || failmake
# We take this from the Debian package insetad.
#make env   || failmake

# Install into package:
mkdir -vpm755 $PKG/usr/{bin,sbin}
cd tools
install -vpm755 \
         bmp_logo \
         gen_eth_addr \
         img2srec \
         mkimage \
         easylogo/easylogo \
         $PKG/usr/bin

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

# Build env:
cd $TMPBUILD
tar xvvf $PORTCWD/sources/uboot-envtools*.orig.tar.gz
cd uboot-envtools*
zcat $PORTCWD/sources/uboot-envtools*diff*.gz | patch -p1 || failpatch
patch -p1 --verbose < debian/patches/makefile.patch || failpatch
patch -p1 --verbose < debian/patches/crc32-function-signature.patch || failpatch
make || failmake

# Install into package:
install -vpm755 fw_printenv $PKG/usr/sbin/ 
( cd $PKG/usr/sbin && ln -vfs fw_printenv fw_setenv )
mkdir -vpm755 $PKG/etc
#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 $PORTCWD/fw_env.config.new $PKG/etc/
mkdir -vpm755 $PKG/usr/man/man8
install -vpm644 debian/*.8 $PKG/usr/man/man8/
install -vpm644 README $PKG/usr/doc/$PKGNAM-$VERSION/README.env
cp -fa debian/uboot-envtools-examples $PKG/usr/doc/$PKGNAM-$VERSION/uboot-env-examples

# Build SheevaPlug u-boot tools.
cd $TMPBUILD
tar xvvf $PORTCWD/sources/sheeva-uboot-tools*tar*
cd sheeva-uboot-tools || exit 1

# Build:
make $NUMJOBS || exit 1

# Install:
make install PREFIX=$PKG/usr || exit 1
# Replace hard links with symlinks.
( cd $PKG/usr/sbin
  rm -f sheeva-nandrs-encode sheeva-ubootenv-encode
  ln -vfs sheeva-nandrs-decode sheeva-nandrs-encode 
  ln -vfs sheeva-ubootenv-decode sheeva-ubootenv-encode )
mkdir -vpm755 $PKG/usr/doc/sheeva-uboot-tools-$SHEEVATOOLSVER
install -vpm644 \
   README COPYING \
   $PKG/usr/doc/sheeva-uboot-tools-$SHEEVATOOLSVER

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh
slackmp         # run makepkg -l y -c n

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
