# Copyright 2025 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8

MY_P=${P/_/-}
MODULES_OPTIONAL_IUSE="+modules"

inherit linux-mod-r1 fcaps

DESCRIPTION="Bypasses Deep Packet Inspection (DPI) systems that rely on SNI"
HOMEPAGE="https://github.com/Waujito/youtubeUnblock"
SRC_URI="https://github.com/Waujito/${PN}/archive/refs/tags/v${PV/_/-}.tar.gz -> ${MY_P}.tar.gz"

LICENSE="GPL-3.0"
SLOT="0"
KEYWORDS="~amd64"

DEPEND=""
RDEPEND="${DEPEND}"
BDEPEND=""

IUSE="module-src uspace"

#CONFIG_CHECK="CONFIG_NETFILTER_NETLINK_QUEUE CONFIG_NFT_QUEUE CONFIG_NF_CONNTRACK"

S=${WORKDIR}/${MY_P}

src_prepare() {
        default

        sed -i "s@^KERNEL_BUILDER_MAKEDIR:=.*@KERNEL_BUILDER_MAKEDIR:=/lib/modules/${KV_FULL}/build@" kmake.mk || die "Fail to set desired kernel version in the Makefile"
        sed -i -e "/\[Service\]/a DynamicUser=true" \
        -e "/\[Service\]/a AmbientCapabilities=CAP_NET_RAW CAP_NET_ADMIN" \
        -e "/\[Service\]/a CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_RAW" \
        -e "/\[Service\]/a RestrictNamespaces=yes" \
        -e "/\[Service\]/a ProtectSystem=strict" \
        -e "/\[Service\]/a ProtectHome=true" \
        -e "/\[Service\]/a PrivateTmp=true" \
        youtubeUnblock.service
}

src_compile() {
		use uspace && emake PREFIX="${EPREFIX}/usr"
		
        local modlist=( kyoutubeUnblock=net:::kmake )
        local modargs=(
                KERNELDIR=${KV_OUT_DIR}
        )
        use modules && linux-mod-r1_src_compile
}

src_install() {
		use uspace && emake \
                PREFIX="${EPREFIX}/usr" \
                DESTDIR="${D}" \
                MANDIR="/usr/share/man" \
                install

        use modules && linux-mod-r1_src_install
        use module-src && emake DESTDIR="${D}" PREFIX="${EPREFIX}/usr" -C src dkms-install
}

pkg_postinst() {
		if use uspace; then
			fcaps cap_net_raw,cap_net_admin "${EROOT}"/usr/bin/"${PN}"
		fi
		
        if use module-src && ! use modules; then
                einfo
                einfo "You have enabled the module-src USE flag without the modules USE"
                einfo "flag. This means that sources are installed to"
                einfo "${ROOT}/usr/src/kyoutubeUnblock instead of having the"
                einfo "kernel module compiled. You will need to compile the module"
                einfo "yourself. Most likely, you don't want this USE flag, and should"
                einfo "rather use USE=modules"
                einfo
        fi

        if use modules; then
                linux-mod-r1_pkg_postinst
                local old new
                if [[ $(uname -r) != "${KV_FULL}" ]]; then
                        ewarn
                        ewarn "You have just built kyoutubeUnblock for kernel ${KV_FULL}, yet the currently running"
                        ewarn "kernel is $(uname -r). If you intend to use this kyoutubeUnblock module on the currently"
                        ewarn "running machine, you will first need to reboot it into the kernel ${KV_FULL}, for"
                        ewarn "which this module was built."
                        ewarn
                elif [[ -f /sys/module/kyoutubeUnblock/version ]] && \
                     old="$(< /sys/module/kyoutubeUnblock/version)" && \
                     new="$(modinfo -F version "${ROOT}/lib/modules/${KV_FULL}/net/kyoutubeUnblock.ko" 2>/dev/null)" && \
                     [[ $old != "$new" ]]; then
                        ewarn
                        ewarn "You appear to have just upgraded kyoutubeUnblock from version v$old to v$new."
                        ewarn "However, the old version is still running on your system. In order to use the"
                        ewarn "new version, you will need to remove the old module and load the new one. As"
                        ewarn "root, you can accomplish this with the following commands:"
                        ewarn
                        ewarn "    # rmmod kyoutubeUnblock"
                        ewarn "    # modprobe kyoutubeUnblock"
                        ewarn
                fi
        fi
}
