#!/bin/bash

# Slackware build script for z88dk

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

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

# 20261112 bkw:
# - update for v2.5.
# - clean up the man pages some.
# - get rid of -Werror.
# - actually use SLKCFLAGS.
# - patch around an invalid upstream assumption about a boost bug.
# - avoid a dos2unix dependency.

# 20240322 bkw: *Finally* updated, v2.3. Many thanks to fuzzix, who did
# most of the work for this upgrade.

# 20230114 bkw: BUILD=2
# - do not ship pre-compiled binaries in the doc dir
# - do not ship the windows installer

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

PRGNAM=z88dk
VERSION=${VERSION:-2.5}
ZSDCC_REV=16639
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"
elif [ "$ARCH" = "x86_64" -o "$ARCH" = "aarch64" ]; then
  SLKCFLAGS="-O2 -fPIC"
else
  SLKCFLAGS="-O2"
fi

set -e

ZSDCCTGZ=zsdcc_r${ZSDCC_REV}_src.tar.gz

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM
tar xvf $CWD/$PRGNAM-src-$VERSION.tgz --no-same-owner
cd $PRGNAM

# We have to patch a file that's inside the zsdcc tarball, which means
# extract, patch, and tar up the result. Annoying.
# Upstream aborts the build if it detects boost < 1.79. However, the
# bug that's referenced:
# https://sourceforge.net/p/sdcc/bugs/3772/
# ...has a test case:
# https://sourceforge.net/p/sdcc/bugs/_discuss/thread/011e5c669a/6644/attachment/test2.cc
# ...which I have tried with Slackware 15.0's boost-1.70, and it does
# not trigger the boost bug. So it's safe to compile with our boost.
# It would have been nice of upstream to control their #error with
# an #ifdef so I could just pass a -D in CFLAGS instead of this
# gods-awful mess.
mkdir a
cd a
echo "Patching zsdcc_r${ZSDCC_REV}_src.tar.gz..."
tar xf $CWD/$ZSDCCTGZ --no-same-owner
perl -i -000 -ne 'print unless /#error/' src/sdcc-build/src/SDCCsalloc.hpp
tar cfz ../$ZSDCCTGZ src
echo "Patched."
cd -
rm -rf a

# Upstream permissions are bad. Please don't replace with "find . -L
# ..." boilerplate.
find -L . -name CVS -a -exec rm -rf {}     + -o \
          -type f   -a -exec chmod 0644 {} + -o \
          -type d   -a -exec chmod 0755 {} +
chmod +x *.sh

# 20261113 bkw: it's completely stupid to depend on dos2unix, it's a
# one-line perl script.
mkdir b
cat <<EOF > b/dos2unix
#!/usr/bin/perl -pi
s,\\r,,g;
EOF
chmod +x b/dos2unix
export PATH=$(pwd)/b:$PATH

export CFLAGS="$SLKCFLAGS"
export CXXFLAGS="$SLKCFLAGS"

# Default prefix.
# We could delete config.h and have make rebuild it with the PREFIX,
# but Z88DK_VERSION gets wiped.
sed -i "s,/usr/local,/usr," src/config.h
export PREFIX=/usr

# Parallel make fails randomly when building the compiler. It also
# fails reliably when building the z80 native libs. Sadly, this
# makes the build take 3x as long to finish :(
export MAKEFLAGS=-j1

# 20261112 bkw: disable -Werror, use our flags in the sdcc sub-build
sed -i 's,--disable-sdbinutils,& --disable-werror,' Makefile
find . -name Makefile | \
  xargs sed -i \
    -e 's,-Werror,,' \
    -e "s,-O3,$SLKCFLAGS,g"

export BUILD_SDCC=1
# The following should skip attempting the SVN fetch of zsdcc,
# and also skip the HTTP fetch as the appropriate tarball is present
export BUILD_SDCC_HTTP=1
./build.sh -i $PREFIX
make install DESTDIR=$PKG/

# strip strips everything but chokes on the perl script.
strip $PKG/usr/bin/* 2>/dev/null || true

# 20261112 bkw: 2 copies of this get installed with different names.
# Assume there's a good reason for it; replace one with a symlink.
rm -f $PKG/usr/bin/z88dk-z80asm
ln -s z80asm $PKG/usr/bin/z88dk-z80asm

# man pages from Debian:
# http://http.debian.net/debian/pool/main/z/z88dk/z88dk_1.8.ds1-10.debian.tar.gz
# They're for an older version of z88dk, and there's not a complete
# set of them, but it's better than nothing I hope.
mkdir -p $PKG/usr/man/man1
for i in $CWD/man/*.1; do
  gzip -9c < $i > $PKG/usr/man/man1/$( basename $i ).gz
done
ln -s z80asm.1.gz $PKG/usr/man/man1/z88dk-z80asm.1.gz

# 20230114 bkw: do not ship executables in the doc dir.
# 20240322 bkw: make this smarter (but uglier).
find support -type f | \
  xargs file -m/etc/file/magic/elf | \
  grep ELF | \
  cut -d: -f1 | \
  xargs rm -f

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
cp -a changelog* README* LICENSE doc examples support $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

# 20240322 bkw: stuff's getting installed to the doc dir with wrong owner.
find $PKGDOC -exec chown root:root {} +

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
