#!/bin/bash
#
# Slackware build script for xchm
# Written by Michael Gehring

# Modified and now maintained by B. Watson <urchlay@slackware.uk>.
# Original version had no license. Modified version is licensed under
# the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# 20260706 bkw: taking this over because we need a lightweight
# viewer for CHM files in our repo.
# - take over maintenance.
# - update for v1.39.
# - force use of correct wxWidgets version, since we have 3 or 4 of
#   them that each overwrite /usr/bin/wx-config.
# - actually use SLKCFLAGS.

# 20220209 bkw: Modified by SlackBuilds.org, fix the build when wxGTK3
# was installed after wxPython.

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

PRGNAM=xchm
SRCNAM=xCHM
VERSION=${VERSION:-1.39}
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 -fno-strict-aliasing"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686 -fno-strict-aliasing"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC -fno-strict-aliasing"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

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 . ! -type l    -a \
  \(     -perm /111 -a ! -perm 755 -a -exec chmod -f 755 {} + \) -o \
  \(   ! -perm /111 -a ! -perm 644 -a -exec chmod -f 644 {} + \)

./bootstrap

CFLAGS=$SLKCFLAGS \
CXXFLAGS=$SLKCFLAGS \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --mandir=/usr/man \
  --with-wx-config=/usr/lib$LIBDIRSUFFIX/wx/config/gtk3-unicode-3.2 \
  --build=$ARCH-slackware-linux \
  --host=$ARCH-slackware-linux

make
make install-strip DESTDIR=$PKG
gzip -9 $PKG/usr/man/man*/*

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
cp -a AUTHORS ChangeLog COPYING README* README.xmlrpc $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

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
