#!/bin/bash

# Slackware build script for gohu-font

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

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

# 20170328 bkw: anyone looking to convert BDF fonts for use with the
# console might be interested in this.

# 20230307 bkw: BUILD=2, add douninst.sh

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

PRGNAM=gohu-font
VERSION=${VERSION:-2.1}
BUILD=${BUILD:-2}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

ARCH=noarch

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}

SRCNAM=${PRGNAM/-/}

set -e

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

# bdftopcf is in a full install of core Slackware.
mkdir -p $PKG/usr/share/fonts/misc
for i in *.bdf; do
  bdftopcf $i | \
    gzip -9 > $PKG/usr/share/fonts/misc/$( basename $i .bdf ).pcf.gz
done

# I *really* want to use this font on the console. It's purdy.
# bdf2psf is a PITA to use. This works, and results in usable console
# fonts that work OK in the framebuffer console with either UTF-8 or
# Latin-1 encoding.
if which bdf2psf 2>/dev/null; then
  echo "=== found bdf2psf, creating console fonts"

  PSFDIR=$PKG/usr/share/kbd/consolefonts
  mkdir -p $PSFDIR

  SETDIR=/usr/share/bdf2psf

  # Admins, please don't "fix" the indentation of this next stanza, or
  # you'll add embedded spaces, which will break everything.
  SETS="\
$SETDIR/ascii.set+\
$SETDIR/linux.set+\
$SETDIR/fontsets/Lat2.256+\
$SETDIR/fontsets/Uni1.512+\
$SETDIR/useful.set\
"

# Trying to squeeze these in actually hurts more than it helps IMO.
#$SETDIR/fontsets/Lat7.256+\
#$SETDIR/fontsets/Lat15.256+\

  # $BDF.log will be full of "no glyph defined" warnings.
  for BDF in *.bdf; do
    bdf2psf --fb $BDF $SETDIR/standard.equivalents $SETS 512 \
      $PSFDIR/$( basename $BDF .bdf ).psfu \
		2>$BDF.log
  done

  gzip -9 $PSFDIR/*.psfu
  FONTTYPES="X and console fonts"
else
  echo "=== no bdf2psf, skipping console fonts"
  FONTTYPES="X fonts only"
fi

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a COPYING-LICENSE README.md $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
sed "s,@FONTTYPES@,$FONTTYPES," $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh
cat $CWD/douninst.sh > $PKG/install/douninst.sh

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