#!/usr/bin/make -f
# SPDX-FileCopyrightText: 2024 Canonical Ltd.
# SPDX-License-Identifier: GPL-3.0-only

include /usr/share/dpkg/default.mk

# Tegra hardware platform; e.g. "t234" for NVIDIA Jetson Orin, "t194" for
# NVIDIA Jetson Xavier
JETSON_PLAT := t234

# Jetson Linux release; drop everything after major and minor version, e.g.
# 36.4.0-20240912212859 becomes 36.4
JETSON_REL := $(shell echo $(DEB_VERSION_UPSTREAM) | cut -d. -f-2)

# Jetson Linux deb used as source data – Ubuntu upstream version is the Jetson
# Linux package version of the nvidia-l4t-firmware package
JETSON_DEB := ../nvidia-l4t-firmware_$(JETSON_REL)*.deb

# Jetson Linux deb version aligned with nvidia-l4t-firmware version
JETSON_VERSION = $(shell echo $(JETSON_DEB) | cut -d_ -f2)

# repacked / stripped down orig tarball for the Ubuntu source package
ORIG_TARBALL = ../$(DEB_SOURCE)_$(JETSON_VERSION).orig.tar.xz

# where repacking happens
ORIG_DIR = $(DEB_SOURCE)-$(JETSON_VERSION)

# list+checksums of licenses in last packaged upstream version as a safety net
# to detect changes to the the list of licenses or their contents
LICENSE_CHECKSUM := debian/licensing.MD5SUMS

%:
	dh $@

execute_before_dh_clean:
	rm -f $(LICENSE_CHECKSUM).new

execute_before_dh_auto_install:
	# checksum licenses in current tree
	find \! -path './debian/*' \( \
	    -iname '*LICENS*' -o -iname '*LICENC*' -o -iname '*NOTICE*' \
	    -o -iname '*copyright*' -o -iname '*COPYING*' \) \
	    | sort | xargs md5sum --tag >$(LICENSE_CHECKSUM).new
	# compare to previously reviewed licenses
	diff $(LICENSE_CHECKSUM) $(LICENSE_CHECKSUM).new

# download upstream source data in deb form for the target Jetson series,
# strip these down and generate orig tarball in ../; usage:
#     debian/rules get-orig-source
# to prepare an orig tarball for a new release series:
#     debian/rules get-orig-source JETSON_REL=36.4
.PHONY: get-orig-source get-jetson-deb
get-orig-source: $(ORIG_TARBALL)

# generate the orig tarball in ../ from local Jetson Linux deb; deb is
# downloaded if missing
$(ORIG_TARBALL): $(JETSON_DEB)
	# TODO make this work when run from other directories
	dh_testdir
	dpkg -x $< $(ORIG_DIR)
	# support for Broadcom/Cypress based WiFi/Bluetooth used in previous
	# generation Tegra (Xavier or older) – firmware files, glue scripts,
	# and corresponding licenses
	cd $(ORIG_DIR) && for path in \
	    etc/systemd/nvwifibt-pre.sh \
	    etc/systemd/nvwifibt.sh \
	    etc/systemd/system/nvwifibt.service \
	    lib/firmware/bcm4354.hcd \
	    lib/firmware/brcm/fw_bcmdhd.bin \
	    lib/firmware/brcm/nvram.txt \
	    lib/systemd/system/bluetooth.service.d/nv-bluetooth-service.conf \
	    usr/sbin/brcm_patchram_plus \
	    usr/share/doc/bluez/copyright.compliant.gz \
	    usr/share/doc/nvidia-l4t-firmware/LICENSE.brcm_patchram_plus.gz \
	    usr/share/doc/nvidia-l4t-firmware/LICENSE.cypress_wifibt.gz \
	    ; do \
	    rm -rf $$path; \
	done
	# build time metadata from Jetson Linux CI/CD
	cd $(ORIG_DIR) && for path in \
	    lib/firmware/nv-BT-Version \
	    lib/firmware/nv-WIFI-Version \
	    usr/share/doc/nvidia-l4t-firmware/changelog.Debian.gz \
	    ; do \
	    rm -rf $$path; \
	done
	# licensing for absent libnvdla_*.so files
	rm -f $(ORIG_DIR)/usr/share/doc/nvidia-l4t-firmware/LICENSE.nvdla.gz
	# firmware for NVIDIA accelerators on older Tegra platforms (e.g.
	# Xavier) including iGPU
	cd $(ORIG_DIR) && for path in \
	    lib/firmware/nvidia/gv11b/ \
	    lib/firmware/nvidia/tegra194/ \
	    lib/firmware/nvhost_nvdla010.fw \
	    lib/firmware/nvpva_010.fw \
	    lib/firmware/tegra19x/ \
	    ; do \
	    rm -rf $$path; \
	done
	# move licensing information to the top-level and use more useful names
	cd $(ORIG_DIR) && \
	    mv usr/share/doc/nvidia-l4t-firmware/copyright copyright.nvidia; \
	    mv usr/share/doc/nvidia-l4t-firmware/LICENSE.realtek_8822ce_wifibt.gz LICENSE.nvidia; \
	    mv usr/share/doc/nvidia-l4t-firmware/NOTICE.nvpva-fw NOTICE.nvpva-fw; \
	    mv ./usr/share/doc/nvidia-l4t-firmware/LICENSE.rtlwifi_firmware LICENSE.rtlwifi_firmware \
	    ;
	# remove any empty directories (e.g. result from removing all files)
	find $(ORIG_DIR) -type d -empty -delete
	# remove executable bits on data files
	find $(ORIG_DIR) -type f -exec chmod 644 {} \;
	# pack new orig tarball
	tar --owner=0 --group=0 -caf $(ORIG_TARBALL) $(ORIG_DIR)
	rm -rf $(ORIG_DIR)

# standalone target to download latest Jetson Linux deb for the target Jetson
# release without generating an orig tarball; usage:
#     debian/rules get-jetson-deb
# to download for another series:
#     debian/rules get-jetson-deb JETSON_REL=36.2
# NB: you can also download the latest deb with uscan --download
get-jetson-deb: $(JETSON_DEB)

# download a single Jetson Linux deb in the parent directory
../%.deb: jetson_pkg = $(shell echo $@ | cut -d/ -f2 | cut -d_ -f1)
../%.deb:
	cd .. && \
	    $(CURDIR)/debian/dl-jetson-linux-pkg --platform $(JETSON_PLAT) \
	        --release r$(JETSON_REL) --package $(jetson_pkg)
