#!/bin/bash

# Slackware build script for chkxex

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

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

# Unfortunately, there's almost no documentation from upstream at all.
# Even in the source, there are almost no comments. I wrote a man
# page, but had to leave out the COPYRIGHT section since there's no
# copyright/licensing info anywhere.

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

PRGNAM=chkxex
SRCNAM=chkXEX
VERSION=${VERSION:-20230607_e5c1564}
COMMIT=${COMMIT:-e5c156472f9c6b2e695f9ee4be3e28b5b0945f07}
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}

# 20241102 bkw: pascal, not C, no need for CFLAGS.

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCNAM-$COMMIT
tar xvf $CWD/$SRCNAM-$COMMIT.tar.gz --exclude '*.exe'
cd $SRCNAM-$COMMIT
chown -R root:root .
chmod 644 *

# 20241102 bkw: I cannot abide programs that spit out error messages
# and then exit with "success" status. This fixes it... though it
# doesn't fix the other annoyance: error messages should go to stderr,
# but don't.
sed -i 's,exit;,ExitCode:=1;&,' $PRGNAM.pas

# 20241102 bkw: no 'make install', so manually create the dirs.
PKGBIN=$PKG/usr/bin
PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
PKGMAN1=$PKG/usr/man/man1
mkdir -p $PKGBIN $PKGDOC $PKGMAN1

# 20241102 bkw: Compile command comes from comments at the top of the
# source. Binary is already stripped.
fpc -Mdelphi -vh -O3 -o$PKGBIN/$PRGNAM chkxex.pas

# 20241102 bkw: Stoopid compiler leaves this turd behind.
rm -f $PKGBIN/*.o

# 20241102 bkw: man page written for this SlackBuild.
gzip -9c < $CWD/$PRGNAM.1 > $PKGMAN1/$PRGNAM.1.gz

cp -a README.md $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