%define pypi_name	youtube_dl

%if 0%{?mageia}
%define dist mgr8
%endif

%if 0%{?fedora}
%define has_pandoc 1
%else
%if "%{_vendor}" != "openEuler"
%define has_md2man 1
%endif
%endif

%if 0%{!?mkrel:1}
%define mkrel(c)  %{1}%{?dist}
%endif

Name:		youtube-dl
Version:	2025.03.11
Release:	%mkrel 1.1
Summary:	Small command-line program to download videos from YouTube
License:	Public Domain
Group:		Video/Players
URL:		https://ytdl-org.github.io/youtube-dl/
# Done on the 2023.06.17 after bug https://github.com/ytdl-org/youtube-dl/issues/32314 is fixed
# Memo: git archive --output=./master.tar.gz --format=tar.gz --prefix=ytdl-org-youtube-dl-a96a45b/ HEAD
Source0:	https://github.com/ytdl-org/ytdl-nightly/archive/refs/%{version}.tar.gz#/ytdl-nightly-%{version}.tar.gz
#Source1:	https://yt-dl.org/downloads/%%{version}/%%{name}-%%{version}.tar.gz.sig
BuildArch:	noarch
BuildRequires:  make
BuildRequires:	python3
BuildRequires:	zip
%if 0%{?has_pandoc}
BuildRequires:  pandoc
%else
%if 0%{?has_md2man}
BuildRequires:  go-md2man
%endif
%endif
Requires:	python3
# MGA#18464 - cclive crashes
Obsoletes:	cclive < 0.9.4

%description
Small command-line program to download videos from YouTube.

%if 0%{?python3_sitelib:1}
%package -n	python3-%{name}
Summary:	Python 3 bindings for %{name}
Group:		Development/Python
BuildArch:	noarch
%{?python_provide:%python_provide python3-%{name}}

%description -n	python3-%{name}
This is the Python 3 bindings for %{name}.
%endif

%prep
%autosetup -n ytdl-nightly-%{version}

%build

%if 0%{?has_pandoc}
# Make the generated man page correct in pandoc 3+
sed -i -e 's,head -c1` = "2",grep -o "^[0-9]\\+"` -ge 2,' Makefile
%else
%if 0%{?has_md2man}
# Generate the man page with go-md2man when pandoc isn't available
sed -i -e 's|pandoc -s -f $(MARKDOWN) -t man youtube-dl.1.temp.md -o youtube-dl.1|go-md2man -in %{name}.1.temp.md -out %{name}.1|g' Makefile
%endif
# Continue to comment out pandoc when it's not generating the man page
sed -i 's|pandoc |#pandoc |g' Makefile
%endif

%if 0%{!?python3:1}
%define python3 /usr/bin/env python3
%endif
%make_build PYTHON="%python3"

sed -i -e 's,#!/usr/bin/env python,#!/usr/bin/python3,' %{pypi_name}/__main__.py %{pypi_name}/YoutubeDL.py

%install

# No pandoc available on mageia - Faking man page
touch %{name}.1
%make_install PREFIX="%{_prefix}" MANDIR="%{_mandir}" PYTHON="%python3"
%if 0%{?python3_sitelib:1}
mkdir -p %{buildroot}%{python3_sitelib}
cp -rf %{pypi_name} %{buildroot}%{python3_sitelib}
%endif

# force the youtube-dl binary to use python 3 environment by default
sed -i -e 's,#!/usr/bin/env python,#!/usr/bin/python3,' %{buildroot}%{_bindir}/%{name}

%files
%license LICENSE
%if 0%{?has_pandoc}
%doc README.txt
%else
%doc README.md
%endif
%doc docs/supportedsites.md
%attr(0755,root,root) %{_bindir}/%{name}
%{_sysconfdir}/bash_completion.d/
%{_sysconfdir}/fish/completions/youtube-dl.fish
%{_datadir}/zsh/site-functions/_youtube-dl
%{_mandir}/man1/%{name}.1.*

%if 0%{?python3_sitelib:1}
%files -n python3-%{name}
%{python3_sitelib}/*
%endif