#!/bin/bash

# Slackware build script for rtorrent
# Written by Brian Kysela 2009-11-04
# Updated to 0.8.6 by Brian Kysela 2010-02-16
# Updated to include rtorrent.rc 2010-04-20
# Updated to 0.8.7 by Brian Kysela 2010-10-27
# Patched for new ncurses in 13.37 by rworkman
# Updated to 0.8.8 by Brian Kysela 2011-05-09
# Updated to 0.8.9 by Brian Kysela 2011-06-22
# Updated to 0.9.0 by Brian Kysela 2011-12-12

# Updated 20240917 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.

# 20260908 bkw: updated for v0.16.22, merged with libtorrent.
# 20260727 bkw: updated for v0.16.18 (d'oh!)
# 20260719 bkw: updated for v0.16.8.
# 20260111 bkw: updated for v0.16.6.
# 20251216 bkw: updated for v0.16.5.
# 20250902 bkw: updated for v0.15.6.
# 20250124 bkw: updated for v0.15.1
# 20241124 bkw: updated for v0.10.0.
# 20240917 bkw:
# - new maintainer.
# - updated for v0.9.8.

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

PRGNAM=rtorrent
VERSION=${VERSION:-0.16.22}
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

WITH=WITHOUT
if [ -x /usr/bin/xmlrpc-c-config ]; then
  xmlrpcc="--with-xmlrpc-c"
  WITH=WITH
fi

set -e

LIBNAM=libtorrent
TOPDIR=$TMP/$PRGNAM-build
PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
PKGLIBDOC=$PKGDOC/$LIBNAM
ULIB=/usr/lib$LIBDIRSUFFIX
PKGULIB=$PKG$ULIB

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
rm -rf $TOPDIR
mkdir -p $TOPDIR
cd $TOPDIR
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz --no-same-owner
tar xvf $CWD/$LIBNAM-$VERSION.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 {} + \) \)

# 20260908 bkw: libtorrent used to be a separate build. Nothing but
# rtorrent ever used it, and we also have two versions of the
# similarly-named (but utterly different) libtorrent-rasterbar
# (one of which is called "libtorrent2"). De-confused the issue
# by merging libtorrent and rtorrent.
# The libtorrent installed with this package does NOT conflict
# with libtorrent-rasterbar (or libtorrent2).

# First, the library. Doesn't ship a configure script, generate it.
cd $TOPDIR/$LIBNAM-$VERSION
autoreconf -if

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --libdir=$ULIB \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --with-posix-fallocate \
  --disable-debug \
  --build=$ARCH-slackware-linux

make
make install-strip DESTDIR=$PKG
rm -f $PKG/usr/lib*/*.la

mkdir -p $PKGLIBDOC
install -m 0644 AUTHORS COPYING README* $PKGLIBDOC

# Now, the application.
cd $TOPDIR/$PRGNAM-$VERSION

SLKCFLAGS+=" -I$PKG/usr/include"

# Fugly options to make it link with the lib while it's still
# in $PKG.
PKG_CONFIG_PATH="$PKGULIB/pkgconfig:$PKG_CONFIG_PATH" \
LDFLAGS="-lpthread -L$PKGULIB" \
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --libdir=$ULIB \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --mandir=/usr/man \
  $xmlrpcc \
  --disable-debug \
  --build=$ARCH-slackware-linux

make
make install-strip DESTDIR=$PKG

# 20250124 bkw: man page from debian.
# 20260909 bkw: updated man page for 0.16.22 and sent it to the
# debian maintainers for rtorrent.
mkdir -p $PKG/usr/man/man1
gzip -9c < $CWD/$PRGNAM.1 > $PKG/usr/man/man1/$PRGNAM.1.gz

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

mkdir -p $PKG/install
sed "s,@WITH@,$WITH," < $CWD/slack-desc > $PKG/install/slack-desc

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