#!/bin/bash

# Slackware build script for xidel

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

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

# 20221229 bkw: BUILD=2, sbopkglint

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

PRGNAM=xidel
VERSION=${VERSION:-0.9.8}
BUILD=${BUILD:-2}
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}

# No CFLAGS (Pascal, not C), no need for LIBDIRSUFFIX. Upstream
# uses fpc's -O3 option, for whatever that's worth.

set -e

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

# Fairly weird build process, but simple enough. -s means strip. Tests
# require internet access, so disabled by default.
[ "${TESTS:-no}" = "yes" ] && T="-t"

cd programs/internet/$PRGNAM
./build.sh -s $T
mkdir -p $PKG/usr/bin
cp -a $PRGNAM $PKG/usr/bin

mkdir -p $PKG/usr/man/man1
gzip -9c < meta/$PRGNAM.1 > $PKG/usr/man/man1/$PRGNAM.1.gz

# Uhhh. Empty file, useless... though I like the name :)
rm -f examples/site-generator/buildtemp/tardis

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a README* COPYING changelog examples $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
