#!/bin/bash
# CUSTOM_BUILD_HARDENED
export CFLAGS="${CFLAGS:--O2} -fhardened -fPIC"
export CXXFLAGS="${CXXFLAGS:--O2} -fhardened -fPIC"
export LDFLAGS="${LDFLAGS:-} -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wl,-z,ibt -Wl,-z,shstk"

# Slackware Build script for txt2tags

# Originally written 2009, eviljames <storgeek [at] gmail [dot] com>
# Now maintained by B. Watson <urchlay@slackware.uk>

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

# 20231120 bkw: update for v3.9.
# 20230711 bkw: update for v3.8.
# 20220110 bkw:
# - take over maintenance.
# - update for v3.7.

# Note: the old version had a lot of things that are missing from
# the 3.x branch. See https://github.com/txt2tags/txt2tags/ for
# details. The 3.x branch has the sole advantage of being written in
# python 3. Since this is only included on SBo as a dependency for
# xdgmenumaker, I don't care... but maybe someone might want to do a
# txt2tags-legacy2 build if they need it.

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

PRGNAM=txt2tags
VERSION=${VERSION:-3.9}
BUILD=${BUILD:-1}
PKGTYPE=${PKGTYPE:-txz}

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.$PKGTYPE"
  exit 0
fi

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

set -e

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

# --- Fix invalid regex for Python 3.11+ ---------------------------------
# Python 3.11+ rejects inline flags like (?i) that are not at the start
# of the pattern. The docs .t2t files contain patterns such as
#   '^(?i) *<LI>.*[Tt]itle Level .*'
# which must become
#   '(?i)^ *<LI>.*[Tt]itle Level .*'
# Use grep -F so '^' is treated as a literal, not an anchor, and scan
# the whole tree since these patterns appear mid-line.
if grep -rqF '^(?i)' . 2>/dev/null; then
    echo ">>> Patching invalid (?i) placement in docs regexes..."
    grep -rlF '^(?i)' . 2>/dev/null | while IFS= read -r f; do
        echo "    fixing: $f"
        sed -i 's/\^(?i)/(?i)^/g' "$f"
    done
fi
# ------------------------------------------------------------------------

python3 setup.py install --root=$PKG

export PATH=$PATH:$PKG/usr/bin
export PYTHONPATH=$PKG/$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"):$PYTHONPATH
cd docs
sh build-docs.sh
rm */*.t2t *.sh
cd -

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
cp -a COPYING *.md extras samples test docs/* $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$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.$PKGTYPE
