#!/bin/bash

# Slackware build script for kakoune

# Oritinally written by Leonardo Citrolo.
# Now maintained by B. Watson (urchlay@slackware.uk).

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

# 20250618 bkw: update for v2025.06.03.
# 20240813 bkw: update for v2024.05.18.
# 20231108 bkw: update for v2023.08.05.
# 20230509 bkw:
# - new maintainer.
# - update for v2022.10.31.
# - relicense as WTFPL with permission from original author.
# - override -O3 with $SLKCFLAGS.

# Note: I (B. Watson) took over this build to keep it from
# disappearing from the repo, but if you actually use and enjoy
# kakoune, please take the build from me (ask via email). I can't
# stand to actually use kakoune (tried it, utterly hate it, sorry).

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

PRGNAM=kakoune
VERSION=${VERSION:-2025.06.03}
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

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.bz2
cd $PRGNAM-$VERSION
chown -R root:root .
rm libexec/kak/kak   # dangling symlink
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

# N.B. slightly nonstandard use of DESTDIR: we have to include $PKG
# in docdir and mandir, below.
PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION

sed -i "s,-O3,$SLKCFLAGS," Makefile
make PREFIX=/usr
strip src/kak
make \
  PREFIX=/usr \
  docdir=$PKGDOC \
  mandir=$PKG/usr/man/man1 \
  DESTDIR=$PKG \
  install

# Man page already gzipped, we stripped the binary above. Thought
# about symlinking all the asciidoc from /usr/share/kak to our doc
# dir, but it's intended to be read from within kak, with the :doc
# command.

cp -a CONTRIBUTING UNLICENSE VIMTOKAK $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
