#!/bin/sh

if [[ -f /usr/lib/systemd/system/gdm.service ]]; then
	systemctl disable lightdm &>/dev/null
	systemctl disable sddm &>/dev/null
	systemctl disable xdm &>/dev/null
	systemctl enable gdm
fi

X11_LANGS="$(localectl status |grep "X11 Layout: " |rev |cut -d" " -f1 |rev)"
# For (unset) bug
if [ "$X11_LANGS" = "(unset)" ] && [ "$LANG" = "ru_RU.UTF-8" ]; then X11_LANGS="us,ru"; fi
for i in $(ls /home); do
	id $i &>/dev/null && getent group $i &>/dev/null || continue
	[ -d /home/$i/.local/share/gnome-shell/extensions ] && ! [ -f /usr/bin/update-r12-to-r13 ] && continue
	mkdir -p /home/$i/.config/autostart
	mkdir -p /home/$i/.config/dconf
	mkdir -p /home/$i/.local/share/gnome-shell
	cp -f /etc/skel/.config/autostart/gnome-desktop-icons.desktop /home/$i/.config/autostart/gnome-desktop-icons.desktop
	cp -f /etc/skel/.config/autostart/gnome-desktop-icons-touch.desktop /home/$i/.config/autostart/gnome-desktop-icons-touch.desktop
	cp -f /usr/share/gnome-shell-rosa-extensions/keyboard-settings.desktop /home/$i/.config/autostart/keyboard-settings.desktop
	cp -f /usr/share/gnome-shell-rosa-extensions/no-overview-at-startup.desktop /home/$i/.config/autostart/no-overview-at-startup.desktop
	cp -f /usr/share/gnome-shell-rosa-extensions/update_applet.desktop /home/$i/.config/autostart/update_applet.desktop
	cp -f /usr/share/gnome-shell-rosa-extensions/xremap.desktop /home/$i/.config/autostart/xremap.desktop
	cp -rf /usr/share/gnome-shell-rosa-extensions/extensions /home/$i/.local/share/gnome-shell
	if echo $X11_LANGS |grep -q ru; then
		cp -f /usr/share/gnome-shell-rosa-extensions/user /home/$i/.config/dconf/user
	else
		cp -f /usr/share/gnome-shell-rosa-extensions/user-en /home/$i/.config/dconf/user
		# Revert hard localization
		pushd /home/$i/.local/share/gnome-shell/extensions
			for PATCH in $(ls /usr/share/gnome-shell-rosa-extensions |grep "hard-ru-translate.patch"); do
				patch -p0 -F3 -R </usr/share/gnome-shell-rosa-extensions/$PATCH || :
			done
		popd
	fi
	chown -R $i:$i /home/$i/.config/autostart
	chown -R $i:$i /home/$i/.config/dconf
	chown -R $i:$i /home/$i/.local/share/gnome-shell
	# For xremap
	gpasswd -a $i input
done
if echo $X11_LANGS |grep -q ru; then
	# Configure keyboard for tty
	localectl set-keymap ruwin_ct_sh-UTF-8
fi

# Set font for tty
sed -i 's/FONT=.*/FONT=latarcyrheb-sun16/' /etc/vconsole.conf

/usr/bin/keyboard-settings

# One shot
rm -f /usr/libexec/iso-switch-to-gnome
touch /usr/libexec/iso-switch-to-gnome
