#! /bin/bash

# Copyright 2026 Vijay Marcel,India
# Modified by Vijay Marcel for rav1e SlackBuild
# Copyright 2023-2025  fourtysixandtwo <fourtysixandtwo@sliderr.net>
# Copyright 2022  Patrick J. Volkerding, Sebeka, MN, USA
# Copyright 2022  Heinz Wiesinger, Amsterdam, The Netherlands

# 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.
#
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the distribution.
#
#  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.


PRGNAM=rav1e
VERSION=0.8.1
BUILD=1
sys_arch=${sys_arch:-$(uname -m)}
CWD=$(pwd)

set -e
trap 'echo "$0 FAILED at line $LINENO!" | tee -a $CWD/error.log' ERR

case "$sys_arch" in
   	x86_64) export ARCH=x86_64 ;;
	  i*86) export ARCH=i586 ;;
	     *) echo "This script will not run on $sys_arch platform" && exit 1 ;;
esac

if [ "$ARCH" = "i586" -o "$ARCH" = "i686" ]; then
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  LIBDIRSUFFIX="64"
else
   echo "This script will not run on $ARCH" && exit 1
fi

# check if rust-opt is installed
if [ ! -d /opt/rust/bin ]; then
  echo "ERROR: The rust-opt slackbuild is required to be installed"
  exit 1
else
  export PATH="/opt/rust/bin:$PATH"
  if [ -z "$LD_LIBRARY_PATH" ]; then
    export LD_LIBRARY_PATH="/opt/rust/lib${LIBDIRSUFFIX}"
  else
    export LD_LIBRARY_PATH="/opt/rust/lib${LIBDIRSUFFIX}:$LD_LIBRARY_PATH"
  fi
fi

SOURCE_DATE_EPOCH="${SOURCE_DATE_EPOCH:-$(date +%s)}"

WORKDIR=$( mktemp -d )
cd $WORKDIR

# don't depend on user's ~/.cargo
mkdir -p cargohome
export CARGO_HOME=$(pwd)/cargohome

tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION

if [ ! -e "Cargo.toml" ]; then
  echo "No Cargo.toml file in $PRGNAM-$VERSION dir" 1>&2
  exit 1
elif [ -e "$CWD/Cargo-toml.patch" ]; then
  echo "INFO: Applying Cargo.toml patch"
  patch -Np1 < $CWD/Cargo-toml.patch
elif [ -e "$CWD/no-git-patch-builds.patch" ]; then
  echo "INFO: Applying no-git-patch-builds.patch patch"
  patch -Np1 < $CWD/no-git-patch-builds.patch
fi

# Configure cargo-vendor-filterer
if [ "$(grep -c "^\[package\]$" Cargo.toml)" -eq 1 ]; then
  echo "This is a normal cargo package."
  SRCTYPE="package"
elif [ "$(grep -c "^\[workspace\]$" Cargo.toml)" -eq 1 ]; then
  echo "This is a cargo workspace."
  SRCTYPE="workspace"
else
  echo "Cargo.toml does not contain [package] or [workspace], exiting."
  exit 1
fi

cat << EOF >> Cargo.toml
[$SRCTYPE.metadata.vendor-filter]
platforms = ["x86_64-unknown-linux-gnu", "i686-unknown-linux-gnu"]
all-features = true
EOF

if [ -f /usr/bin/cargo-vendor-filterer ]; then
  echo "INFO: Creating filtered vendor libs tarball..."
  /usr/bin/cargo-vendor-filterer | tee $WORKDIR/tmpconfig.toml
else
  echo "WARNING: Creating unfiltered vendor libs tarball!"
  cargo vendor | tee $WORKDIR/tmpconfig.toml
fi

# add vendored-sources section generated by cargo-vendor-filterer
mkdir -pv .cargo
cat $WORKDIR/tmpconfig.toml >> .cargo/config.toml
cd -

cd $WORKDIR
echo "Creating vendored tar file..."
tar --totals --checkpoint=100 --checkpoint-action=dot \
    --sort=name --mtime="@0" --owner=0 --group=0 --numeric-owner \
    --pax-option=exthdr.name=%d/PaxHeaders/%f,delete=atime,delete=ctime \
    -cJf $CWD/$PRGNAM-vendored-sources-$VERSION-$BUILD.tar.xz \
    $PRGNAM-$VERSION/{vendor,.cargo}
md5sum $CWD/$PRGNAM-vendored-sources-$VERSION-$BUILD.tar.xz
cd $CWD
rm -rf $WORKDIR
