#!/bin/sh
set -e
if [ "$1" = configure ]; then
	# versions before 0.153 removed their own units' enable links on every upgrade (the legacy
	# migration ran on upgrades too): enable them again
	if [ -n "${2:-}" ] && dpkg --compare-versions "$2" lt 0.153; then
		for u in ssh-hostkeys.service ps3gpu-nodes.service ps3-swap.service ps3-vswap.service; do
			deb-systemd-helper enable "$u" >/dev/null 2>&1 || true
		done
	fi
	# other packages' services this setup relies on, enabled on the first install (later, a
	# user's own disable stands). Not deb-systemd-helper: their own postinsts registered them
	# already (networkd as disabled), so its enable does nothing: a fresh image came up without
	# networkd (docs/IMAGE-DIFF.md). systemctl works offline in the image's chroot too.
	if [ -z "${2:-}" ]; then
		systemctl enable systemd-networkd.service ssh.service walnut-firstboot.service >/dev/null 2>&1 || true
	fi
	# DNS from NetworkManager (00-ps3.conf); the kernel's /proc/net/pnp only on an NFS root, whose
	# wired port NetworkManager leaves alone
	systemctl enable ps3-nfsroot-network.service >/dev/null 2>&1 || true
	if [ "$(readlink /etc/resolv.conf 2>/dev/null)" = /proc/net/pnp ] && ! grep -q 'root=/dev/nfs' /proc/cmdline 2>/dev/null; then
		ln -sf /run/NetworkManager/resolv.conf /etc/resolv.conf
	fi
	# DualShock 3: the controller never bonds, so BlueZ's input profile must accept unbonded
	# devices (bluez has no drop-in directory for input.conf)
	if [ -e /etc/bluetooth/input.conf ] && ! grep -q '^ClassicBondedOnly=false' /etc/bluetooth/input.conf; then
		sed -i 's/^#\? *ClassicBondedOnly *=.*/ClassicBondedOnly=false/' /etc/bluetooth/input.conf
		grep -q '^ClassicBondedOnly=false' /etc/bluetooth/input.conf ||
			printf '[General]\nClassicBondedOnly=false\n' >> /etc/bluetooth/input.conf
	fi
	# The image's one-line sources.list (the build snapshot, from snapshot.debian.org or the build
	# cache in front of it) gives way to debian.sources (live Debian ports); any other sources.list
	# is left alone
	if [ -e /etc/apt/sources.list ] &&
	   [ "$(grep -v '^\s*#' /etc/apt/sources.list | grep -v '^\s*$' | grep -cvE '(snapshot\.debian\.org|/debian-snapshot)/archive/debian-ports/')" = 0 ]; then
		mv /etc/apt/sources.list /etc/apt/sources.list.ps3-snapshot
	fi
fi
#DEBHELPER#
exit 0
