#!/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.

# 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.3}
ZSDCC_REV=14210
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" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM
tar xf $CWD/$PRGNAM-src-$VERSION.tgz
cd $PRGNAM
cp $CWD/zsdcc_r${ZSDCC_REV}_src.tar.gz .

# 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

# Sneaky ways to inject CFLAGS.
SLKCFLAGS+=" -fcommon"
sed -i "s/CC=gcc/CC='gcc $SLKCFLAGS'/" build.sh
export CXX="g++ $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 of the compiler itself works OK, but not of the
# z80 native libraries.
sed -i '/^\$MAKE *-e/aexport MAKEFLAGS="-j1"' build.sh

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

# 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

# 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 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
