#!/bin/sh
#
# lxtaz-setup - check that the LXTaz desktop is set up, fix it with --fix.
#
# As a user: the window manager of ~/.config/slitaz/applications.conf
# (slim and startx) and the autostart copies that override the system
# entries. As root: /etc/rcS.conf login manager, lxtaz-dm default
# session, the system and skel applications.conf.
#
# Copyright (C) 2026 SliTaz GNU/Linux - BSD License
# Author: Christophe Lincoln <pankso@slitaz.org>
# AI assistant: Claude (Anthropic)
#

usage() {
	cat <<EOT
Usage: lxtaz-setup [--fix]

Check the LXTaz desktop of the current user (and of the system when run
as root), one line per item. --fix repairs what is wrong.
EOT
}

fix=""
case "$1" in
	--fix) fix=yes ;;
	"") ;;
	*) usage; exit 1 ;;
esac

errors=0

ok()   { echo "ok    $1"; }
note() { echo "note  $1"; }
bad()  {
	if [ "$fix" ]; then
		echo "fix   $1"
	else
		echo "fail  $1"
		errors=$((errors + 1))
	fi
}

# WINDOW_MANAGER of an applications.conf file: lxtaz-session
check_wm() {
	file="$1"
	if [ ! -f "$file" ]; then
		if [ -f /etc/skel/.config/slitaz/applications.conf ]; then
			bad "$file: missing"
			[ "$fix" ] && mkdir -p "${file%/*}" &&
				cp /etc/skel/.config/slitaz/applications.conf "$file"
		else
			note "$file: missing, nothing to copy it from"
			return
		fi
	fi
	[ -f "$file" ] || return
	wm=$(sed -n 's/^WINDOW_MANAGER="*\([^"]*\)"*.*/\1/p' "$file" | head -n1)
	if [ "$wm" = "lxtaz-session" ]; then
		ok "$file: WINDOW_MANAGER=lxtaz-session"
		return
	fi
	bad "$file: WINDOW_MANAGER=${wm:-(unset)} instead of lxtaz-session"
	[ "$fix" ] || return
	if [ "$wm" ]; then
		sed -i 's/^WINDOW_MANAGER=.*/WINDOW_MANAGER="lxtaz-session"/' "$file"
	else
		printf '\n# Window manager.\nWINDOW_MANAGER="lxtaz-session"\n' >> "$file"
	fi
	# lxde-session when the lxtaz package is removed
	grep -qF 'which $WINDOW_MANAGER' "$file" ||
		echo 'which $WINDOW_MANAGER >/dev/null 2>&1 || WINDOW_MANAGER=lxde-session' \
			>> "$file"
}

#
# The lxtaz session itself
#

if command -v lxtaz-session >/dev/null; then
	ok "lxtaz-session installed"
else
	note "lxtaz-session not found: install the lxtaz package"
fi

#
# System (root)
#

if [ "$(id -u)" = 0 ]; then
	# Login manager
	if [ -x /etc/init.d/lxtaz-dm ]; then
		lm=$(sed -n 's/^LOGIN_MANAGER="*\([^"]*\)"*.*/\1/p' /etc/rcS.conf)
		if [ "$lm" = "lxtaz-dm" ]; then
			ok "/etc/rcS.conf: LOGIN_MANAGER=lxtaz-dm"
		else
			bad "/etc/rcS.conf: LOGIN_MANAGER=${lm:-(empty)} instead of lxtaz-dm"
			if [ "$fix" ]; then
				[ -e "/etc/rcS.conf.${lm:-none}" ] ||
					cp -a /etc/rcS.conf "/etc/rcS.conf.${lm:-none}"
				sed -i 's/^LOGIN_MANAGER=.*/LOGIN_MANAGER="lxtaz-dm"/' \
					/etc/rcS.conf
				echo "      old file: /etc/rcS.conf.${lm:-none}, used at next boot"
			fi
		fi
	else
		note "lxtaz-dm not installed: login manager unchanged"
	fi

	# lxtaz-dm default session (dm.conf is kept on upgrades)
	dm=/etc/lxtaz/dm.conf
	if [ -f "$dm" ]; then
		ds=$(sed -n 's/^default-session=//p' "$dm")
		case "$ds" in
			lxtaz) ok "$dm: default-session=lxtaz" ;;
			""|lxde)
				bad "$dm: default-session=${ds:-(empty)} instead of lxtaz"
				[ "$fix" ] && sed -i \
					's/^default-session=.*/default-session=lxtaz/' "$dm" ;;
			*) note "$dm: default-session=$ds (kept, your choice)" ;;
		esac
	fi

	# One lxsession config for LXDE and LXTaz
	if [ -e /etc/xdg/lxsession/LXTaz ]; then
		ok "/etc/xdg/lxsession/LXTaz"
	else
		bad "/etc/xdg/lxsession/LXTaz: missing"
		[ "$fix" ] && ln -sfn LXDE /etc/xdg/lxsession/LXTaz
	fi

	# Root reads the system file, new users get the skel copy
	check_wm /etc/slitaz/applications.conf
	[ -f /etc/skel/.config/slitaz/applications.conf ] &&
		check_wm /etc/skel/.config/slitaz/applications.conf
fi

#
# Current user
#

if [ "$(id -u)" != 0 ]; then
	check_wm "$HOME/.config/slitaz/applications.conf"
fi

# A user autostart entry replaces the system one of the same name. Copies
# made before the LXTaz session (no LXTaz in *ShowIn=) break the session
# split: moved aside to name.desktop.old, which lxsession ignores.
auto="${XDG_CONFIG_HOME:-$HOME/.config}/autostart"
for name in lxtaz-files-desktop lxtaz-dock-daemon lxtaz-dash-daemon \
	lxtaz-ctrl-daemon lxtaz-panel lxpanel pcmanfm; do
	f="$auto/$name.desktop"
	[ -f "$f" ] || continue
	[ -f "/etc/xdg/autostart/$name.desktop" ] || continue
	if grep -q '^[A-Za-z]*ShowIn=.*LXTaz' "$f"; then
		ok "$f (LXTaz-aware copy)"
	else
		bad "$f: old copy, overrides /etc/xdg/autostart/$name.desktop"
		[ "$fix" ] && mv -f "$f" "$f.old"
	fi
done

if [ "$errors" -gt 0 ]; then
	echo "$errors item(s) to fix: lxtaz-setup --fix"
	exit 1
fi
