#!/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

# Determine the CFLAGS for the known architectures:
case $ARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv4 -mtune=xscale" ;;
   armel)   export SLKCFLAGS="-O2 -march=armv4t" ;;
   powerpc) export SLKCFLAGS="-O2" ;;
   sparc)   export SLKCFLAGS="-O2" ;;
   hppa)    export SLKCFLAGS="-O2" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Extract source:
tar xvvf $PORTCWD/sources/u-boot-$VERSION.tar.bz2
cd u-boot*
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, 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/
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

# 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
