#!/bin/bash

# Slackware build script for pyside6-meta 

# Copyright 2026 Frank Honolka <info@snukware.com>
# 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=pyside6-meta			
SRCNAM=pyside-setup-everywhere-src			
VERSION=${VERSION:-$(echo $SRCNAM-*.tar.?z | rev | cut -d "." -f 3- | cut -d "-" -f1 | rev)}
BUILD=${BUILD:-1}
TAG=${TAG:-_snuk}	
PKGTYPE=${PKGTYPE:-txz}

# Automatically determine the architecture we're building on:
if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
# Unless $ARCH is already set, use uname -m for all other archs:
       *) ARCH=$( uname -m ) ;;
  esac
fi

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

TMP=${TMP:-/var/cache/snukware/tmp}		
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/var/cache/snukware/l}		

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -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 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e 

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCNAM-$VERSION
tar xvf $CWD/$SRCNAM-$VERSION.tar.xz
cd $SRCNAM-$VERSION
chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

CLANG_INSTALL_DIR="/usr" \
	cmake -B build -S $TMP/$SRCNAM-$VERSION -G Ninja \
	-DCMAKE_INSTALL_PREFIX=/usr \
	-DCMAKE_INSTALL_LIBDIR=/usr/lib$LIBDIRSUFFIX \
	-DCMAKE_BUILD_TYPE=None \
	-DSHIBOKEN_PYTHON_LIBRARIES=`pkg-config python3-embed --libs` \
	-DBUILD_TESTS=OFF \
	-DFORCE_LIMITED_API=no \
        -DNO_QT_TOOLS=yes
PYTHONPATH="$TMP"/"$SRCNAM-$VERSION"/build/sources \
	cmake --build build

DESTDIR="$PKG" cmake --install build/sources/shiboken6
DESTDIR="$PKG" cmake --install build/sources/shiboken6_generator

export PATH="/usr/lib$LIBDIRSUFFIX/qt6/bin:$PATH"
python3 setup.py egg_info --build-type=shiboken6
python3 setup.py egg_info --build-type=shiboken6-generator
_pythonpath=`python3 -c "from sysconfig import get_path; print(get_path('platlib'))"`

if [ ! -d $PKG/${_pythonpath} ]; then
	mkdir -p $PKG/$_pythonpath || exit 1
fi

cp -r shiboken6*.egg-info $PKG/$_pythonpath || exit 1

DESTDIR="$PKG" cmake --install build/sources/pyside6

export PATH="/usr/lib$LIBDIRSUFFIX/qt6/bin:$PATH"
python setup.py egg_info --build-type=pyside6
_pythonpath=`python3 -c "from sysconfig import get_path; print(get_path('platlib'))"`

if [ ! -d $PKG/${_pythonpath} ]; then
	mkdir -p $PKG/$_pythonpath || exit 1
fi

cp -r PySide6.egg-info $PKG/$_pythonpath || exit 1

DESTDIR="$PKG" cmake --install build/sources/pyside-tools
echo "export LD_PRELOAD="`python3 -c "from sysconfig import get_config_var; print(get_config_var('LDLIBRARY'))"` > pyside6-designer
echo "exec designer6 \$@" >> pyside6-designer

install -Dm755 pyside6-designer -t $PKG/usr/bin || exit 1

# start /usr/include fix...hopefully fixed with 6.12.0  #
if [ ! -d $PKG/usr/include ]; then
	mkdir -p $PKG/usr/include
fi

cp -a $PKG/usr/PySide6/include $PKG/usr/include/PySide6 || exit 1
cp -a $PKG/usr/shiboken6/include/ $PKG/usr/include/shiboken6 || exit 1
rm -rf $PKG/usr/shiboken6
rm -rf $PKG/usr/PySide6

sed -i 's/includedir\=\/usr\/PySide6\/include/includedir\=\/usr\/include\/PySide6/g' \
	$PKG/usr/lib/pkgconfig/pyside6.pc || exit 1
sed -i 's/includedir\=\/usr\/shiboken6\/include/includedir\=\/usr\/include\/shiboken6/g' \
	$PKG/usr/lib/pkgconfig/shiboken6.pc || exit 1

sed -i 's#${_IMPORT_PREFIX}/shiboken6/include#${_IMPORT_PREFIX}/include/shiboken6#g' \
  	$PKG/usr/lib/cmake/Shiboken6/Shiboken6Targets.cmake || exit 1

sed -i 's#${_IMPORT_PREFIX}/PySide6/include#${_IMPORT_PREFIX}/include/PySide6#g' \
  	$PKG/usr/lib/cmake/PySide6/PySide6Targets.cmake || exit 1

# end of /usr/include fix

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 -name perllocal.pod -o -name ".packlist" -o -name "*.bs" | xargs rm -f || true

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
DOCS="README.*"
cp -a $DOCS $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

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

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