#!/bin/bash

# Originally written 2015 Didier Spaier Paris, France
# Now maintained by B. Watson (urchlay@slackware.uk)

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

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

PKGNAM=bdf2psf
VERSION=${VERSION:-1.249}
BUILD=${BUILD:-1}
ARCH=noarch

# If the variable PRINT_PACKAGE_NAME is set, then this script will report what
# the name of the created package would be, and then exit. This information
# could be useful to other scripts.
if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PKGNAM-$VERSION-$ARCH-$BUILD.txz"
  exit 0
fi

# Fail on undeclared variables and broken pipes:
set -u -o pipefail

TMP=${TMP:-/tmp}
PKG=$TMP/package-$PKGNAM

rm -rf $PKG
mkdir -p $TMP $PKG

cd $PKG
ar p $CWD/${PKGNAM}_${VERSION}_all.deb data.tar.xz | tar xvfJ - --no-same-owner || exit 1

chown -R root:root .
find . \
  \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \) \
  -exec chmod 755 {} \+ -o \
  \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
  -exec chmod 644 {} \+

mv $PKG/usr/share/man $PKG/usr/man

PKGDOC=$PKG/usr/doc/$PKGNAM-$VERSION
mkdir -p $PKGDOC
mv $PKG/usr/share/doc/$PKGNAM/* $PKGDOC
rm -rf $PKG/usr/share/doc

# Compress manual pages:
find $PKG/usr/man -type f -exec gzip -9 {} \+
for i in $( find $PKG/usr/man -type l ) ; do
  ln -s $( readlink $i ).gz $i.gz
  rm $i
done

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

cd $PKG
/sbin/makepkg -l y -c n $TMP/$PKGNAM-$VERSION-$ARCH-$BUILD.txz
