#!/bin/bash

# Slackware build script for transmission-remote-gtk

# Copyright 2012-2016  Chris Walker Copperas Cove, TX
# Copyright 2020-2026 B. Watson <urchlay@slackware.uk>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
#   copyright notice, this list of conditions and the following disclaimer
#   in the documentation and/or other materials provided with the
#   distribution.
# * Neither the name of the {company} nor the names of its
#   contributors may be used to endorse or promote products derived from
#   this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "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 COPYRIGHT
# OWNER OR 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.

# 20260110 bkw: update for v1.7.0.
# - new build dep python3-meson-opt.
# - no more GeoIP support (dropped upstream).

# 20230713 bkw: update for v1.6.0.
# - new hard dep libsoup3.
# - no more optional mrss dep (upstream dropped the RSS reader).

# 20220623 bkw: update for v1.5.1.
# 20210926 bkw: BUILD=2, make NOTIFY=no also disable libappindicator.
# 20200526 bkw:
# - take over maintenance
# - update for v1.4.1
# - add NOTIFY=no to build --without-libnotify
# - add README note and slack-desc support for optional deps
# - fix homepage and typo in slack-desc
# - build tweaks
# - get rid of the list of translation languages in README, it's
#   no longer accurate (several more added) and I can't be bothered
#   to keep it up-to-date.

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

PRGNAM=transmission-remote-gtk
VERSION=${VERSION:-1.7.0}
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.tar.?z
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 {} + \)

# 20260111 bkw: use python3-opt-meson, our meson's too old.
PYVER=$(python3 -c 'import sys; print("%d.%d" % sys.version_info[:2])')
export PYTHONPATH=/opt/python$PYVER/site-packages

[ "${NOTIFY:-yes}" = "no" ] && NOTIFYOPT="-Dlibappindicator=disabled"

# 20260110 bkw: upstream forgot to update their appdata file, which serves
# as the ChangeLog.
patch -p1 < $CWD/appdata.diff

sed -i '/-Werror/d' meson.build

mkdir build
cd build
  CFLAGS="$SLKCFLAGS" \
  CXXFLAGS="$SLKCFLAGS" \
  meson .. \
    $NOTIFYOPT \
    --buildtype=release \
    --infodir=/usr/info \
    --libdir=/usr/lib${LIBDIRSUFFIX} \
    --localstatedir=/var \
    --mandir=/usr/man \
    --prefix=/usr \
    --sysconfdir=/etc \
    -Dstrip=true
  "${NINJA:=ninja}"
  DESTDIR=$PKG $NINJA install
cd ..

gzip -9 $PKG/usr/man/man1/$PRGNAM.1

# 20200526 bkw: lots of optional deps, let the slack-desc keep track of them
# 20260110 bkw:
objdump -p $PKG/usr/bin/$PRGNAM | grep NEEDED > ldd.tmp
for i in libappindicator; do
  opts+="$i:"
  grep -q $i ldd.tmp && opts+="yes " || opts+="no "
done

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

mkdir -p $PKG/install
sed "s,@opts@,$opts," $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
