#!/bin/bash

# Slackware build script for fontforge

# Copyright 2006-2012,2016  Robby Workman  Tuscaloosa, Alabama, USA
# 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.

# Currently maintained by B. Watson <urchlay@slackware.uk>

# 20260802 bkw: major changes!
# - update for v20251009. this uses python3 for scripting, so some dependees
#   have to be updated, too.
# - get rid of the cidmaps.tar.gz, they're now included and installed with
#   fontforge itself.
# - get rid of the uthash and gnulib downloads, they're not used.
# - add DOCS=no in case someone has Sphinx installed but doesn't care about
#   building the fontforge docs.
# - get rid of the "please uninstall any previous version" exit. it's not
#   needed any more.
# - get rid of the freetype source download. it was never actually used, it
#   would have required an extra cmake arg to enable... and it's only for
#   debugging TrueType hints. since it already didn't work (cmake option was
#   missing anyway), I don't think anyone using this cares about it.

# 20260724 bkw, BUILD=4:
# - take over maintenance.
# - actually use SLKCFLAGS.

#
# 2022-02-08 19:11:35 +0000,20201107
# Switch to cmake
#
# 2020-10-12 20:48:40 +0100:
# Update to Version ff 20190801, gnulib 20200505~0747bf0
# Added --without-libuninameslist

# 20220409 bkw: Modified by SlackBuilds.org, BUILD=2:
# - make PRINT_PACKAGE_NAME work even if fontforge already installed.
# - get rid of /usr/share/doc.

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

PRGNAM=fontforge
VERSION=${VERSION:-20251009}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

ENABLE_BCINT=${ENABLE_BCINT:-yes}
FREETYPE=${FREETYPE:-2.11.1}
GNULIB=${GNULIB:-0.1}
UTHASH=${UTHASH:-2.3.0}

if [ "${DOCS:-yes}" = "yes" -a -x /usr/bin/sphinx-build ]; then
  DOCS=ON
fi

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
SLKCFLAGS+=" -DNDEBUG"

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION
chown -R root:root .
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_C_FLAGS_RELEASE="$SLKCFLAGS" \
    -DCMAKE_CXX_FLAGS_RELEASE="$SLKCFLAGS" \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_INSTALL_MANDIR=man \
    -DCMAKE_INSTALL_LIBDIR=lib$LIBDIRSUFFIX \
    -DENABLE_LIBSPIRO=OFF \
    -DENABLE_LIBUNINAMESLIST=OFF \
    -DENABLE_DOCS=${DOCS:-OFF} \
    -DCMAKE_BUILD_TYPE=Release ..
  make VERBOSE=1
  make install/strip DESTDIR=$PKG
  gzip -9 $PKG/usr/man/man*/*
cd ..

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

# Move docs (if Sphinx was installed)
if [ -d $PKG/usr/share/doc/$PRGNAM/ ]; then
  mv $PKG/usr/share/doc/$PRGNAM/* $PKGDOC
  rm -rf $PKG/usr/share/doc
fi

cp -a AUTHORS LICENSE $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild
find $PKG/usr/doc -type f -exec chmod 644 {} \+

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
