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

# hwm-bw-lx2160acex7.SlackBuild
# by Stuart Winter <mozes@slackware.com>
#
# Copyright 2023  Stuart Winter, Earth, Milky Way, "".
# 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

# Parse any command line operators:
PARAMS="$( getopt -qn "$( basename $0 )" -o P: -l pkgstoreoverride: -- "$@" )"
eval set -- "${PARAMS}"
for param in $*; do
  case "$param" in
     -P|--pkgstoreoverride) PKGSTORE_OVERRIDE="$2"
                    shift 2;;
     --) shift; break;;
  esac
done

# If the user has specified -P/--pkgstoreoverride, they want to store the resulting
# package files in a directory other than $PKGSTORE.
# We will ensure that the destination directory exists first.
[ ! -z "${PKGSTORE_OVERRIDE}" -a ! -d "${PKGSTORE_OVERRIDE}" ] && {
   echo "ERROR: Alternate package destination directory '${PKGSTORE_OVERRIDE}' does not exist"
   exit 1 ;} || { echo "Packages will be stored in: ${PKGSTORE_OVERRIDE}" ;}

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

# Assemble the package:
mkdir -vpm755 $PKG/usr/{sbin,share/$PKGNAM/bootloader-firmware}
mkdir -vpm755 $PKG/var/lib/pkgtools/setup
# Install the Boot Loader firmware:
install -vpm644 \
   $PORTCWD/sources/firmware_spi/* \
   $PKG/usr/share/$PKGNAM/bootloader-firmware/ || failinstall
# Install the flashing tool:
install -vpm755 $PORTCWD/sources/bootloader-flash-lx2160acex7 $PKG/usr/sbin/ || failinstall
# Setup the symlink so that it's called from within the post-install phase of the Slackware Installer:
# This one's numbered 11, where as the rk3399 is 12.
# We want this to run earlier for development reasons (since this
# was initially tested on the RockPro64).
ln -vrfs $PKG/usr/sbin/bootloader-flash-lx2160acex7 $PKG/var/lib/pkgtools/setup/setup.11.bootloader-flash-lx2160acex7

# If $PKGSTORE_OVERRIDE is set and exists as a directory, we'll switch
# $PKGSTORE to it so that our packages are stored outside of the main tree.
# This enables test builds of the package to be created more easily.
[ -d "${PKGSTORE_OVERRIDE}" ] && PKGSTORE="${PKGSTORE_OVERRIDE}"

# 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
