#!/bin/bash

# Slackware build script for yt-dlp

# Written by B. Watson (urchlay@slackware.uk)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# Note: I honestly prefer the standalone executable version of this,
# which I keep in ~/bin. It allows me to run "yt-dlp -U" as my user to
# get the latest version. However, since it's listed as a dependency
# for clipgrab, haruna, xdman, ytfzf, and an optional dependency for
# pipe-viewer, it has to exist on SBo.

# 20240313 bkw:
# - updated for v2024.03.10
# - build method changed (python3 -m build, rather than setup.py)
# - upstream managed to lose the man page, so include the man page
#   from the previous version (should be OK for now at least).
# - switch to github archive/ URL, since pythonhosted.org doesn't have
#   this version (yet?). filename still has the version number in it.

# 20240217 bkw: switched to pythonhosted.org source tarball, thanks
# to Jeremy Hansen for finding this. Filename now has the version
# number in it, so no more caching problems for the download. Build
# had to change slightly as there's no more CONTRIBUTING file.
# 20240216 bkw: updated for v2023.12.30
# 20231219 bkw: updated for v2023.11.16
# 20231031 bkw: updated for v2023.10.13
# 20230904 bkw: BUILD=2, silence complaints from 'pip3 check'
# 20230711 bkw: updated for v2023.07.06
# 20230623 bkw: updated for v2023.06.22, added version check
# 20230402 bkw: updated for v2023.03.04
# 20230222 bkw: updated for v2023.02.17
# 20230104 bkw: updated for v2023.01.02

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

# Included in Slint by Didier Spaier didieratslintdotfr

PRGNAM=yt-dlp
VERSION=${VERSION:-2024.04.09}
BUILD=${BUILD:-1}
TAG=slint
PKGTYPE=${PKGTYPE:-txz}

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=$CWD
PKG=$TMP/package-$PRGNAM
OUTPUT=$CWD

# no need for CFLAGS or LIBDIRSUFFIX. actually this would be noarch,
# except for the lib vs. lib64 python problem.

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

python3 -m build --wheel --no-isolation
python3 -m installer --destdir "$PKG" dist/*.whl

mkdir -p $PKG/usr/man/man1
gzip -9c < $CWD/$PRGNAM.1 > $PKG/usr/man/man1/$PRGNAM.1.gz

# AUTHORS is 0 bytes in 2022.01.21 (still is, in 2023.03.04).
PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
cp -a LICENSE *.md $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$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
