#!/bin/bash
#
# ex:set ai shiftwidth=4 inputtab=spaces smarttab noautotab:

# Slackware build script for cglm

# Copyright 2023 Brian Hoffpauir, USA
# Modified and now maintained by B. Watson <urchlay@slackware.uk>
# 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.

# 20261020 bkw:
# - take over maintenance. the new fsv requires this.
# - update for v0.9.6.
# - remove all the build options except building the docs, and make
#   that DOCS instead of CGLM_BUILD_DOCS. it defaults to yes, but
#   really it autodetects the optional deps and skips building
#   the docs if they're missing.

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

PRGNAM=cglm
VERSION=${VERSION:-0.9.6}
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}

# 20261020 bkw: No SLKCFLAGS here. This library is "highly
# optimized", so we'll go with upstream's default flags. If we set
# CMAKE_C_FLAGS_RELEASE, it just puts -O3 after our -O2 anyway. And
# it always builds with -fPIC. Also, it's smart enough to get lib vs.
# lib64 right by itself.

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz --no-same-owner
cd $PRGNAM-$VERSION
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 {} + \) \)

mkdir -p build
cd build
cmake \
  -DCMAKE_INSTALL_PREFIX=/usr \
  -DCMAKE_BUILD_TYPE=Release \
  -DCGLM_SHARED=ON \
  -DCGLM_STATIC=OFF \
  -DCGLM_USE_C99=OFF \
  -DCGLM_USE_TEST=OFF ..

make VERBOSE=1
make install/strip DESTDIR=$PKG
cd -

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC

WITHDOCS=WITHOUT

[ "${DOCS:-yes}" = "yes" ] &&
type -p sphinx-build &>/dev/null && \
python3 -c 'import sphinx_rtd_theme' &>/dev/null &&
sphinx-build docs/source $PKGDOC/html &&
WITHDOCS=WITH

cp -a *.md CREDITS* LICENSE* $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
sed "s,@WITHDOCS@,$WITHDOCS," < $CWD/slack-desc > $PKG/install/slack-desc

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