#!/bin/bash

# Slackware build script for yt-dlp-bin

# Originally written by G. Galdini. Modified and now maintained by
# B. Watson <urchlay@slackware.uk>.

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

# 20260402 bkw: updated for v2026.03.17.
# 20260306 bkw: updated for v2026.03.03.
# 20260207 bkw: updated for v2026.02.04.
# 20260131 bkw: updated for v2026.01.29.
# 20251209 bkw: updated for v2025.12.08.
# 20251114 bkw: updated for v2025.11.12.
# 20251022 bkw: updated for v2025.10.22.
# 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.
# - add version check, since the "source" filename never changes.
# 20250901 bkw: DOWNLOAD="UNSUPPORTED", add fake .info file for aarch64.
# 20250825 bkw:
# - take over maintenance.
# - relicense as WTFPL.
# - update for v2025.08.22.
# - add getdocs.sh script to auto-download the documentation from github.
# - use the ELF binary compiled for linux (yt-dlp_linux) rather than
#   the python-wrapped zip file (yt-dlp), to avoid the impending EOL
#   of python-3.9: the ELF binary includes its own python interpreter.
#   probably this can be changed back after the next Slackware release
#   (15.1 or 16.0, whatever it ends up being called). this means no
#   32-bit x86 support, sorry.

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

PRGNAM=yt-dlp-bin
SRCNAM=${PRGNAM%-bin}
VERSION=${VERSION:-2026.03.17}
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

# 20250826 bkw: Should this come before or after the unsupported arch
# error? I think it's safer but more confusing if it's before...
if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

case "$ARCH" in
  x86_64)  SRCNAM=yt-dlp_linux         ;;
  aarch64) SRCNAM=yt-dlp_linux_aarch64 ;;
  *) # get the user's attention.
    cat <<EOF

****************************************************
* Unsupported architecture: ARCH=$ARCH
* Supported ARCH values are "x86_64" and "aarch64".
****************************************************

EOF
    exit 1
esac

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT

# 20250826 bkw: the -s option for install means we can't build an
# aarch64 package on x86_64 or vice versa. stripping the binary
# doesn't make it much smaller, but it doesn't hurt either.
mkdir -p $PKG/usr/bin
install -s -m0755 -oroot -groot $CWD/$SRCNAM $PKG/usr/bin/yt-dlp

# 20250913 bkw: the download filename never changes when the version
# does, so make sure we have the right one.
BINVER="$( $PKG/usr/bin/yt-dlp --version )"
if [ "$BINVER" != "$VERSION" ]; then
  cat <<EOF

************************************************************
* You downloaded the wrong version of "$SRCNAM".
* We expected version "$VERSION", but got "$BINVER".
* If this was intentional, re-run this script with
* 'export VERSION="$BINVER". Otherwise, download
* the correct version of "$SRCNAM".
************************************************************

EOF
  exit 1
fi

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
for i in $CWD/docs/*; do
  cat $i > $PKGDOC/$( basename $i )
done
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
