#!/bin/bash

# Slackware build script for xosd
# Written by Jick Nan.
# Formerly maintained by Robby Workman.
# 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.

# 20260825 bkw:
# - take over maintenance.
# - update for v2.2.15, from 2022 (2.2.14 was from 2004).
# - add WTFPL license.
# - clean up /usr/doc.
# - enable "old" xmms plugin. the new one can't be built because
#   it needs an ancient version of gdk-pixbuf (Slackware's is
#   too new).

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

PRGNAM=xosd
VERSION=${VERSION:-2.2.15}
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"
  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
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/${PRGNAM}_$VERSION.orig.tar.gz --no-same-owner
cd $PRGNAM-$VERSION
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 {} + \) \)

# 20260825 bkw: patches came from Debian (along with the source),
# see https://packages.debian.org/sid/libxosd2
# The patches mostly fix the man pages, plus a typo in a Makefile,
# plus a compile fail for recent gcc (not needed on 15.0, but it'll
# help with -current). Not included:
# - 20_underquoted_definition.diff: useless since autoreconf fails with
#   "undefined macro: AM_PATH_GDK_PIXBUF".
# - 30_aclocal.diff: -removes gtk1 support, which breaks the xmms plugin.
# - 35_beep_media_player.diff: we already don't have beep-media-player in
#   the repo.
for i in $CWD/patches/*; do
  echo "=== applying $( basename $i )"
  patch -p1 < $i
done

# 20260825 bkw: fix a typo in one of the man pages:
sed -i 's,weather,whether,g' man/xosd_is_onscreen.3

# 20260825 bkw: we don't have beep-media-player, so disable it.
# --disable-new-plugin is redundant, but avoid surprises...

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --disable-beep_media_player_plugin \
  --disable-new-plugin \
  --enable-old-plugin \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --mandir=/usr/man \
  --enable-static=no \
  --build=$ARCH-slackware-linux

make
make install-strip DESTDIR=$PKG

gzip -9 $PKG/usr/man/man?/*

# 20260825 bkw: omit NEWS (0-byte) and INSTALL (generic GNU).
PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
install -m0644 AUTHORS COPYING ChangeLog README TODO $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

rm -f $PKG/usr/lib*/*.la

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
