PKG_NAME = tvheadend
PKG_VERS = 4.3
PKG_EXT = tar.gz
# no official release since v4.2.8 (Jan 12, 2019)
# git hash from July 9th 2026
PKG_GIT_HASH = 0ad5b1a3be94199c90e321fe63c13016b038e275
PKG_DIST_NAME = $(PKG_GIT_HASH).$(PKG_EXT)
PKG_DIST_SITE = https://github.com/tvheadend/tvheadend/archive
PKG_DIST_FILE = $(PKG_NAME)-git$(PKG_GIT_HASH).$(PKG_EXT)
PKG_DIR = $(PKG_NAME)-$(PKG_GIT_HASH)

HOMEPAGE = https://tvheadend.org/
COMMENT  = Tvheadend is a TV streaming server and recorder for Linux, FreeBSD and Android supporting DVB-S, DVB-S2, DVB-C, DVB-T, ATSC, ISDB-T, IPTV, SAT IP and HDHomeRun as input sources. Tvheadend offers HTTP, HTSP and SAT IP streaming.
LICENSE  = GPLv3

# PPC archs except qoriq are not supported
# https://tvheadend.org/issues/5060
# ffmpeg needs C11 -- gcc 4.9 or newer
MIN_GCC_VERSION = 4.9

OPTIONAL_DEPENDS += cross/ffmpeg7
OPTIONAL_DEPENDS += cross/fdk-aac
OPTIONAL_DEPENDS += cross/fontconfig
OPTIONAL_DEPENDS += cross/opus
OPTIONAL_DEPENDS += cross/pcre2
OPTIONAL_DEPENDS += cross/libtheora
OPTIONAL_DEPENDS += cross/libvorbis
OPTIONAL_DEPENDS += cross/libvpx
OPTIONAL_DEPENDS += cross/x264
OPTIONAL_DEPENDS += cross/x265

# WARNING: pngquant will fail when built after cross/zlib (i.e. cross/libpng)
# with prebuilt ffmpeg this is not a problem
# without prebuilt ffmpeg the "DEPENDS += cross/ffmpeg*" must be defined below
DEPENDS  = cross/pngquant

DEPENDS += cross/openssl3 cross/gnutls
DEPENDS += cross/uriparser cross/libiconv
DEPENDS += cross/libdvbcsa cross/dvb-apps
DEPENDS += cross/libhdhomerun

# curl build needs additional configuration for gnutls and libssh2
DEPENDS += cross/curl
export BUILD_CURL_WITH="gnutls libssh2 zstd"

GNU_CONFIGURE = 1
PRE_CONFIGURE_TARGET = tvheadend_pre_configure
POST_INSTALL_TARGET = tvheadend_post_install

CONFIGURE_ARGS  = --python=/usr/bin/python3
CONFIGURE_ARGS += --enable-imagecache --enable-bundle --enable-trace --release
CONFIGURE_ARGS += --enable-imagecache_audit
CONFIGURE_ARGS += --disable-bintray_cache --disable-avahi
CONFIGURE_ARGS += --enable-dvbcsa --enable-pngquant
CONFIGURE_ARGS += --disable-hdhomerun_static --enable-hdhomerun_client

# Enable media libraries incoming from FFMPEG
CONFIGURE_ARGS += --disable-libfdkaac_static --enable-libfdkaac
CONFIGURE_ARGS += --disable-libopus_static --enable-libopus
CONFIGURE_ARGS += --disable-libtheora_static --enable-libtheora
CONFIGURE_ARGS += --disable-libvorbis_static --enable-libvorbis
CONFIGURE_ARGS += --disable-libvpx_static --enable-libvpx
CONFIGURE_ARGS += --disable-libx264_static --enable-libx264
CONFIGURE_ARGS += --disable-libx265_static --enable-libx265
CONFIGURE_ARGS += --disable-ffmpeg_static --enable-libav

# Enable building of new experimental UI
CONFIGURE_ARGS += --enable-vue_build
BUILD_DEPENDS = native/nodejs_22
# Build environment - use native Node.js from spksrc
ENV += PATH=$(WORK_DIR)/../../../native/nodejs_22/work-native/node/bin:$$PATH

# dbus splits its headers between include/ and lib/dbus-1.0/include/
#    - include/dbus-1.0/ (public) must precede
#    - lib/dbus-1.0/include/ (arch-specific dbus-arch-deps.h)
DEPENDS += cross/dbus
CONFIGURE_ARGS += --enable-dbus_1
ADDITIONAL_CFLAGS += -I$(or $(VIDEODRV_STAGING_INSTALL_PREFIX),$(STAGING_INSTALL_PREFIX))/include/dbus-1.0
ADDITIONAL_CFLAGS += -I$(or $(VIDEODRV_STAGING_INSTALL_PREFIX),$(STAGING_INSTALL_PREFIX))/lib/dbus-1.0/include

include ../../mk/spksrc.common.mk

ifeq ($(findstring $(ARCH),$(x64_ARCHS)),$(ARCH))
CONFIGURE_ARGS += --enable-vaapi
CONFIGURE_ARGS += --enable-qsv
endif

# If libraries through FFMPEG_PACKAGE shared libraries unavailable
ifeq ($(wildcard $(FFMPEG_PACKAGE_WORK_DIR)),)
DEPENDS += cross/ffmpeg7
DEPENDS += cross/fdk-aac
DEPENDS += cross/fontconfig
DEPENDS += cross/opus
DEPENDS += cross/pcre2
DEPENDS += cross/libtheora
DEPENDS += cross/libvorbis
DEPENDS += cross/libvpx
DEPENDS += cross/x264
DEPENDS += cross/x265
ifeq ($(findstring $(ARCH),$(x64_ARCHS)),$(ARCH))
DEPENDS += cross/libva
endif
endif

ifeq ($(call version_lt, ${TC_GCC}, 5.0),1)
CONFIGURE_ARGS += --nowerror
ADDITIONAL_CFLAGS += -std=gnu99
endif

include ../../mk/spksrc.cross-cc.mk

.PHONY: tvheadend_pre_configure
tvheadend_pre_configure:
	# Fix so mkbundle finds the pngquant binary
	@$(RUN) sed -i.bak "s|^pngquant_bin = '/usr|pngquant_bin = '$(STAGING_INSTALL_PREFIX)|g" support/mkbundle

.PHONY: tvheadend_post_install
tvheadend_post_install:
	# Enforce python3
	@$(RUN) sed -i 's|env python$$|env python3|g' $(INSTALL_DIR)/$(INSTALL_PREFIX)/bin/tv_meta_tmdb.py
	@$(RUN) sed -i 's|env python$$|env python3|g' $(INSTALL_DIR)/$(INSTALL_PREFIX)/bin/tv_meta_tvdb.py
