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

# hwm-bw-raspberrypi.SlackBuild
# by Stuart Winter <mozes@slackware.com>
#
# Copyright 2021, 2022  Stuart Winter, Donostia, Spain.
# 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.

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

# Paths to skeleton port's source & real Slackware source tree:
slackset_var_cwds

# Extract package version from the firmware archive:
# This is the tag from https://github.com/raspberrypi/firmware/tree/master/boot
cd sources
VERSION=$( ls -1 rpi-boot-fw-*.tar.xz | cut -d- -f4- | sed 's?.tar.*??g' )
cd -

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

# Assemble package:
mkdir -vpm755 $PKG/usr/share/$PKGNAM
# Install the RPi firmware:
tar xvvf $PORTCWD/sources/rpi-boot-fw-$VERSION.tar.!(*sign|*asc|*sig) \
    -C $PKG/usr/share/$PKGNAM/ || failinstall

# Raspberry Pi4 U-Boot Boot Loader:
# Install the U-Boot 2nd stage Boot Loader:
# This stored within the /platform directory (outside of the Slackware trees):
# If we ever want to upgrade the version of U-Boot within the Hardware Model
# Platform Bootware partition (/boot/platform/hwm_bw mount point), there is
# provision for this within the Kernel package's post installation script.
# This is set up to install the appropriate U-Boot binary into the standard
# file name, 'u-boot.bin'.
# For now, we don't do that as I cannot see the need.
install -vpm644 \
   $SLACKPLATFORMDIR/bootware/bin/bcm2711/u-boot-*/bcm2711_rpi4-u-boot.bin \
   $PKG/usr/share/$PKGNAM/ || failinstall
#   $PKG/usr/share/$PKGNAM/u-boot.bin || failinstall

# Redo the package name since the version has been updated:
export SLACKPACKAGE=$PKGNAM-$VERSION-$PKGARCH-$BUILD.txz

# Apply generic Slackware packaging policies:
cd $PKG
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
