#!/bin/sh
# Takes over from the files the image's desktop setup wrote (before ps3-desktop existed): each one
# that no package owns is archived to /var/backups/ps3-legacy-desktop-<date>.tar and removed;
# dpkg then installs the packaged versions. Diversions: gvfs's iPhone and online-accounts volume
# monitors (moved aside, ~1.3 MB each per session for nothing on a PS3) and the menu entries of
# OpenArena, dsda-doom, OpenTTD and QuakeSpasm (ours start each mode in game mode) become this package's.
set -e
LEGACY="
/etc/systemd/system/getty@tty1.service.d/autologin.conf
/usr/local/bin/ps3-session
/usr/local/bin/ps3-game
/usr/local/bin/ps3-power
/usr/local/sbin/ps3-game-mode
/etc/systemd/user/ps3-display.slice
/etc/systemd/user/ps3-desktop.slice
/etc/systemd/user/ps3-game.slice
/etc/systemd/system/user-.slice.d/50-ps3-game-mode.conf
/etc/systemd/system/user@.service.d/50-ps3-game-mode.conf
/etc/systemd/user/gvfs-daemon.service.d/50-ps3-desktop.conf
/etc/systemd/user/gvfs-metadata.service.d/50-ps3-desktop.conf
/etc/systemd/user/gvfs-udisks2-volume-monitor.service.d/50-ps3-desktop.conf
/etc/systemd/user/gvfs-mtp-volume-monitor.service.d/50-ps3-desktop.conf
/etc/systemd/user/gvfs-gphoto2-volume-monitor.service.d/50-ps3-desktop.conf
/etc/systemd/user/gvfs-afc-volume-monitor.service.d/50-ps3-desktop.conf
/etc/systemd/user/gvfs-goa-volume-monitor.service.d/50-ps3-desktop.conf
/etc/sudoers.d/ps3-game-mode
/etc/xdg/pcmanfm/default/desktop-items-0.conf
/usr/share/applications/ps3-poweroff.desktop
/usr/share/applications/ps3-reboot.desktop
/etc/X11/xorg.conf.d/20-ps3rsx.conf
/etc/X11/xorg.conf.d/40-joystick-pointer.conf
"
migrate() {
	# the autologin user of the old drop-in becomes /etc/default/ps3-desktop's (the generator's)
	a=/etc/systemd/system/getty@tty1.service.d/autologin.conf
	if [ -e "$a" ] && [ ! -e /etc/default/ps3-desktop ]; then
		u=$(sed -n 's/.*--autologin \([^ ]*\).*/\1/p' "$a" | head -1)
		[ -z "$u" ] || printf '%s\n' '# The user that gets the desktop on tty1 (ps3-desktop; empty: a normal login prompt)' \
			"PS3_DESKTOP_USER=$u" > /etc/default/ps3-desktop
	fi
	found=
	for f in $LEGACY; do
		[ -e "$f" ] || [ -L "$f" ] || continue
		dpkg-query -S "$f" >/dev/null 2>&1 && continue
		found="$found ${f#/}"
	done
	[ -n "$found" ] || return 0
	mkdir -p /var/backups
	tar -C / -cf "/var/backups/ps3-legacy-desktop-$(date +%Y%m%d%H%M%S).tar" $found
	for f in $found; do rm -f "/$f"; done
	echo "ps3-desktop: legacy files archived in /var/backups and removed:$found" | fold -s -w 100
}
divert() {	# <file> <diverted name>: the file moves to the diverted name (a package installing it later lands there)
	f=$1 t=$2
	mkdir -p "$(dirname "$t")"
	if dpkg-divert --list "$f" | grep -q '^local diversion'; then
		dpkg-divert --quiet --local --rename --remove "$f"
	fi
	if [ "$(dpkg-divert --listpackage "$f")" = ps3-desktop ]; then
		# ours already, maybe to an older place (0.307: next to the file): moved by hand, since
		# --rename would refuse while the old package's own file is at <file>
		cur=$(dpkg-divert --truename "$f")
		[ "$cur" != "$t" ] || return 0
		dpkg-divert --quiet --package ps3-desktop --remove "$f"
		[ ! -e "$cur" ] || mv "$cur" "$t"
		dpkg-divert --quiet --package ps3-desktop --divert "$t" --add "$f"
	else
		dpkg-divert --quiet --package ps3-desktop --rename --divert "$t" --add "$f"
	fi
}
case "$1" in
install|upgrade)
	# the legacy files only on the first install (on an upgrade they are this package's own)
	if [ "$1" = install ] && [ -z "${2:-}" ]; then migrate; fi
	for m in afc goa; do
		divert /usr/share/gvfs/remote-volume-monitors/$m.monitor /usr/share/gvfs/remote-volume-monitors/$m.monitor.disabled
	done
	# the games' own menu entries (ours give each game its modes), moved out of the menu's reach:
	# jgmenu takes any file name containing ".desktop" in the applications directories, so the
	# originals go to a directory it does not read (0.307's openarena.desktop.ps3-orig showed up as
	# a second OpenArena). The list is read by tests/menu-check.sh
	for g in openarena dsda-doom openttd quakespasm; do
		divert /usr/share/applications/$g.desktop /usr/share/ps3-desktop/diverted-menu/$g.desktop
	done
	;;
esac
#DEBHELPER#
exit 0
