#!/bin/bash

# Slackware build script for html2ps

# Copyright 2012-2021  Robby Workman, Tuscaloosa, Alabama, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# 20260523 bkw: BUILD=4
# - update to Debian's latest set of patches, which fix a lot of
#   bugs, including:
# - fix POSIX::tmpnam() error at runtime. makes html2ps actually *work*;
#   it hasn't since 15.0 released!
# - actually include xhtml2ps docs in package.
# - remove baked-in $PKG path in .ps doc.
# - add missing "set -e".
# - fix xhtml2ps view command (gs, not ghostscript).
# Note that the "Check HTML" xhtml2ps option still doesn't work
# due to not having "weblint" in Slackware or SBo.
# This remains Robby's build, but he's too busy to update it
# at the moment.

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

PRGNAM=html2ps
VERSION=${VERSION:-1.0b7}
BUILD=${BUILD:-4}
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}

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/${PRGNAM}_${VERSION}.orig.tar.gz
cd $PRGNAM-$VERSION
chown -R root:root .
find . ! -type l    -a \
  \(     -perm /111 -a ! -perm 755 -a -exec chmod -f 755 {} + \) -o \
  \(   ! -perm /111 -a ! -perm 644 -a -exec chmod -f 644 {} + \)

# 20260523 bkw: For a long time now, this would build but fail
# at run time. These patches from Debian fix it, along with some
# other issues.
for i in $( cat $CWD/patches/series ); do
  patch -p1 < $CWD/patches/$i
done

# 20260523 bkw: We don't have Debian's "see" command, so:
sed -i 's,"see","gv",' contrib/xhtml2ps/xhtml2ps

# 20260523 bkw: One more patch, written by me, to make the window's
# close button work. Without this, xhtml2ps's window disappears when
# you close it, but the process keeps running. Ugh.
patch -p1 < $CWD/close_button.diff

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
PKGXDOC=$PKGDOC/xhtml2ps

# Create the skeleton directory
mkdir -p \
  $PKG/usr/bin \
  $PKG/usr/man/man1 \
  $PKG/usr/man/man5 \
  $PKG/usr/share/html2ps \
  $PKGXDOC

# hardcode the answers to all the questions it wants to ask
patch -p1 < $CWD/noninteractive_install.diff

# noninteractive_install needs these
export PKG VERSION

./install

# Install xhtml2ps stuff
mkdir -p $PKG/usr/share/applications $PKG/usr/share/pixmaps
cp -a contrib/xhtml2ps/xhtml2ps $PKG/usr/bin
cat $CWD/xhtml2ps.desktop > $PKG/usr/share/applications/xhtml2ps.desktop
cat $CWD/xhtml2ps.png > $PKG/usr/share/pixmaps/xhtml2ps.png

cp -a COPYING README html2ps.ps $PKGDOC
cp -a contrib/xhtml2ps/LICENSE $PKGXDOC
cp -a contrib/xhtml2ps/README $PKGXDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

# Now filter out the "$PKG" strings:
sed -i "s,$PKG,,g" $(find $PKG -type f -print)

# 20260523 bkw: do this after $PKG-removal.
gzip -9 $PKG/usr/man/man*/*

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

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