#!/bin/bash

# Slackware build script for cproc

# Written by B. Watson (urchlay@slackware.uk)

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

# 20260928 bkw:
# - update for v20260601_d1c53dd.
# - build our own qbe. it's only a single binary, and this way I
#   don't have to convince the qbe maintainer to update his build.
#   it gets installed in /usr/libexec/cproc, so it won't conflict
#   with the qbe build.

# Note: thought about including mcpp (either in this build
# or a separate one) so cproc wouldn't have to use gcc's
# preprocessor. There even used to be a development/mcpp
# SlackBuild... for Slackware 12.1 (in 2010).
# However, mcpp looks to be old and unmaintained upstream, and cproc
# doesn't seem to support using it (would require patching, not gonna
# do).

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

PRGNAM=cproc
VERSION=${VERSION:-20260601_d1c53dd}
QBEVER=${QBEVER:-1.3}
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}

# No need for LIBDIRSUFFIX (nothing gets installed there), can't
# use regular SLKCFLAGS because they get passed to cproc itself, and
# it doesn't support -fPIC, -march=, etc. -O2 is allowed, but ignored.

set -e

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

# cproc needs qbe, and it (currently) must be 1.3.
make -C qbe-$QBEVER
sed -i "/CFLAGS/s,$, $SLKCFLAGS," Makefile
export PATH=$( pwd/ )qbe-$QBEVER:$PATH
mkdir -p $PKG/usr/libexec/cproc
install -s qbe-$QBEVER/qbe $PKG/usr/libexec/cproc

# cproc needs to know the location of the ELF "interpreter" (the
# runtime linker). The defaults in the configure script are OK for
# x86_64 but wrong for aarch64. Rather than hardcode anything here,
# look and see what's in use.
ELFTERP="$( readelf -p .interp /bin/ls | sed -n '/\.so\./s,.* ,,p' )"

# Non-standard configure (not autoconf). We have to set host and
# target because otherwise it detects x86_64-slackware-linux, which
# cproc doesn't support. In theory they could be different, which
# would give us a cross compiler... but it would require the crt*.o
# and a binutils for the target platform. Not gonna spend time on
# that.
./configure \
  --host=$ARCH-linux-gnu \
  --target=$ARCH-linux-gnu \
  --with-ldso="$ELFTERP" \
  --with-qbe=$PKG/usr/libexec/cproc/qbe \
  --prefix=/usr

make bootstrap

# Manual install. It's only 2 binaries and 1 man page.
# Use the binaries compiled by cproc, not gcc. stage2/ and stage3/ binaries
# are identical (or else 'make bootstrap' would have failed, above). These
# are already stripped.
mkdir -p $PKG/usr/bin $PKG/usr/man/man1
cp -a stage2/cproc{,-qbe} $PKG/usr/bin
gzip -9c < $PRGNAM.1 > $PKG/usr/man/man1/$PRGNAM.1.gz

# 20260928 bkw: since we had to build with a $PKG path baked in,
# fix it by patching the binary.
offset="$(
  strings -td $PKG/usr/bin/cproc | fgrep $TMP/package | awk '{ print $1 }'
)"
if [ "$offset" = "" ]; then
  echo "Couldn't get offset of $TMP in binary, giving up" 1>&2
  exit 1
fi
gcc -Wall $SLKCFLAGS -o patchbin $CWD/patchbin.c
./patchbin $PKG/usr/bin/cproc "$offset" /usr/libexec/cproc/qbe

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

# Self-test can be disabled, if some future version has issues.
[ "${CHECK:-yes}" = "yes" ] && make check &> $PKGDOC/check-results.txt

cp -a README* LICENSE* doc/* $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
