#!/bin/bash

# Slackware build script for xxd-standalone

# Original author: Alexander Verbovetsky, Moscow, Russia.
# Modified and now maintained by B. Watson <urchlay@slackware.uk>.
# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# 20250121 bkw: update for v9.1.1045.
# 20241007 bkw:
# - take over maintenance.
# - update for v9.1.0764.
# - reduce the number of downloads (get rid of redundant files).
# - add our README to the package.

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

PRGNAM=xxd-standalone
VERSION=${VERSION:-9.1.1045}
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"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
else
  SLKCFLAGS="-O2"
fi

set -e

# 20241007 bkw: we don't need a Makefile: upstream's Makefile just runs
# one compile command. We don't even need a source directory, we'll just
# compile the source straight from $CWD and output the binary under $PKG.

rm -rf $PKG
mkdir -p $PKG/usr/{bin,man/man1} $OUTPUT

cd $CWD
${CC:-gcc} -Wl,-s -o $PKG/usr/bin/xxd $SLKCFLAGS -DUNIX xxd.c
gzip -9c < xxd.1 > $PKG/usr/man/man1/xxd.1.gz

# 20241007 bkw: we don't need non-UTF-8 man pages, since our man
# command won't even use them: if you have a non-UTF-8 locale like
# LANG=fr_FR and run "man xxd", man will actually convert the UTF-8
# man page to ISO-8859-1 and display that. You can check, like so:
# $ LANG=fr_FR man -w xxd
# /usr/man//fr.UTF-8/man1/xxd.1.gz

for i in xxd-??.UTF-8.1; do
  lang="${i:4:2}"
  dir=$PKG/usr/man/$lang.UTF-8/man1
  mkdir -p $dir
  gzip -9c < $i > $dir/xxd.1.gz
done

# 20241007 bkw: since there's no documentation, add our README.
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild
cat $CWD/README > $PKG/usr/doc/$PRGNAM-$VERSION/README

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
