#!/bin/bash
# Slackware build script for <carla>
#
# Michales (clavisound) Michaloudes korgie@gmail.com <2018-2022>

# 20261105 bkw: Modified by SlackBuilds.org:
# - remove SLKCFLAGS (they were never used).
# - update README to mention python3-pyliblo as optional.

# 20230701 bkw: Modified by SlackBuilds.org, BUILD=2:
# - +x permissions for shared libs.
# - rm INSTALL.md (compile instructions) from doc dir.

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

PRGNAM=carla
VERSION=${VERSION:-2.5.2}
BUILD=${BUILD:-2}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

SRCNAM=Carla

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

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

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

# 20261105 bkw: SLKCFLAGS weren't being used, removed them.
LIBDIRSUFFIX=""
if [ "$ARCH" = "x86_64" -o "$ARCH" = "aarch64" ]; then
  LIBDIRSUFFIX="64"
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCNAM-$VERSION
tar xvf $CWD/$SRCNAM-$VERSION.tar.gz --no-same-owner
cd $SRCNAM-$VERSION
chown -R root:root .
# upstream ships archive with broken symlinks, remove
find -L . -type l -exec rm -f {} \+
find . ! -type l    -a \( \
  \(     -perm /111 -a ! -perm 755 -a -exec chmod -f 755 {} + \) -o \
  \(   ! -perm /111 -a ! -perm 644 -a -exec chmod -f 644 {} + \) \)

sed -i -e /CARLA_LIBDIR/s/\"lib\"/\"lib$LIBDIRSUFFIX\"/ data/carla-single

make PREFIX=/usr LIBDIR=/usr/lib$LIBDIRSUFFIX
make install PREFIX=/usr LIBDIR=/usr/lib$LIBDIRSUFFIX DESTDIR=$PKG

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

find $PKG -type f -a -name '*.so' -exec chmod 755 {} +

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
cp -a README.md doc/* $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$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 -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
