#!/usr/bin/make -f
P := debian/ps3-desktop

%:
	dh $@

override_dh_auto_build:
	visudo -cf debian/sudoers
	python3 -I files/sounds/make-startup-chime.py debian/startup.wav

override_dh_auto_install:
	install -d $(P)/usr/bin $(P)/usr/lib/ps3/sbin
	install -m 755 files/bin/* $(P)/usr/bin/
	install -m 755 files/sbin/* $(P)/usr/lib/ps3/sbin/
	install -d $(P)/usr/lib/ps3/desktop
	find files/libexec -maxdepth 1 -type f ! -name '*.py' -exec install -m 755 {} $(P)/usr/lib/ps3/desktop/ \;
	install -m 644 files/libexec/*.py $(P)/usr/lib/ps3/desktop/
	# ps3_game_prefetch.py byte-compiled at installation (postinst) and again when python3 changes
	# (this hook, as dh_python3 writes it for a private directory): ps3-game-prefetch imports it
	# instead of compiling it at every start, which would cost its game watcher ~5 MB
	install -D -m 755 debian/python3.rtupdate $(P)/usr/share/python3/runtime.d/ps3-desktop.rtupdate
	install -D -m 755 files/generators/ps3-desktop-autologin \
		$(P)/usr/lib/systemd/system-generators/ps3-desktop-autologin
	install -D -m 644 files/profile.d/ps3-desktop.sh $(P)/etc/profile.d/ps3-desktop.sh
	install -D -m 440 debian/sudoers $(P)/etc/sudoers.d/ps3-desktop
	# discs in the PS3's drive mount from the desktop like any removable medium (udisks + polkit)
	install -D -m 644 files/udev/60-walnut-optical.rules $(P)/usr/lib/udev/rules.d/60-walnut-optical.rules
	install -D -m 644 files/udev/61-walnut-loop.rules $(P)/usr/lib/udev/rules.d/61-walnut-loop.rules
	install -D -m 644 files/polkit/49-walnut-optical.rules $(P)/usr/share/polkit-1/rules.d/49-walnut-optical.rules
	cd files/xdg && find . -type f -exec install -D -m 644 {} ../../$(P)/etc/xdg/ps3/{} \;
	install -d $(P)/usr/share/X11/xorg.conf.d
	install -m 644 files/xorg/* $(P)/usr/share/X11/xorg.conf.d/
	install -d $(P)/usr/lib/systemd/user
	install -m 644 files/systemd/user/* $(P)/usr/lib/systemd/user/
	printf '%s\n' '[Slice]' 'MemoryLow=176M' > debian/game-mode.conf
	install -D -m 644 debian/game-mode.conf $(P)/usr/lib/systemd/system/user-.slice.d/50-ps3-game-mode.conf
	printf '%s\n' '[Service]' 'MemoryLow=176M' > debian/game-mode-service.conf
	install -D -m 644 debian/game-mode-service.conf $(P)/usr/lib/systemd/system/user@.service.d/50-ps3-game-mode.conf
	printf '%s\n' '[Service]' 'Slice=ps3-desktop.slice' > debian/gvfs-slice.conf
	for u in gvfs-daemon gvfs-metadata gvfs-udisks2-volume-monitor gvfs-mtp-volume-monitor \
		gvfs-gphoto2-volume-monitor gvfs-afc-volume-monitor gvfs-goa-volume-monitor; do \
		install -D -m 644 debian/gvfs-slice.conf $(P)/usr/lib/systemd/user/$$u.service.d/50-ps3-desktop.conf; \
	done
	install -D -m 644 debian/startup.wav $(P)/usr/share/sounds/walnut/startup.wav
	install -d $(P)/usr/share/applications
	install -m 644 files/applications/* $(P)/usr/share/applications/
	install -d $(P)/usr/share/backgrounds/walnut
	install -m 644 files/backgrounds/*.jpg $(P)/usr/share/backgrounds/walnut/
	install -D -m 644 files/backgrounds/CREDITS $(P)/usr/share/doc/ps3-desktop/CREDITS.backgrounds

override_dh_auto_clean:
	rm -f debian/game-mode.conf debian/game-mode-service.conf debian/gvfs-slice.conf
