#!/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.

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

PRGNAM=mdcat-bin
SRCNAM=mdcat
VERSION=${VERSION:-2.0.3}
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" = "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 -m0644"
$INST LICENSE *.md $PKGDOC
gzip -9c < $SRCNAM.1 > $PKGMAN1/$SRCNAM.1.gz
ln -s $SRCNAM.1.gz $PKGMAN1/mdless.1.gz

cd completions
$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
