#!/bin/bash

# Slackware build script for mdcat-bin

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

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

# This is a binary repack because the source is in Rust, and won't
# build with the Rust that ships with Slackware 15.0. It would be
# possible for someone to use rustup and deal with "cargo vendor"
# stuff to create build-from-source mdcat SlackBuild, but that
# someone will not be me. If you are that someone, I've named this one
# "mdcat-bin" so you can name yours "mdcat".

# Despite the "musl" in the tarball filename, this works fine on
# Slackware, since it's 100% statically linked. Upstream probably used
# musl because glibc's not capable of creating a truly static binary
# that uses DNS...

# Many thanks to chrisdent and pwshdooduk of libera.chat's #powershell,
# for making sense of the powershell completions and just generally
# being friendly folks.

# 20260712 bkw:
# - updated for v2.5.0.
# Newer releases exist, but they require a newer glibc than Slackware
# 15.0 has. I don't use this often enough to jump through hoops to get
# a new version working, so this is it for 15.0.

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

PRGNAM=mdcat-bin
SRCNAM=mdcat
VERSION=${VERSION:-2.5.0}
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}

# 20260712 bkw: newer releases dropped the completions/ dir, so use the
# one from the old release.
if [ "$ARCH" = "x86_64" ]; then
  SRCDIR="$SRCNAM-$VERSION-$ARCH-unknown-linux-musl"
  TARBALL="$SRCDIR.tar.gz"
else
  cat <<EOF

*** ARCH "$ARCH" is not supported (only x86_64 is supported, sorry).

EOF
  exit 1
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
# use e.g. /tmp/SBo/mdcat-bin/<blah>/, for easy cleanup.
rm -rf $PRGNAM
mkdir -p $PRGNAM
cd $PRGNAM
tar xvf $CWD/$TARBALL
cd $SRCDIR
chown -R root:root .
# no find|chmod, but we install everything with explicit permissions
# or redirection, below.

PKGBIN=$PKG/usr/bin
PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
PKGMAN1=$PKG/usr/man/man1
PKGBASHC=$PKG/usr/share/bash-completion/completions
PKGZSHC=$PKG/usr/share/zsh/site-functions
PKGFISHC=$PKG/usr/share/fish/completions

# powershell doesn't really have a completions directory, so I picked
# something plausible-looking. The README tells how to edit your
# profile to add these.
PKGPSHC="$PKG/usr/share/$PRGNAM/powershell-completions"

mkdir -p $PKGBIN $PKGDOC $PKGMAN1 $PKGBASHC $PKGZSHC $PKGFISHC $PKGPSHC

install -m0755 $SRCNAM $PKGBIN
ln -s $SRCNAM $PKGBIN/mdless

INST="install -oroot -groot -m0644"
$INST LICENSE *.md $PKGDOC
gzip -9c < $SRCNAM.1 > $PKGMAN1/$SRCNAM.1.gz
ln -s $SRCNAM.1.gz $PKGMAN1/mdless.1.gz

# 20260712 bkw: upstream no longer ships completions/*. instead, we
# generate them. this is more complex than it should be, to avoid
# running mdcat as root.
mkdir completions
chown nobody:nobody completions
cd completions
su nobody -s /bin/sh -c "$PKGBIN/mdcat --completions bash" > mdcat.bash
su nobody -s /bin/sh -c "$PKGBIN/mdless --completions bash" > mdless.bash
su nobody -s /bin/sh -c "$PKGBIN/mdcat --completions zsh" > _mdcat
su nobody -s /bin/sh -c "$PKGBIN/mdless --completions zsh" > _mdless
su nobody -s /bin/sh -c "$PKGBIN/mdcat --completions fish" > mdcat.fish
su nobody -s /bin/sh -c "$PKGBIN/mdless --completions fish" > mdless.fish
su nobody -s /bin/sh -c "$PKGBIN/mdcat --completions powershell" > _mdcat.ps1
su nobody -s /bin/sh -c "$PKGBIN/mdless --completions powershell" > _mdless.ps1

$INST mdcat.bash $PKGBASHC/mdcat
$INST mdless.bash $PKGBASHC/mdless
$INST _mdcat _mdless $PKGZSHC
$INST *.fish $PKGFISHC
$INST *.ps1 $PKGPSHC

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
