#!/bin/bash
ulimit -s unlimited
shopt -s extglob

# doxygen.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <mozes@slackware.com>
# 28-Sep-2004
#
# Copyright 2008-2026  Patrick J. Volkerding, Sebeka, MN, 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.

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
slackset_var_cwds

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Extract source:
tar xf $CWD/$PKGNAM-${VERSION}.*tar.!(*sign|*asc|*sig)
#tar xvvf $PORTCWD/sources/$PKGNAM-${VERSION}.*tar.!(*sign|*asc|*sig)
cd $PKGNAM*/ || failextract
slackhousekeeping

# Apply patches for ARM:
#for i in \
#  doxgen-1.8.17-broken-urls-in-the-xml-output.patch.gz \
#  doxygen-1.8.17-test-suite-is-failing.patch.gz \
# ; do auto_apply_patch $PORTCWD/sources/$i || exit 1
#done

# If we don't have qt installed (during bootstrap perhaps), don't
# ask for it.
if [ ! -f /var/log/packages/qt-[0-9]* ]; then
   echo "No QT installed: cannot build with Document Wizard"
   unset QTDIR
   QT_BUILDWIZARD="OFF"
 else
   echo "QT installed: build with Document Wizard feature"
   QT_BUILDWIZARD="yes"
   export QTDIR=/usr/lib${LIBDIRSUFFIX}/qt
fi

# Some times bits of the documentation fails to build, but this fixes it:
# This is a bug in some of the ARM packages, but I'm not sure which yet.
# mktexlsr > yes | updmap-sys --syncwithtrees -> updmap-sys
# fixes a build problem with the PDF documentation, so I assume that some
# other packages installed subsequently to texlive are overwriting the files.
# Needs investigation, but for now, this is the hack.
rm -rf ~/.texlive/
mktexlsr
yes | updmap-sys --syncwithtrees
updmap-sys
# and for good measure:
texconfig rehash
texconfig-sys rehash

#BUILDDOCS="OFF"
#BUILDDOCS="ON"
#echo "***** Building docs? $BUILDDOCS *******"

# Apply patches:

# Configure:
# Set CFLAGS:
#sed -i 's/^CFLAGS.*=\(.*\)$/CFLAGS = '"$SLKCFLAGS"' \1/'  qtools/Makefile.qtools
#sed -i 's/^CXXFLAGS.*=\(.*\)$/CXXFLAGS = '"$SLKCFLAGS"' \1/'  qtools/Makefile.qtools
export LDFLAGS="-Wl,-z,relro -Wl,--as-needed  -Wl,-z,now"
mkdir build
pushd build
#

# Using build_doc=OFF (and comment out "make docs" below) because a lack of
# tabularray.sty causes it to fail. Perhaps we'll look into adding that to
# TeXLive.

# Note: on ARM we added ' -DBUILD_SHARED_LIBS=OFF ..' otherwise
# the linker doesn't pick up one of the functions in the 'msc' library.
# There are no shared objects shipped in the Slackware package, so - this works for me.
# I know that it's *enabled* further up in the list below, but I'm leaving that in place
# since it makes it easier to merge changes from Slackware upstream.
# Mar-2021: it's included in x86 build now.
cmake \
    \
    \
    -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON \
    \
    \
    -DCMAKE_C_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_BUILD_TYPE=Release \
    -DDOC_INSTALL_DIR:STRING="/doc/doxygen-$VERSION" \
    -DBUILD_SHARED_LIBS=OFF \
    -Dbuild_app=ON \
    -Dbuild_doc=OFF \
    -Dbuild_parse=ON \
    -Dbuild_search=ON \
    -Dbuild_wizard=ON \
    -Dbuild_xmlparser=ON \
    -Duse_sqlite3=ON \
    -G "Unix Makefiles" .. || failconfig
popd

# Why -O3 is in here I don't know..
grep -Flr -- '-O3' . | xargs sed -i 's?-O3??g'

# Build:
echo "***** Doing main build *****"
make -C build $NUMJOBS || failmake # make -C build || failmake
#make -C build $NUMJOBS docs || failmake # make -C build || failmake

#if [ "$BUILDDOCS" = "ON" ]; then
#   echo "***** Doing docs build *****"
#   make --trace -C build docs #|| exit 1
#   # The doxygen manual won't build in to PDF format.  I've tried on Slackware ARM 14.2 as well
#   # and no dice.  There's no real debug output for pdflatex.  Fixes welcome!
#   # For now, let's patch out the installation of the PDF manual so that the remainder
#   # gets installed:
##   ( cd build
##     auto_apply_patch $PORTCWD/sources/nopdfmanual.diff ) || exit 1
#fi

# Install:
echo "***** Doing make install *****"
make -C build install DESTDIR=$PKG || exit 1

# Fix manpages location:
if [ -d $PKG/usr/share/man ]; then
   mv -fv $PKG/usr/share/man $PKG/usr
   rmdir $PKG/usr/share
fi

# Install docs:
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION
cp -a INSTALL LANGUAGE.HOWTO LICENSE PLATFORMS README* VERSION \
      $PKG/usr/doc/$PKGNAM-$VERSION
changelogliposuction ChangeLog $PKGNAM $VERSION # Trim down a "ChangeLog" file

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slack_delete_lafiles # delete usr/lib{,64}/*.la
#slackstriprpaths     # strip rpaths
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh
slackmp         # run makepkg -l y -c n

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
