#!/usr/bin/make -f
P := debian/ps3-system
# The release version: the top-level VERSION file, the only place it is written (copied in as
# walnut-version, package.conf COPY_IN); 0.x is an alpha
VER := $(shell cat walnut-version)
STAGE := $(if $(filter 0.%,$(VER)), (alpha),)

%:
	dh $@

override_dh_auto_install:
	install -D -m 644 files/network/20-ps3-wired.network $(P)/usr/lib/systemd/network/20-ps3-wired.network
	install -D -m 644 files/network/21-ps3-wired-nm.network $(P)/usr/lib/systemd/network/21-ps3-wired-nm.network
	install -D -m 644 files/network/ps3-nfsroot-network.service $(P)/usr/lib/systemd/system/ps3-nfsroot-network.service
	install -d $(P)/usr/lib/ps3/sbin
	install -m 755 files/sbin/* $(P)/usr/lib/ps3/sbin/
	install -d $(P)/usr/sbin
	ln -s ../lib/ps3/sbin/ps3-gameos-mount $(P)/usr/sbin/ps3-gameos-mount
	ln -s ../lib/ps3/sbin/walnut-firstboot $(P)/usr/sbin/walnut-firstboot
	ln -s ../lib/ps3/sbin/walnut-bugreport $(P)/usr/sbin/walnut-bugreport
	install -D -m 644 debian/ps3vram.conf $(P)/usr/lib/modprobe.d/ps3vram.conf
	install -D -m 644 debian/60-ps3-memory.conf $(P)/usr/lib/sysctl.d/60-ps3-memory.conf
	install -D -m 644 debian/00-ps3.conf $(P)/usr/lib/NetworkManager/conf.d/00-ps3.conf
	install -D -m 644 debian/70-ds3-pair.rules $(P)/usr/lib/udev/rules.d/70-ds3-pair.rules
	install -D -m 644 debian/71-ps3-vswap.rules $(P)/usr/lib/udev/rules.d/71-ps3-vswap.rules
	# a GPU crash (ps3rsx's uevent) starts ps3-gpu-crash-report.service; display-only mode's X
	# configuration (the desktop's session start passes -configdir ps3-display-only.d)
	install -D -m 644 debian/72-ps3-gpu-crash.rules $(P)/usr/lib/udev/rules.d/72-ps3-gpu-crash.rules
	# the RSX's render node for the render group only (after 50-udev-default's 0666)
	install -D -m 644 debian/73-ps3-render-node.rules $(P)/usr/lib/udev/rules.d/73-ps3-render-node.rules
	install -D -m 644 files/xorg/20-ps3rsx-display-only.conf $(P)/etc/X11/ps3-display-only.d/20-ps3rsx-display-only.conf
	install -D -m 644 debian/sshd-ps3.conf $(P)/etc/ssh/sshd_config.d/ps3.conf
	install -D -m 644 debian/kernel-img.conf $(P)/etc/kernel-img.conf
	install -D -m 644 files/apt/walnut.sources $(P)/etc/apt/sources.list.d/walnut.sources
	install -D -m 644 files/apt/debian.sources $(P)/etc/apt/sources.list.d/debian.sources
	install -D -m 644 files/apt/walnut.pref $(P)/etc/apt/preferences.d/walnut
	install -D -m 644 files/apt/walnut-archive-keyring.gpg $(P)/usr/share/keyrings/walnut-archive-keyring.gpg
	install -D -m 644 files/apt/walnut-testing-keyring.gpg $(P)/usr/share/keyrings/walnut-testing-keyring.gpg
	install -D -m 644 files/sound/alsoft.conf $(P)/etc/xdg/alsoft.conf
	# Walnut's name for the system (hostnamectl, lsb_release, neofetch), Debian-based (ID_LIKE);
	# base-files' os-release and login banners are diverted (preinst), /etc/debian_version stays.
	# The banners go to /usr/share/walnut and postinst copies them into /etc: shipped in /etc they
	# would be conffiles at a path base-files' conffiles hold too, and dpkg stopped to ask about it
	printf '%s\n' 'PRETTY_NAME="Walnut $(VER)$(STAGE)"' 'NAME="Walnut"' 'VERSION_ID="$(VER)"' \
		'VERSION="$(VER)$(STAGE)"' 'ID=walnut' 'ID_LIKE=debian' 'HOME_URL="https://walnut.nanobyte.dev/"' \
		'SUPPORT_URL="https://github.com/nanobyte-dev/walnut-linux/blob/main/docs/INSTALL.md"' \
		'BUG_REPORT_URL="https://github.com/nanobyte-dev/walnut-linux/issues"' > debian/os-release
	install -D -m 644 debian/os-release $(P)/usr/lib/os-release
	printf 'Walnut $(VER)$(STAGE) \\n \\l\n\n' > debian/issue
	install -D -m 644 debian/issue $(P)/usr/share/walnut/issue
	printf 'Walnut $(VER)$(STAGE)\n' > debian/issue.net
	install -D -m 644 debian/issue.net $(P)/usr/share/walnut/issue.net

# Our units: enabled; swap and the boot-time oneshots are never stopped or restarted by upgrades
override_dh_installsystemd:
	dh_installsystemd --name=ssh-hostkeys --no-start
	dh_installsystemd --name=ps3gpu-nodes --no-stop-on-upgrade --no-restart-after-upgrade
	dh_installsystemd --name=ps3-swap --no-start --no-stop-on-upgrade --no-restart-after-upgrade
	dh_installsystemd --name=ps3-vswap --no-start --no-stop-on-upgrade --no-restart-after-upgrade
	dh_installsystemd --name=ps3-shutdown-log --no-stop-on-upgrade --no-restart-after-upgrade
	dh_installsystemd --name=walnut-firstboot --no-start
	dh_installsystemd --name=ps3-sound-volume --no-start --no-stop-on-upgrade --no-restart-after-upgrade
	dh_installsystemd --name=ps3-gpu-escape
	dh_installsystemd --name=ps3-gpu-crash-report --no-start --no-stop-on-upgrade --no-restart-after-upgrade
