#!/bin/bash

# Slackware build script for jp2-pixbuf-loader

# Written by B. Watson (urchlay@slackware.uk)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# Notes:

# - I'm using the latest git because it actually has some important fixes.

# - gdk-pixbuf2 upstream used to support jpeg2000 via jasper, but
#   removed it from their repo a while back. I could have done a
#   jasper-pixbuf-loader like I did for pcx, but jasper doesn't
#   fully support the jpeg2000 standard. This probably doesn't fully
#   support it either, but it does support more of it anyway.

# - I only ever test this with qiv. geeqie can display jpeg2000 without
#   installing this, and I don't really know what else on SBo might use it.

# - /usr/share/thumbnailers/jp2-pixbuf.thumbnailer is included in the
#   package, but it may not be useful. It's for GUI filemanagers. I
#   checked dolphin and thunar, and they already know how to display
#   thumbnails for .jp2 files without this package. Tried nautilus,
#   and it won't display thumbnails *with* this package. Not being
#   a user of any of the above, I have no idea what's going on and
#   no real motivation to spend any more time on it. Including the
#   thumbnailer doesn't *hurt* anything so I'll leave it in case
#   someone finds it useful for something.

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

PRGNAM=jp2-pixbuf-loader
VERSION=${VERSION:-0.0.2+20240318_cbd8c7a}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

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}

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

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.xz
cd $PRGNAM-$VERSION
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod -h 755 {} + -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod -h 644 {} +

mkdir build
cd build
  CFLAGS="$SLKCFLAGS" \
  CXXFLAGS="$SLKCFLAGS" \
  meson .. \
    --buildtype=release \
    --infodir=/usr/info \
    --libdir=/usr/lib${LIBDIRSUFFIX} \
    --localstatedir=/var \
    --mandir=/usr/man \
    --prefix=/usr \
    --sysconfdir=/etc \
    -Dstrip=true \
    -Dgdk_pixbuf_query_loaders_path=/bin/true
  "${NINJA:=ninja}"
  DESTDIR=$PKG $NINJA install
cd ..

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
cp -a CHANGELOG* README* COPYING* $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
cat $CWD/doinst.sh > $PKG/install/douninst.sh

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