#!/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.

# 20251022 bkw: python EOL note in README, no other changes.
# 20251015 bkw: updated for v2025.10.14.
# 20250929 bkw: updated for v2025.09.26.
# 20250923 bkw: updated for v2025.09.23.
# 20250913 bkw: updated for v2025.09.05.
# 20250824 bkw: updated for v2025.08.22.
# 20250819 bkw: updatef for v2025.08.20.
# 20250804 bkw: updated for v2025.07.21.
# 20250702 bkw: updated for v2025.06.30.
# 20250609 bkw: updated for v2025.06.09.
# 20250522 bkw: updated for v2025.05.22.
# 20250512 bkw: updated for v2025.04.30.
# 20250403 bkw: updated for v2025.03.31.
# 20250328 bkw: updated for v2025.03.27.
# 20250321 bkw: updated for v2025.03.21.
# 20250304 bkw: updated for v2025.02.19.
# 20250131 bkw: updated for v2025.01.26.
# 20250116 bkw: updated for v2025.01.15.
# 20241226 bkw: updated for v2024.12.23.
# 20241209 bkw: updated for v2024.12.06.
# 20241122 bkw: updated for v2024.11.18.
# 20241101 bkw: updated for v2024.10.22.
# 20241004 bkw: updated for v2024.09.27.
# 20240814 bkw: updated for v2024.08.06.
# 20240802 bkw: updated for v2024.08.01.
# 20240711 bkw: updated for v2024.07.09.

# 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)

PRGNAM=yt-dlp
VERSION=${VERSION:-2025.10.14}
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}

# 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 xvf $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 {} +

PYVER=$(python3 -c 'import sys; print("%d.%d" % sys.version_info[:2])')
export PYTHONPATH=/opt/python$PYVER/site-packages

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
