#!/bin/bash
# Slackware build script for w3m

# Copyright 2011 Grigorios Bouzakis <grbzks@imap.cc>
# All rights reserved.
#
# Permission to use, copy, modify, and distribute this software for any purpose
# with or without fee is hereby granted, provided that the above copyright
# notice and this permission notice appear in all copies.
#
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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
# AUTHORS AND COPYRIGHT HOLDERS AND THEIR CONTRIBUTORS 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.

# Now maintained by Dave Woodfall <dave@slackbuilds.org> (2018)
# Much appreciation to the debian maintainers for keeping w3m alive and
# patched https://packages.debian.org/stretch/w3m
#
# 2026/07/27 Ioannis Anagnostakis: switched upstream from the dormant Debian
#   fork (tats/w3m, last touched 2023) to Rene Kita's actively maintained
#   fork at https://sr.ht/~rkta/w3m, (not gemini) which is where w3m development lives
#   now for most distros.

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

PRGNAM=w3m
VERSION=${VERSION:-0.5.6}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

# Upstream ships no release tarballs; the source is the SourceHut archive
# of the v$VERSION tag, which unpacks into w3m-v$VERSION.
SRCNAM=$PRGNAM-v$VERSION

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"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

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

# The SourceHut archive downloads as the bare tag name, v$VERSION.tar.gz.
# Accept $SRCNAM.tar.gz as well, for anyone who renamed it on the way in
# or is building from a rehosted copy.
if [ -f $CWD/v$VERSION.tar.gz ]; then
  SRCTAR=$CWD/v$VERSION.tar.gz
elif [ -f $CWD/$SRCNAM.tar.gz ]; then
  SRCTAR=$CWD/$SRCNAM.tar.gz
else
  echo "$PRGNAM: no source tarball in $CWD" 1>&2
  echo "expected v$VERSION.tar.gz or $SRCNAM.tar.gz" 1>&2
  exit 1
fi

cd $TMP
rm -rf $SRCNAM
tar xvf $SRCTAR
cd $SRCNAM

chown -R root:root .
find . \
 \( -perm 777 -o -perm 775 -o -perm 750 -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 {} \;

# Upstream's po/Makefile.in.in still uses the automake<=1.9 $(mkdir_p)
# variable, which configure never substitutes. The literal string
# "@mkdir_p@" then gets run as a command, every mkdir in install-po fails,
# and the .mo files are silently dropped from the package -- "make install"
# does not return an error, so this is easy to miss. Reported upstream.
sed -i 's|^mkdir_p = @mkdir_p@|mkdir_p = $(MKDIR_P)|' po/Makefile.in.in

# Optional build-time knobs; see README.
[ -n "$NOMOUSE" ]     && mouse="--disable-mouse"
[ -n "$NOMAILER" ]    && mailer="--disable-w3mmailer"
[ -n "$W3M_BROWSER" ] && browser="--with-browser=$W3M_BROWSER"
[ -n "$W3M_EDITOR" ]  && editor="--with-editor=$W3M_EDITOR"

# Inline images need gdk-pixbuf2 + gdk-pixbuf2-xlib (both in Slackware).
# NOIMAGE=yes drops w3mimgdisplay entirely and unlinks us from gdk-pixbuf2.
if [ -n "$NOIMAGE" ]; then
  image="--enable-image=no"
else
  image="--enable-image=x11,fb --with-imagelib=gtk2"
fi

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --libexecdir=/usr/libexec \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --mandir=/usr/man \
  --with-gc \
  --with-ssl \
  --with-termlib="terminfo ncurses" \
  --enable-keymap="w3m" \
  $image \
  $mouse \
  $mailer \
  $browser \
  $editor \
  --build=$ARCH-slackware-linux

make
make install DESTDIR=$PKG

# Compiled-in CONF_DIR, but nothing installs it.
mkdir -p $PKG/etc/w3m

# "make install" ships these helpers unconditionally, even when the
# feature that calls them has been compiled out. Drop the dead ones.
[ -n "$NOIMAGE" ]  && rm -f $PKG/usr/libexec/w3m/xface2xpm
[ -n "$NOMAILER" ] && rm -f $PKG/usr/libexec/w3m/cgi-bin/w3mmail.cgi

# When configure cannot find nkf it bakes a /usr/local/bin/nkf fallback
# into multipart.cgi, which is never a valid path here. Point it at the
# location the SBo nkf package uses, so the script starts working if nkf
# is installed later. No-op when configure found nkf itself.
sed -i 's|/usr/local/bin/nkf|/usr/bin/nkf|g' \
  $PKG/usr/libexec/w3m/cgi-bin/multipart.cgi

find $PKG -print0 | xargs -0 file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

find $PKG/usr/man -type f -exec gzip -9 {} \+

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
# All shipped documentation is UTF-8 since 0.5.4, no recoding needed.
# ChangeLog is deprecated upstream in favour of NEWS and the git log.
cp -a COPYING NEWS README doc doc-de doc-jp $PKG/usr/doc/$PRGNAM-$VERSION

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION/examples
cp -a Bonus $PKG/usr/doc/$PRGNAM-$VERSION/examples
chmod 644 $PKG/usr/doc/$PRGNAM-$VERSION/examples/Bonus/*

cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$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
