#!/usr/bin/env bash
#shellcheck disable=SC2155
#shellcheck source=/dev/null

#  /usr/bin/bigcontrolcenter-clean-menu
#  Created: 0000/00/00
#  Altered: 2023/09/02
#
#  Copyright (c) 2023-2023, Vilmar Catafesta <vcatafesta@gmail.com>
#                0000-2023, bigbruno Bruno Gonçalves <bruno@biglinux.com.br>
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions
#  are met:
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
#  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
#  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
#  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
#  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
#  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
#  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


APP="${0##*/}"
_VERSION_="1.0.0-20230902"
export BOOTLOG="/tmp/bigcontrolcenter-$USER-$(date +"%d%m%Y").log"
export LOGGER='/dev/tty8'
LIBRARY=${LIBRARY:-'/usr/share/bigbashview/bcc/shell'}
[[ -f "${LIBRARY}/bcclib.sh" ]] && source "${LIBRARY}/bcclib.sh"

function sh_config {
	export TEXTDOMAINDIR="/usr/share/locale"
	export TEXTDOMAIN=bigcontrolcenter

	#diretorio onde vão estar os arquivos ocultos do menu
	declare -g bigdir='/usr/share/applications/bigcontrolcenter'
	declare -gA APath=(	[share_path]='/usr/share/applications'
						[ksrv_path]='/usr/share/kservices5/bigcontrolcenter')
	#lista dos arquivos a serem ocultados
	declare -ga aDesktopFiles=("biglinux-config.desktop"
										"big-reset.desktop"
										"system-config-printer.desktop"
										"pavucontrol-qt.desktop"
										"bigkernel.desktop"
										"gparted.desktop"
										"timeshift-gtk.desktop"
										"mintstick-kde.desktop"
										"mintstick-format-kde.desktop"
										"biglinux-themes-gui.desktop"
										"org.kde.kwalletmanager5.desktop"
										"kdesystemsettings.desktop"
										"org.kde.kinfocenter.desktop"
										"biglinux-tweaks.desktop"
										"kvantummanager.desktop"
										"manjaro-settings-manager.desktop"
										"msm_kde_notifier_settings.desktop"
										"UserFeedbackConsole.desktop"
										"cmake-gui.desktop"
										"avahi-discover.desktop"
										"bssh.desktop"
										"bvnc.desktop"
										"htop.desktop"
										"lstopo.desktop"
										"cups.desktop"
										"qv4l2.desktop"
										"mpv.desktop"
										"hplip.desktop"
										"hp-uiscan.desktop"
										"systemsettings.desktop"
										"kdesettings.desktop"
										"org.kde.kuserfeedback-console.desktop"
										"gsmartcontrol.desktop"
										"bootsplash-manager.desktop"
										"gnome-alsamixer.desktop"
										"appimagelaunchersettings.desktop"
										"org.kde.ksystemlog.desktop"
										"org.kde.filelight.desktop"
										"klassy-settings.desktop")
}

function sh_fix_permissions {
	# Fix permissions
	chmod 644 "$bigdir"/*.desktop
}

function sh_change_file {
	local cfile="$1"

	#remover as entradas do KCModulee Type=Service e desocultar a categoria
	sed -i 's|X-KDE-ServiceTypes=KCModule||g;s|Type=Service||g;/Categories/s/^/#/;/DBusActivatable/s/^/#/' "$cfile"
	mv -f "$cfile" "$bigdir/"
}

function sh_init {
	local i
	local cpath
	local cfile

	#criar o diretorio
	if [[ ! -d "$bigdir" ]]; then
		if ! mkdir -p "$bigdir" 2> /dev/null; then
			kdialog --title $"$APP" --error $"Erro na criação do diretório : $bigdir"
			exit 1
		fi
	fi

	for i in "${aDesktopFiles[@]}"; {
		for cpath in "${APath[@]}"; {
			[[ -s "$cpath/$i" ]] && sh_change_file "$cpath/$i"
		}

		if [[ ! -e "${APath[share_path]}/$i" ]]; then
			if [[ -e "$bigdir/$i" ]]; then
				if ! which "$(grep -m1 "Exec=" "$bigdir/$i" | cut -f2 -d= | cut -f1 -d" ")" >/dev/null 2>&- ; then
					rm -f "$bigdir/$i" >/dev/null 2>&-
				fi
			fi
		fi
	}

	cfile='/usr/share/desktop-directories/kf5-unknown.directory'
	if [[ -e "$cfile" ]]; then
		if ! grep -q NoDisplay=true "$cfile"; then
			echo "NoDisplay=true" >> "$cfile" 2> /dev/null
		fi
	fi

	cfile='/usr/share/applications/bigcontrolcenter/org.kde.ksystemlog.desktop'
	[[ -e "$cfile" ]] && sed -i 's|X-KDE-SubstituteUID=true|X-KDE-SubstituteUID=false|g' "$cfile"

	sh_fix_permissions
	update-desktop-database 2> /dev/null
}

#sh_debug
sh_config
sh_init
