#!/bin/bash

#include BSD license warranty disclaimer here... :D

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

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

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"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

# 1. Make sure you have all deps needed installed
#   REQUIRES="libwpe bubblewrap geoclue2 wpebackend-fdo xdg-dbus-proxy unifdef"

# 2. Build icu4c-70_1 and install in /opt

wget -c https://github.com/unicode-org/icu/releases/download/release-70-1/icu4c-70_1-src.tgz
tar xf icu4c-70_1-src.tgz
cd icu/source

mkdir build && cd build
../configure --prefix=/opt/icu4c-70 --disable-samples --disable-tests
make -j$(nproc)
make install

# NOTE we only need:
# icu4c-70/lib \
#    icu4c-70/include \
#    icu4c-70/lib/pkgconfig

rm -rf /opt/icu4c-70/{sbin,bin,share}

# 3. Configure webkit2gtk to Use This ICU.

wget -c https://www.webkitgtk.org/releases/webkitgtk-2.48.1.tar.xz

PRGNAM=webkit2gtk
SRCNAM=webkitgtk
VERSION=${VERSION:-2.48.1}
BUILD=${BUILD:-1}
TAG=${TAG:-_slint}
PKGTYPE=${PKGTYPE:-txz}

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

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 {} \;

patch -p1 < $CWD/unhide-deprecated-api.patch
patch -p1 < $CWD/3d5373575695b293b8559155431d0079a6153aff.patch

mkdir -p build
cd build

export ICU_ROOT=/opt/icu4c-70
export PKG_CONFIG_PATH=/opt/icu4c-70/lib/pkgconfig:${PKG_CONFIG_PATH:-}
export LD_LIBRARY_PATH=/opt/icu4c-70/lib:${LD_LIBRARY_PATH:-}

cmake -DPORT=GTK \
  -DCMAKE_C_FLAGS:STRING="$SLKCFLAGS" \
  -DCMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \
  -DCMAKE_BUILD_TYPE=Release \
  -DCMAKE_SKIP_RPATH=ON \
  -DCMAKE_INSTALL_PREFIX=/usr \
  -DICU_ROOT=$ICU_ROOT \
  -DCMAKE_PREFIX_PATH=$ICU_ROOT \
  -DLIB_INSTALL_DIR=/usr/lib${LIBDIRSUFFIX} \
  -DLIBEXEC_INSTALL_DIR=/usr/lib${LIBDIRSUFFIX}/webkit2gtk-4.0 \
  -DENABLE_GAMEPAD=OFF \
  -DENABLE_MINIBROWSER=ON \
  -DUSE_SOUP2=ON \
  -DENABLE_JOURNALD_LOG=OFF \
  -DENABLE_DOCUMENTATION=OFF \
  -DENABLE_BUBBLEWRAP_SANDBOX=OFF \
  -DUSE_SYSTEM_MALLOC=ON \
  -DUSE_GSTREAMER_TRANSCODER=OFF \
  -DUSE_AVIF=OFF \
  -DUSE_GTK4=OFF \
  -DUSE_LIBBACKTRACE=OFF \
  -DUSE_JPEGXL=OFF \
  -DUSE_SYSTEM_SYSPROF_CAPTURE=NO \
  -Wno-dev \
  -G Ninja \
  -DENABLE_SPEECH_SYNTHESIS=OFF \
  ..

"${NINJA:=ninja}" -j$(expr '(' $(nproc) + 1 ')' / 2)

DESTDIR=$PKG $NINJA -j1 install

mkdir -p $PKG/usr/share/gtk-doc/html/webkit{2,dom}gtk-4.0
cp -rf ../Documentation/* \
  $PKG/usr/share/gtk-doc/html/webkit2gtk-4.0

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a ../NEWS $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
