#!/usr/bin/env bash

#  usr/bin/bigcontrolcenter-restore-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 {
	#diretorio onde vão estar os arquivos ocultos do menu
	declare -gA APath=(	[bigdir_path]='/usr/share/applications/bigcontrolcenter'
						[ksrv_path]='/usr/share/kservices5/bigcontrolcenter'
						)
	declare -ga aDesktopFiles
#:<<'comment'
#	declare -g bigdir='/usr/share/applications/bigcontrolcenter'
#	declare -g ksrv_path='/usr/share/kservices5/bigcontrolcenter'
#	declare -ga aDesktopFiles=(
#					"biglinux-config.desktop"
#					"big-reset.desktop"
#					"system-config-printer.desktop"
#					"openjdk-8-policytool.desktop"
#					"pavucontrol-qt.desktop"
#					"bigkernel.desktop"
#					"gufw.desktop"
#					"gparted.desktop"
#					"hardinfo.desktop"
#					"bigapache.desktop"
#					"timeshift-gtk.desktop"
#					"mintstick-kde.desktop"
#					"mintstick-format-kde.desktop"
#					"biglinux-themes-gui.desktop"
#					"sambasearch.desktop"
#					"org.kde.kwalletmanager5.desktop"
#					"bigflash-javaws-widevine-fonts.desktop"
#					"tlpui.desktop"
#					"kdesystemsettings.desktop"
#					"org.kde.kinfocenter.desktop"
#					"grub-customizer.desktop"
#					"biglinux-tweaks.desktop"
#					"kvantummanager.desktop"
#					"manjaro-settings-manager.desktop"
#					"msm_kde_notifier_settings.desktop"
#					"designer.desktop"
#					"linguist.desktop"
#					"qdbusviewer.desktop"
#					"assistant.desktop"
#					"UserFeedbackConsole.desktop"
#					"cmake-gui.desktop"
#					"avahi-discover.desktop"
#					"bssh.desktop"
#					"bvnc.desktop"
#					"htop.desktop"
#					"lstopo.desktop"
#					"cups.desktop"
#					"org.gnome.baobab.desktop"
#					"qv4l2.desktop"
#					"qvidcap.desktop"
#					"mpv.desktop"
#					"hplip.desktop"
#					"hp-uiscan.desktop"
#					"systemsettings.desktop"
#					"kdesettings.desktop"
#					"org.kde.kuserfeedback-console.desktop"
#					"gsmartcontrol.desktop"
#					"org.pipewire.Helvum.desktop"
#					"bootsplash-manager.desktop"
#					"gnome-alsamixer.desktop"
#					"appimagelaunchersettings.desktop"
#					"org.kde.ksystemlog.desktop"
#					"jamesdsp.desktop"
#					"brightness-controller.desktop"
#					)
#comment
}

# Usando find para localizar arquivos .desktop e salvá-los em um array
function sh_search_desktop_files {
	local aFiles=()

	for i in "${APath[@]}"; {
#		aDesktopFiles+=($(find "$i" -type f -name "*.desktop"))
		mapfile -t aFiles < <(find "$i" -type f -name "*.desktop")
		aDesktopFiles+=("${aFiles[@]}")
	}
}

function sh_change_file {
	local cfile="$1"

	sed -i 's|X-KDE-ServiceTypes=KCModule||g;s|Type=Service||g;/Categories/s/#//;/DBusActivatable/s/#//' "$cfile"
	mv -f "$cfile" "/usr/share/applications/"
}

function sh_main {
	[[ ! -e "${APath[bigdir_path]}" && ! -e "${APath[ksrv_path]}" ]] && return

	sh_search_desktop_files
	for i in "${aDesktopFiles[@]}"; {
		if [[ -e "$i" ]]; then
#			EXEC="$(awk -F '=' '/Exec=/ {split($2, a, " "); print a[1]}' "$i")"
			EXEC="$(grep -m1 "Exec=" "$i" | cut -f2 -d= | cut -f1 -d" ")"

			if ! which "$EXEC"; then
				rm -f "$i"
				continue
			fi
			sh_change_file "$i"
		fi
	}
}

#sh_debug
sh_config
sh_main
