#!/bin/bash
ulimit -s unlimited
shopt -s extglob

# u-boot-tools.SlackBuild
# Build the tools for Das U-Boot
# by Stuart Winter
#
# Copyright 2018-2025  Stuart Winter, Surrey, England, UK.
# 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.

# 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:
slackset_var_cwds

# Temporary build locations:
#shm_tmp # Use /dev/shm if >8GB RAM is available & not mounted 'noexec'
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Extract source:
tar xf $PORTCWD/sources/u-boot-tools*.tar.!(*sign|*asc|*sig)
cd u?boot*/ || failextract
# Add some Debian source to obtain some default env configs and extra man pages:
tar xf $PORTCWD/sources/u-boot*debian* || exit 1
slackhousekeeping

# 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 need a configuration file only so that we
# can build the tools:
make distclean
#make guruplug_config || exit 1
make tools-only_defconfig || failconfig

# Build:
echo "** Building tools **"
make $NUMJOBS tools || make tools || failmake
echo "** Building Env tools **"
make $NUMJOBS env   || make env   || failmake
make $NUMJOBS envtools || make envtools || failmake
make -C tools gen_eth_addr || failmake

# Install into package:
mkdir -vpm755 $PKG/usr/doc/$PKGNAM-$VERSION/
mkdir -vpm755 $PKG/usr/man/man1
#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 \
           dumpimage \
           fdt_add_pubkey \
           fdtgrep \
           fit_{info,check_sign} \
           ifwitool \
           mkeficapsule \
           proftool \
           img2srec \
           mkimage \
           mkenvimage \
           $PKG/usr/bin
  cd env
  install -vpm755 fw_printenv $PKG/usr/sbin/ || failinstall
  ( cd $PKG/usr/sbin && ln -vfs fw_printenv fw_setenv )
    install -vpm644 README $PKG/usr/doc/$PKGNAM-$VERSION/README.envtools
) || failinstall

#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/
#install -vpm644 debian/manpages/*.8 $PKG/usr/man/man8/
install -vpm644 doc/mkimage.1 $PKG/usr/man/man1/
install -vpm644 doc/dumpimage.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/

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackstriprpaths     # strip rpaths
slack_delete_lafiles # delete usr/lib{,64}/*.la
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

# Replace version number with a _ so it doesn't get confused with
# the package name.
export VERSION="$( echo $VERSION | sed 's?-?_?g' )"
export SLACKPACKAGE=$PKGNAM-$VERSION-$ARCH-$BUILD.txz
slackmp         # run makepkg -l y -c n

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