#!/bin/bash

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

trap 'rm -Rf /tmp/assets; exit' ERR EXIT

#Fix the locale to use
export LC_ALL=C

#Download asset file from gitlab
curl https://gitlab.manjaro.org/ste74/cleanup-manjaro-gnome/raw/master/assets -o /tmp/assets -s

source /tmp/assets

manjaro=/usr/share/icons/manjaro/maia/maia.svg
warning=/usr/share/icons/hicolor/40x40/status/warn.png

yad --title "Manjaro GNOME Reset Tool" --on-top --center --window-icon=$manjaro --image=$warning --width=550 --fixed \
	--text '
	
<span font="11">This utility allows you to reset to a vanilla GNOME session:</span>
<span font="11">We remove all Manjaro GNOME customizations like extensions and 
configurations.

Keep in mind: after performing this action you need to reboot
your system:  Only the default wallpaper, theme,
icons and fonts will be kept. </span>

<span foreground="red" font="11">Press OK to perform the action!</span>' --text-align=left;

if [[ $? == 0 ]]; then

	if [[ -f /var/lib/pacman/db.lck  ]]; then
		yad --title "Manjaro GNOME Reset Tool" --on-top --center --window-icon=$manjaro --image=$warning --width=550 --fixed \
	--text '
	
<span foreground="red" font="11">Another install/remove session is running:
Abort or press Ok to force the execution</span>' --text-align=left;

		if [[ $? == 0 ]]; then
			gksu-polkit rm /var/lib/pacman/db.lck
		else exit 1
		fi
	fi
	
	manjaro_extensions=$( curl -s 'https://gitlab.manjaro.org/profiles-and-settings/iso-profiles/-/raw/master/manjaro/gnome/Packages-Desktop' | \
						grep 'gnome-shell-extension-' | grep -v '#' | cut -d " " -f2 )
	
	manjaro_set=("${manjaro_stuff}" "{$manjaro_extensions}")

	yes | pkexec pamac remove --no-confirm "${manjaro_set[@]}" && minimal_settings | yad --progress --title "Manjaro GNOME Reset Tool" --progress-text=Removing... --pulsate --auto-close
	
	yes | pkexec pamac install --no-confirm gnome-shell | yad --progress --title "Manjaro GNOME Reset Tool" --progress-text=Installing... --pulsate --auto-close

	yad --title "Manjaro GNOME Reset Tool" --on-top --center --window-icon=$manjaro --image=$warning --width=550 --fixed \
	--text '
	
<span font="11">Done! Eenjoy Manjaro GNOME Vanilla: Logout and Login again to for it to take effect.</span>'
		
fi
