#!/bin/bash

# Raspberry Pi EEPROM configuration utility software for the Raspberry Pi 4
# and Raspberry Pi 5.
#
# Exaga - sarpi.penthux.net - 17 Jan 2021 [version 11.5]
#         updated           - 15 Sep 2022 [version 13.6 - for AArch64]
#         updated           - 09 May 2023 [version 16.0-1]
#         updated           - 22 Dec 2023 [version 21.0-1 for Raspberry Pi 5]
#         updated           - 29 Feb 2024 [version 21.4-1]
#
# SARPi Project - https://sarpi.penthux.net
#
# Version: rpi-eeprom_11.5.orig.tar.gz
#
# This script creates a rpi-eeprom configuration utility software package for
# Slackware ARM/AArch64 Linux, using an existing official Debian package from 
# Raspberry Pi Ltd. sources. No source code is (de)compiled or (re)built. No 
# content of any files are modified, only re-packaged for Slackware purposes. 
#
# NB: This software may only be used for developing, running, or using, on 
# a Raspberry Pi device, and strictly for no other purpose(s).
#
# BUILD NOTE:
# The rpi-eeprom source package filename needs changing because the version 
# contains a hyphen and slack-desc won't display anything when the package is
# installed. That's all taken care of when this script is executed.
#
###
#
# Upstream-Name: rpi-eeprom
# Source: https://github.com/raspberrypi/rpi-eeprom
#
# Files: *
# Copyright: 2019, Raspberry Pi (Trading) Ltd.
# License: BSD-3
#
# Details on using the rpi-eeprom configuration utility software can be found 
# by consulting the man pages after installation, and/or in the Raspberry Pi 
# Documentation online at:
# https://www.raspberrypi.com/documentation/computers/raspberry-pi.html#raspberry-pi-boot-eeprom
#
# Further information and links to rpi-eeprom bootloader documentation, 
# user support, software release versions and changelog details, can be 
# found in the README file, located in the /usr/doc/rpi-eeprom-VERSION 
# directory after installation. 
#
# Full details on copyrights and licenses can be found in the LICENCE file, 
# located in the /usr/doc/rpi-eeprom-VERSION directory after installation.
#
###

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

# PKG vars
PRGNAM="rpi-eeprom"
VERSION=${VERSION:-27.4-1}
BUILD=${BUILD:-1}
TAG="${TAG:-_sp1}"
PKGTYPE="${PKGTYPE:-txz}"

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

# Source PKG [Debian] file and docs location 
RPi_EEPROM_GITREPO_URL="https://archive.raspberrypi.org/debian/pool/main/r/${PRGNAM}"
RPi_EEPROM_GITREPO_BRANCH="master"
RPi_EEPROM_GITREPO_PKG="${PRGNAM}_${VERSION}_all.deb"
RPi_EEPROM_GITRAW_DOCS="https://raw.githubusercontent.com/raspberrypi/${PRGNAM}/${RPi_EEPROM_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" "$OUTPUT"

# Download the rpi-eeprom [Debian] PKG file and documents
curl -L "${RPi_EEPROM_GITREPO_URL}"/"${RPi_EEPROM_GITREPO_PKG}" -o "${CWD}"/"${RPi_EEPROM_GITREPO_PKG}"
curl -L "${RPi_EEPROM_GITRAW_DOCS}"/LICENSE -o "${CWD}"/LICENSE
curl -L "${RPi_EEPROM_GITRAW_DOCS}"/README.md -o "${CWD}"/README
curl -L "${RPi_EEPROM_GITRAW_DOCS}"/releases.md -o "${CWD}"/releases

# Rename source VERSION for slack-desc to work correctly 
VERZION="$(printf "${VERSION}" | tr  "-"  ".")"
rename "${PRGNAM}_${VERSION}_all.deb" "${PRGNAM}_${VERZION}_all.deb" "${PRGNAM}_${VERSION}_all.deb"

# Re-VERSION PKG due to new filename
VERSION="${VERZION}"
RPi_EEPROM_GITREPO_PKG="${PRGNAM}_${VERSION}_all.deb"

# Unpack files from rpi-eeprom [Debian] PKG
cd "${PKGTMP}"
ar x "${CWD}"/"${RPi_EEPROM_GITREPO_PKG}"
tar -xvJf data.tar.xz
rm -rf debian-binary ./*.tar.xz ./*.deb lib/systemd

# Configure params
chown -R root:root .

# Copy everything from PKGTMP to PKG directory
rsync -Pavvl  "${PKGTMP}"/* "${PKG}"/
mv "${PKG}"/usr/share/doc/$PRGNAM "${PKG}"/usr/share/doc/$PRGNAM-"$VERSION"

#Perform housekeeping
cd "${PKG}"
rm -f "${PKG}"/usr/lib${LIBDIRSUFFIX}/*.la
rm -f "${PKG}"/usr/lib${LIBDIRSUFFIX}/*.a

find "${PKG}" -print0 | xargs -0 file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

#  Tarball any man pages
if [ -d "${PKG}"/usr/share/man ]; then
  find "${PKG}"/usr/share/man -type f -exec gzip -9 {} \;
  for i in $( find "${PKG}"/usr/man -type l ) ; do ln -s "$( readlink "$i" )".gz "$i".gz ; rm "$i" ; done
fi

# Copy docs dir and build script
#mkdir -p "${PKG}"/usr/doc/$PRGNAM-"$VERSION"
cp -avr "${CWD}"/LICENSE "${CWD}"/README "${CWD}"/releases "${PKG}"/usr/share/doc/$PRGNAM-"$VERSION"/
cat "${CWD}"/$PRGNAM.SlackBuild > "${PKG}"/usr/share/doc/$PRGNAM-"$VERSION"/$PRGNAM.SlackBuild

# Create 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

# rpi-eeprom is ARM ARCH independent
ARCH="noarch"

# 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<*>

