#!/bin/bash

# Slackware build script for minimal-basic

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

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

# Notes:

# Abandon all hope, ye who attempt to build this on non-x86_64
# platforms. The ecma55 binary gets linked with a bunch of x86_64
# object files in a directory named AMD64 (which ought to be a clue),
# which are built from x86_64 assembly source (not C). Nothing you do
# (hacking up the Makefile, etc) will let you link x86_64 objects with
# non-x86_64 ones. Seriously. Do not email me asking for help with it.

# Building on multilib should be possible, but I haven't tested it.
# If you do, you can only build an x86_64 binary. See above.

# There's a Makefile.clang, but it won't work on Slackware 15.0's
# clang 13.x because it's too *new*. That's a rare situation in
# Slackware...

# There's also a Makefile.tcc, which actually does work with the
# tcc in our repo (20220221_308d8d1), but I see no advantage to
# building with tcc. And no, using tcc won't get around the "only
# builds on x86_64" rule, either.

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

PRGNAM=minimal-basic
SRCNAM=MinimalBASIC
VERSION=${VERSION:-2.40}
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" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
else
  cat <<EOF

**************************************************
* Unsupported ARCH: $ARCH
* MinimalBASIC only builds on x86_64, by design.
**************************************************

EOF
  exit 1
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCNAM-$VERSION
tar xvf $CWD/$SRCNAM-$VERSION.tar.xz
cd $SRCNAM-$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 {} \+

runmake() {
  make -f Makefile.gcc \
    OPT="$SLKCFLAGS" \
    PIE=1 \
    LTO=1 \
    DESTDIR=$PKG \
    BINDIR=/usr/bin \
    MANDIR=/usr/man/man1 \
    "$@"
}

runmake
strip ecma55
runmake install

# Sample code, including my own ports of Hamurabi and Bagels. They
# came from http://vintage-basic.net/games.html (and before that, from
# the book "101 BASIC Computer Games" by David Ahl, and before Ahl
# collected them in his book, other people wrote them, but the mists
# of time have obscured the details).

# I modified them slightly to make them ECMA-55 compatible. They also
# work in bas55, and may show up in a future release of it.

# PI.BAS is my own BASIC port of pi.py from https://github.com/MrBlaise/learnpython/

EXTRA=$PKG/usr/share/$PRGNAM/examples
mkdir -p $EXTRA
cp -a CSCLASSICS $PKG/usr/share/$PRGNAM
install -m0644 -o root -g root $CWD/examples/*.BAS $EXTRA

# *Lots* of documentation.
PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
cp -a COPYING ChangeLog CC0-1.0-Universal NEWS \
      README TESTING THANKS TODO GNU_FDL \
      *.pdf *.txt *.TXT *.dot BOOK/*.pdf \
      $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