#!/bin/bash

# Slackware build script for libation-bin

# Copyright 2026 Ioannis Anagnostakis GR
# 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.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=libation-bin
VERSION=${VERSION:-13.4.1}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

SRCNAM64=Libation
SRCNAM32=
COMPRESS=deb

Disposition64="$SRCNAM64"."$VERSION"-linux-chardonnay-amd64
Disposition32=

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

if [ "$ARCH" = "i586" ]; then
  SRCNAM="$SRCNAM32"
  DISPO="$Disposition32"
elif [ "$ARCH" = "i686" ]; then
  SRCNAM="$SRCNAM32"
  DISPO="$Disposition32"
elif [ "$ARCH" = "x86_64" ]; then
  SRCNAM="$SRCNAM64"
  DISPO="$Disposition64"
else
  SRCNAM=
  DISPO=
fi

if [ -z "$SRCNAM" ] || [ -z "$COMPRESS" ]; then
  echo "SRCNAM and/or COMPRESS are empty, exiting."
  exit 1
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT


if [[ $COMPRESS = "deb" ]]; then
 ar x "$CWD"/$SRCNAM.$COMPRESS 2>/dev/null || \
 ar x "$CWD"/"$DISPO"."$COMPRESS" 2>/dev/null || \
    { echo "No valid archive found!"; exit 1; }
	rm debian-binary control.tar.*
	tar -xJf data.tar.xz -C "$PKG" 2>/dev/null || \
    tar --use-compress-program=unzstd -xf data.tar.zst -C "$PKG" 2>/dev/null || \
    tar -xzf data.tar.gz -C "$PKG" 2>/dev/null || \
    { echo "No valid archive found!"; exit 1; }
rm data.tar.*
cd "$PKG"
elif
[[ $COMPRESS = "pkg.tar.zst" ]]; then
# extract directly to $PKG/
tar --use-compress-program=unzstd -xf "$CWD"/*.$COMPRESS -C "$PKG" || tar -I unzstd -xf "$CWD"/*.$COMPRESS -C "$PKG" 2>/dev/null || \
    { echo "No valid archive found!"; exit 1; }
cd "$PKG"
elif
 [[ $COMPRESS == "AppImage" ]]; then
 if [ -d "$TMP"/squashfs-root ]; then
    rm -rf "$TMP"/squashfs-root
 fi
 if [ -d "$TMP"/"$PRGNAM-$VERSION" ]; then
    rm -rf "$TMP"/"$PRGNAM-$VERSION"
 fi
  mv "$CWD"/"$SRCNAM"*."$COMPRESS" "$TMP"/"$SRCNAM-$VERSION"."$COMPRESS"
 chmod +x "$TMP"/"$SRCNAM-$VERSION"."$COMPRESS"
 pushd "$TMP" || exit 1
 ./"$SRCNAM-$VERSION"."$COMPRESS" --appimage-extract
 popd || exit 1
 mv "$TMP"/squashfs-root "$TMP"/"$PRGNAM-$VERSION"
 cd "$TMP"/"$PRGNAM-$VERSION"
elif
 [[ $COMPRESS == "snap" ]]; then
 cd "$TMP"
 unsquashfs -d "$CWD"/"$SRCNAM"."$COMPRESS"
 mv squashfs-root "$PRGNAM-$VERSION"
 cd "$PRGNAM-$VERSION"
elif
 [[ $COMPRESS = "rpm" ]]; then
# extract to $PKG/
 rpm -Kv "$CWD"/"$SRCNAM.$COMPRESS"
 rpm2cpio "$CWD"/"$SRCNAM"."$COMPRESS" > "$TMP"/"$SRCNAM"."$COMPRESS".cpio
 cd "$PKG"
 cpio -idv < "$TMP"/"$SRCNAM"."$COMPRESS".cpio
else
echo "COMPRESS is not deb or rpm or snap or AppImage or pkg.tar.zst file"
echo "Add here custom commands for $COMPRESS to proceed binary repackage..."
exit 1
fi

chown -R root:root .
find . ! -type l -type d ! -perm 755 -exec chmod -f 755 {} +

mv $PKG/usr/lib $PKG/usr/lib64
rm -rf $PKG/usr/lib || true

mkdir -p $PKG/usr/bin
( cd $PKG/usr/bin ; rm -rf hangover )
( cd $PKG/usr/bin ; ln -sf /usr/lib64/libation/Hangover hangover )
( cd $PKG/usr/bin ; rm -rf libation )
( cd $PKG/usr/bin ; ln -sf /usr/lib64/libation/Libation libation )
( cd $PKG/usr/bin ; rm -rf libationcli )
( cd $PKG/usr/bin ; ln -sf /usr/lib64/libation/LibationCli libationcli )

# Strip binaries and libraries
find $PKG -type f -print0 | xargs -0 file | grep ELF | cut -d: -f1 \
  | grep -v -E "libcoreclr|libclrjit|libhostfxr|libhostpolicy" \
  | xargs strip --strip-unneeded 2>/dev/null || true
find "$PKG"/ -type f -name "*.so*" -exec chmod 755 {} \;

mkdir -p "$PKG"/usr/doc/$PRGNAM-"$VERSION"
if [[ $COMPRESS != "rpm" && $COMPRESS != "deb" && $COMPRESS != "pkg.tar.zst" ]]; then
cp -a \
  documentation \
  $PKG/usr/doc/$PRGNAM-$VERSION
else
if [ -d "$PKG/usr/share/doc/$PRGNAM" ]; then
    mv "$PKG/usr/share/doc/$PRGNAM" "$PKG/usr/doc/$PRGNAM-$VERSION/"
fi

# Move man pages, if exists
if [ -d "$PKG/usr/share/man" ]; then
    mv "$PKG/usr/share/man" "$PKG/usr/man/"
fi

if [ -d "$PKG/usr/share/licenses" ]; then
    mv "$PKG/usr/share/licenses" "$PKG/usr/doc/$PRGNAM-$VERSION/"
elif [ -f "$PKG/usr/share/LICENSE" ]; then
    mv "$PKG/usr/share/LICENSE" "$PKG/usr/doc/$PRGNAM-$VERSION/"
fi
find "$PKG"/usr/doc/"$PRGNAM"-"$VERSION" -type f -exec chmod 644 {} \;
fi

cat "$CWD"/$PRGNAM.SlackBuild > $PKG/usr/doc/"$PRGNAM-$VERSION"/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg --remove-tmp-rpaths -l y -c n "$OUTPUT"/"$PRGNAM"-"$VERSION"-"$ARCH"-"$BUILD""$TAG"."$PKGTYPE"
