#!/bin/bash

# Raspberry Pi Imaging Utility software.
#
# Exaga - sarpi.penthux.net - 11 May 2023 [version 1.7.4]
#         updated           - 24 Dec 2023 [version 1.8.4]
#         updated           - 29 Feb 2024 [version 1.8.5]
#
# SARPi Project - https://sarpi.penthux.net
#
# Version: rpi-imager-1.8.5.tar.gz
# https://github.com/raspberrypi/rpi-imager/archive/refs/tags/v1.8.5.tar.gz
#
# This script creates a Raspberry Pi Imaging Utility  package for Slackware
# ARM/AArch64 Linux, using source from Raspberry Pi GitHub rpi-imager
# repository sources. 
#
# For usage see the README file included within the rpi-imager package. It
# is found in the /usr/doc/rpi-imager-VERSION directory after installation.
# Or view the README online at:
# https://github.com/raspberrypi/rpi-imager/blob/qml/README.md
#
######
#
# Building and installing from source:
# Unpack the source tarball and 'cd' to that directory. Then create a build
# directory and 'cd' to it. From there you can 'ccmake ../src' to view the 
# list of build parameters. If you aren't sure just run with the defaults.
#
# tar -zxvf rpi-imager-VERSION.tar.gz
# cd rpi-imager
# mkdir build && cd build
# ccmake ../src     # to view the config build params and edit
# cmake ../src      # to configure the source
# make
# make install
#
# Or simply run this rpi-imager.SlackBuild script and it will build a package
# automagically for you, which you can install on your Slackware Linux system.
#
# This script is set to build for Slackware AArch64 -current version. Change
# the TAG variable accordingly if you're building it for other architectures.
#
######
#
# Upstream-Name: rpi-imager
# Source: https://github.com/raspberrypi/rpi-imager
#
# Files: *
# Copyright: 2020, Raspberry Pi Ltd.
# License: Apache License, Version 2.0
#
# Copyright (c) 2020 Raspberry Pi Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# For further licence details and third-party component licence details
# see the license.txt file included within the resulting package. Or view
# online: https://github.com/raspberrypi/rpi-imager/blob/qml/license.txt
#
######

CWD="$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)"

# PKG vars
PRGNAM="rpi-imager"
VERSION=${VERSION:-1.8.5}
BUILD=${BUILD:-1}
TAG="${TAG:-_slackcurrent_sp1}"
PKGTYPE="${PKGTYPE:-txz}"

# PKG build dir vars
TMP=${TMP:-/tmp/SBo}
PKG=${TMP}/package-${PRGNAM}
PKGTMP=${TMP}/"${PRGNAM}"-"${VERSION}"
OUTPUT=${OUTPUT:-/tmp}

# Source archive and docs location 
RPi_IMAGER_GITREPO_URL="https://github.com/raspberrypi/rpi-imager"
RPi_IMAGER_GITREPO_BRANCH="qml"
RPi_IMAGER_GITREPO_PKG="v${VERSION}.tar.gz"
RPi_IMAGER_GITRAW_DOCS="https://raw.githubusercontent.com/raspberrypi/${PRGNAM}/${RPi_IMAGER_GITREPO_BRANCH}"

# Determine ARCH
if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i486 ;;
	aarch64) ARCH=aarch64 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

# PRINT_PACKAGE_NAME when ARCH not set in env
if [ -n "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

# Set CFLAGS based on ARCH
if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
elif [ "$ARCH" = "aarch64" ]; then
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

# Set builtin
set -e

# Start clean
rm -rf "$PKG" "${PKGTMP}"
mkdir -p "$TMP" "$PKG" "$PKGTMP"/build "$OUTPUT"

# Download rpi-imager GitHub repository files
curl -L "${RPi_IMAGER_GITREPO_URL}"/archive/refs/tags/"${RPi_IMAGER_GITREPO_PKG}" -o "${CWD}"/"${PRGNAM}-${VERSION}.tar.gz"
curl -L "${RPi_IMAGER_GITRAW_DOCS}"/README.md -o "${CWD}"/README
curl -L "${RPi_IMAGER_GITRAW_DOCS}"/license.txt -o "${CWD}"/license.txt

# Unpack rpi-imager tarball
cd "${TMP}"
tar -zxvf "${CWD}"/"${PRGNAM}-${VERSION}.tar.gz"

# Configure params
cd "${PKGTMP}"
chown -R root:root .

# Configure and build the source
cd "${PKGTMP}"/build
cmake ../src
make -j${nproc}
make install DESTDIR="${PKG}"

# Copy docs and SlackBuild script to PKG docs dir
mkdir -p "${PKG}"/usr/doc/$PRGNAM-"$VERSION"
cp -avr "${CWD}"/license.txt "${CWD}"/README "${PKG}"/usr/doc/$PRGNAM-"$VERSION"
cat "${CWD}"/$PRGNAM.SlackBuild > "${PKG}"/usr/doc/$PRGNAM-"$VERSION"/$PRGNAM.SlackBuild

# Create PKG install dir and populate
mkdir -p "${PKG}"/install
cp -avr "${CWD}"/doinst.sh "${PKG}"/install/doinst.sh
cat "${CWD}"/slack-desc > "${PKG}"/install/slack-desc

# Update slack-desc with PRGNAM version info
sed -Ei "s/@VERSION@/${VERSION}/g" "${PKG}"/install/slack-desc

# Create PKG
cd "${PKG}"
/sbin/makepkg -l y -c n "$OUTPUT"/$PRGNAM-"$VERSION"-"${ARCH}"-"$BUILD""$TAG"."${PKGTYPE:-txz}"
cd "${OUTPUT}"
/bin/md5sum $PRGNAM-"$VERSION"-"${ARCH}"-"$BUILD""$TAG"."${PKGTYPE:-txz}" > "$OUTPUT"/$PRGNAM-"$VERSION"-"${ARCH}"-"$BUILD""$TAG".md5

# Done
exit 0;

#EOF<*>

