#!/bin/bash

# Slackware build script for srcML

# Copyright 2017 Marcel Steinbeck Germany
# Copyright 2026 xaizek <xaizek@posteo.net>
# 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.

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

PRGNAM=srcML
VERSION=${VERSION:-1.1.0}
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" -o "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=$ARCH -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" -o "$ARCH" = "aarch64" ]; 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 --no-same-owner
cd $PRGNAM-$VERSION

# Devs did some weird shit in v1.1.0 with downloading and patching third-party
# sources.  So patch the build system to not download anything and use installed
# CLI11.  The rest of the dependencies is pulled as package sources and placed
# where they can be found.  This is also why antlr2 is no longer a dependency,
# it's built in place.
patch -p1 < $CWD/no-network-build.patch
# Set up https://www.antlr2.org/.
tar xvf $CWD/antlr-2.7.7.tar.gz
# Set up https://github.com/vit-vit/CTPL.
mkdir ctpl
cp $CWD/ctpl_stl.h ctpl/
# Set up https://github.com/mohaps/TinySHA1.
cp $CWD/TinySHA1.hpp src/parser

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 {} + \) \)

# If cmake-opt is present, assume /usr/bin/cmake < v3.28 (i.e., running 15.0).
if [ -f /etc/profile.d/cmake-opt.sh ]; then
  . /etc/profile.d/cmake-opt.sh
fi

mkdir -p build
cd build
  cmake \
    -DCMAKE_C_FLAGS_RELEASE="$SLKCFLAGS" \
    -DCMAKE_CXX_FLAGS_RELEASE="$SLKCFLAGS" \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_INSTALL_LIBDIR=lib$LIBDIRSUFFIX \
    -DCMAKE_INSTALL_MANDIR=man \
    -DCMAKE_INSTALL_DOCDIR=doc/$PRGNAM-$VERSION \
    -DBUILD_CLIENT_DOC=ON \
    -DCMAKE_BUILD_TYPE=Release ..
  make
  make install/strip DESTDIR=$PKG
cd ..

# See no option to disable building or installing this one.
rm $PKG/usr/lib${LIBDIRSUFFIX}/libsrcml.a

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
cp -a \
  CHANGES.md COPYING.txt README.md \
  $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$TAG.$PKGTYPE
