#!/bin/sh
#
# This script is used to create a fake orig tarball that concatenates
# the DKMS packages from a DOCA_OFED release source tarball.
#
# It also removes some undesired files that are shipped in that tarball.
# Hopefully in the future upstream starts to clean up the releases so
# we do not have to do it here.
#
# Example usage: ./create-orig.sh ../../MLNX_OFED_SRC-debian-24.04-0.7.0.0.tgz

set -eu

DOCA_OFED_TGZ="$1"

tmpdir="$(mktemp -td doca-ofed-create-orig.XXXXXXXXXX)"
echo "Using tmpdir $tmpdir"

echo "Extracting $DOCA_OFED_TGZ to $tmpdir"
tar xf "$DOCA_OFED_TGZ" -C "$tmpdir"

# Use a loop to store shell glob into var
for dir in "$tmpdir"/MLNX*
do
	DOCA_OFED_DIR="$dir"
done

DOCA_OFED_DIRNAME=$(basename "$dir")

# Example version: 24.04-0.6.6.0
DOCA_OFED_VERSION=${DOCA_OFED_DIRNAME##MLNX_OFED_SRC-}
echo "Detected DOCA_OFED version $DOCA_OFED_VERSION"

# Split minor from major version, and recompose to a Debian-compatible
# full version, eg. 24.04.0.6.6.0
DOCA_OFED_VERSION_MAJOR=${DOCA_OFED_VERSION%%-*}
echo "Extracted DOCA_OFED version major $DOCA_OFED_VERSION_MAJOR"

DOCA_OFED_VERSION_MINOR=${DOCA_OFED_VERSION##*-}
echo "Extracted DOCA_OFED version minor $DOCA_OFED_VERSION_MINOR"

if [ ! -f debian/doca_version ]; then
	echo "Failed to find debian/doca_version file, cannot extract DOCA version"
	exit 1
fi
DOCA_VERSION_MAJOR=$(cat debian/doca_version)
echo "DOCA version major $DOCA_VERSION_MAJOR"
if [ -z "$DOCA_VERSION_MAJOR" ]; then
	echo "Failed to extract DOCA version major from changelog"
	exit 1
fi

#DOCA_OFED_DEBIAN_VERSION="$DOCA_OFED_VERSION_MAJOR.$DOCA_OFED_VERSION_MINOR"
# We should get something like 3.4.0+0.8.5.0
DOCA_OFED_DEBIAN_VERSION="$DOCA_VERSION_MAJOR+$DOCA_OFED_VERSION_MINOR"
echo "Recomposed DOCA_OFED debian version $DOCA_OFED_DEBIAN_VERSION"

SOURCE_DATE_EPOCH=${SOURCE_DATE_EPOCH:-$(dpkg-parsechangelog -STimestamp)}
echo "Using SOURCE_DATE_EPOCH $SOURCE_DATE_EPOCH"


DOCA_OFED_SRCDIR=$DOCA_OFED_DIR/SOURCES

dest="$tmpdir/doca-ofed"
mkdir "$dest"

for pkg in \
	mlnx-ofed-kernel \
	iser \
	isert \
	mlnx-nfsrdma \
	mlnx-nvme \
	srp \
	kernel-mft \
	xpmem
do
	pkgdest="$dest/$pkg"
	echo "Extracting $pkg to $pkgdest"
	mkdir "$pkgdest"
	tar xf "$DOCA_OFED_SRCDIR/${pkg}_"*.orig.tar* -C "$pkgdest" --strip-components 1
done

echo "Removing upstream cache files"
rm -vrf "$dest/mlnx-ofed-kernel/compat/autom4te.cache"

echo "Removing upstream temporary files"
rm -vf "$dest/mlnx-nvme/host/.Makefile.swp"
rm -vf "$dest/mlnx-ofed-kernel/drivers/nvme/host/.Makefile.swp"

echo "Removing upstream VCS files"
rm -vf "$dest/mlnx-ofed-kernel/.gitignore"
rm -vf "$dest/mlnx-ofed-kernel/compat/config/.gitignore"
rm -vrf "$dest/xpmem/.github"
rm -vf "$dest/xpmem/.gitignore"

tarfile="doca-ofed_$DOCA_OFED_DEBIAN_VERSION.orig.tar.xz"
echo "Creating tar file $tarfile"

tar acf "${tarfile}" -C "$tmpdir" "doca-ofed" \
	--sort=name \
	--mtime="@${SOURCE_DATE_EPOCH}" \
	--owner=0 --group=0 --numeric-owner

echo "Cleanup, removing $tmpdir"

echo "Done"
