#!/bin/sh

PRGNAM=yt-dlp
VERSION=2026.03.17
BUILD=1
ARCH=all

HOMEPAGE="https://github.com/yt-dlp/yt-dlp"
SOURCE="${PRGNAM}-${VERSION}.tar.gz"
DOWNLOAD="https://github.com/yt-dlp/yt-dlp/releases/download/${VERSION}/${PRGNAM}.tar.gz"
SHA256SUM="c74f56fc88a7f9b7c995646f30538974a03dd7609f55257b20594fdeeb1e13df"

MAINTAINER="Gernot Walzl"
EMAIL="gernot.walzl@gmx.at"
DATE="2026-03-22"

DESC="downloader of videos from YouTube and other sites
 yt-dlp is a youtube-dl fork based on the now inactive youtube-dlc.
 The main focus of this project is adding new features and patches
 while also keeping up to date with the original project."


# set initial variables
CWD=$(pwd)
TMP=${TMP:-/tmp}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

# exit on error
set -e

# download the source if needed
if [ ! -f "$CWD/$SOURCE" ]; then
  wget -O "$CWD/$SOURCE" "$DOWNLOAD" || exit 1
fi

# check for correct source file
if [ "$(sha256sum "$CWD/$SOURCE" | cut -f 1 -d ' ')" != "$SHA256SUM" ]; then
  exit 1
fi

# extract
cd "$TMP" || exit 1
tar xvf "$CWD/$SOURCE"
cd "$PRGNAM" || exit 1

# fix permissions
chown -R root:root .
chmod -R u+w,go+r-w,a-s .

# install
rm -rf "$PKG"
#python3 setup.py install --root="$PKG"
python3 -m build --wheel
python3 -m installer --destdir "$PKG" dist/*.whl

# fix dirs
mv "$PKG/usr/local/"* "$PKG/usr"
rmdir "$PKG/usr/local"
mv "$PKG/usr/lib/python3."* "$PKG/usr/lib/python3"
mv "$PKG/usr/share/doc/yt_dlp" "$PKG/usr/share/doc/yt-dlp"
find "$PKG/usr/lib/python3/dist-packages" -type f -name '*.pyc' -delete
find "$PKG/usr/lib/python3/dist-packages" -type d -name '__pycache__' -delete

# compress man pages
if [ -d "$PKG/usr/share/man" ]; then
  find "$PKG/usr/share/man" -type f -name "*.?" -exec gzip -n9 {} \;
fi

# copy build script
cat "$CWD/$0" > "$PKG/usr/share/doc/$PRGNAM/$PRGNAM.DebBuild"

# changelog
cat > "$PKG/usr/share/doc/$PRGNAM/changelog.Debian" <<EOF
$PRGNAM ($VERSION-$BUILD) unstable; urgency=medium

  * Created a Debian package.

 -- $MAINTAINER <$EMAIL>  $(date -d$DATE -R)
EOF
gzip -n9 "$PKG/usr/share/doc/$PRGNAM/changelog.Debian"

# copyright
cat > "$PKG/usr/share/doc/$PRGNAM/copyright" <<EOF
Files: *
Copyright: none
License: public-domain
EOF

# DEBIAN/control
mkdir -p "$PKG/DEBIAN"
cat > "$PKG/DEBIAN/control" <<EOF
Package: $PRGNAM
Source: $SOURCE
Version: $VERSION-$BUILD
Section: web
Priority: optional
Architecture: $ARCH
Depends: python3, ffmpeg
Installed-Size: $(du -s "$PKG" | cut -f1)
Maintainer: $MAINTAINER <$EMAIL>
Description: $DESC
Homepage: $HOMEPAGE
EOF

# DEBIAN/postinst
cat > "$PKG/DEBIAN/postinst" <<EOF
#!/bin/sh
set -e
if which py3compile >/dev/null 2>&1; then
  py3compile -p yt-dlp
fi
EOF
chmod 0755 "$PKG/DEBIAN/postinst"

# DEBIAN/prerm
cat > "$PKG/DEBIAN/prerm" <<EOF
#!/bin/sh
set -e
if which py3clean >/dev/null 2>&1; then
  py3clean -p yt-dlp
fi
EOF
chmod 0755 "$PKG/DEBIAN/prerm"

# DEBIAN/md5sums
cd "$PKG"
find . -type d -name 'DEBIAN' -prune -o -type f -printf '%P\0' \
  | sort -z | xargs -0 md5sum \
  > "$PKG/DEBIAN/md5sums"

# build a deb
cd "$TMP"
fakeroot dpkg-deb -b "$PKG" "$OUTPUT"

# clean
cd "$CWD"
rm -rf "$PKG"
rm -rf "$TMP/$PRGNAM"
