#!/bin/bash

# Slackware build script for bmake

# Copyright 2015 John Vogel Corning, NY USA
# 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.

# Now maintained by B. Watson <yalhcru@gmail.com>

# 20210829 bkw: update for 20210803
# - there is a 20210808 release (5 days newer) but it fails its
#   own self-tests (specifically job-output-null).
# 20201106 bkw: update for 20201010
# 20200306 bkw: update for 20200212
# 20191211 bkw: update for 20181221
# 20170214 bkw:
# - update for v20170201
# - i486 => i586
# - clear MAKEFLAGS from environment. setting it doesn't actually
#   give you a parallel build, but it does break the 'make test'
#   portion of the bmake build.

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

PRGNAM=bmake
VERSION=${VERSION:-20210803}
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-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
mv $PRGNAM $PRGNAM-$VERSION
cd $PRGNAM-$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 {} \+

# Fix an implicit declaration warning that *probably* doesn't hurt anything,
# but might cause problems someday.
patch -p1 < $CWD/compilefix.diff

DOCS="ChangeLog README LICENSE"

# The current build system does not respect a large portion of
# the standard configure flags, but leave them in the hopes that
# someday it will.
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --mandir=/usr/man \
  --docdir=/usr/doc/$PRGNAM-$VERSION \
  --with-default-sys-path=/usr/share/mk-$PRGNAM \
  --build=$ARCH-slackware-linux

# The usual MAKEFLAGS=-j8 or such, will break the build. bmake interprets
# MAKEFLAGS differently from GNU make. Plus, the main part of the build
# (actually compiling bmake) isn't done by make, so MAKEFLAGS would have
# no effect. Also, the build interprets TAG as selecting a test scenario.
env \
  -u MAKEFLAGS \
  -u TAG \
  make install DESTDIR=$PKG MANTARGET=man MAKESYSPATH=/usr/share/mk-$PRGNAM

# Override the build system's installation locations.
mv $PKG/usr/share/man $PKG/usr
mv $PKG/usr/share/mk $PKG/usr/share/mk-$PRGNAM

# Fix permissions of installed files.
find -L $PKG  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

# Binary is already stripped. Only have one man page.
# Oh FFS, 20201010 ignores MANTARGET, we get /usr/man/cat1.
rm -rf $PKG/usr/man/*
mkdir -p $PKG/usr/man/man1
gzip -9c < $PRGNAM.1 > $PKG/usr/man/man1/$PRGNAM.1.gz

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
head -n70 main.c > LICENSE
cp -a $DOCS $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$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
