#!/usr/bin/env bash
#shellcheck disable=SC2155,SC2005,SC2034,SC1078,SC1079,SC2026
#shellcheck source=/dev/null

#  usr/bin/hardinfo-relatory
#  Created: 0000/00/00
#  Altered: 2023/08/07
#
#  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-20230807"
BOOTLOG="/tmp/bigcontrolcenter-$USER-$(date +"%d%m%Y").log"
LOGGER='/dev/tty8'
LIBRARY=${LIBRARY:-'/usr/share/bigbashview/bcc/shell'}
[[ -f "${LIBRARY}/bcclib.sh" ]] && source "${LIBRARY}/bcclib.sh"

function sh_config {
	#Translation
	export TEXTDOMAINDIR="/usr/share/locale"
	export TEXTDOMAIN=bigcontrolcenter
	declare -g FILE_TO_SAVE="$HOME/hardware-info-$(date +%Y-%m-%d_%Hh-%Mm).html"
	declare -g PROGRESS="/tmp/hardinfo-relatory-progress"
	declare -g OIFS=$IFS
	declare -gi counter=0
	IFS=$'\n'
}

function sh_progress {
#	((counter+=10))
#	qdbus "$PROGRESS" org.kde.kdialog.ProgressDialog.value "$counter"
	return
}


function sh_style {
	sh_progress
	bigsudo urxvt -geometry 200x1 -e "/usr/share/bigbashview/bcc/apps/bigcontrolcenter/inxi"

	echo '<style>
	* {
		font-family: 'ubuntu mono', monospace;
		font-size: 17px;
	}

	.content {
		background-color: #505050;
		margin: 20px;
		border-radius: 8px;
		padding: 16px;
		color: #ffffff;
	}
	.ansi1 {
		font-weight: bold;
	}
	.ansi34 {
		color: #afe1ff !important;
		display: inline;
		line-height: 26px;
	}
	.ansititleansititle {
		text-align-last: center;
		/* padding-top: 15px; */
		display: block !important;
		color: #afe1ff !important;
	}
	textarea {
		background-color: #505050;
		color: white;
		border: 0;
		line-height: 26px;
	}
	.body_background {
		background: linear-gradient(to bottom, #414345, #232526);
	}
	table {
		width: 100%;
	}
	th {
		background: #dfdfdf;
		border-radius: 16px;
		padding: 3px;
	}
	td {
		line-height: 30px;
		padding-left: 8px;
		padding-right: 8px;
	};
	.color1 {
		background: white;
	}
	.color2 {
		background: red;
	}
	.color1 {
		background: #505050;
		color: white;
	}
	.color2 {
		background: #404244;
		color: #76f2ff;
	}
	a {
		font-weight: bold;
		color: #afe1ff;
	}
	td.search {
		text-align: center;
	}
	</style>' > "$FILE_TO_SAVE"
}

function sh_lspci_special {
	sh_progress
	{
		echo '<div class="content">'
		echo '<div class="ansititleansititle ansi1 ansi34">'
		echo 'PCI DEVICES'
		echo '</div>'
		echo '<table><tr><th>'
		echo $"Category"
		echo '</th><th>'
		echo $"Name"
		echo '</th><th>'
		echo $"ID"
		echo '</th><th>'
		echo $"Info"
		echo '</th></tr>'
	} >> "$FILE_TO_SAVE"

	line=color1
	for i in $(lspci -nn); do
		[[ "$line" = "color2" ]] && line=color1 || line=color2
		{
			echo "<tr class=$line><td>"
			#Category
			echo "$i" | cut -f2 -d: | cut -f2- -d" " | cut -f1 -d[
			echo '</td><td>'
			#Name
			echo "$i" | cut -f3 -d: | rev | cut -f2- -d[ | rev
			echo '</td><td class=search>'
			#ID
			echo "$i" | rev | cut -f1 -d[ | rev | cut -f1 -d]
			echo '</td><td class=search>'
			#Search
			ID_SEARCH="$(echo "$i" | rev | cut -f1 -d[ | rev | cut -f1 -d] | sed 's|:|-|g')"
			echo "<a href=\"https://linux-hardware.org/?id=pci:${ID_SEARCH}\">"
			echo 'Info'
			echo '</a>'
			echo '</td></tr>'
		} >> "$FILE_TO_SAVE"
	done
	echo '</table></div>' >> "$FILE_TO_SAVE"
}

function sh_lsusb {
	sh_progress
	{
		echo '<div class="content">'
		echo '<div class="ansititleansititle ansi1 ansi34">'
		echo 'USB DEVICES'
		echo '</div>'
		echo '<table><tr><th>'
		echo $"Name"
		echo '</th><th>'
		echo $"ID"
		echo '</th><th>'
		echo $"Info"
		echo '</th></tr>'
	} >> "$FILE_TO_SAVE"

	line=color1
	for i in $(lsusb); do
		[[ "$line" = "color2" ]] && line=color1 || line=color2
		{
			echo "<tr class=$line><td>"
			#Name
			echo "$i" | cut -f7- -d" "
			echo '</td><td class=search>'
			#ID
			echo "$i" | cut -f6 -d" "
			echo '</td><td class=search>'
			#Search
			ID_SEARCH="$(echo "$i" | cut -f6 -d" " | sed 's|:|-|g')"
			echo "<a href=\"https://linux-hardware.org/?id=usb:${ID_SEARCH}\">"
			echo "Info"
			echo "</a>"
			echo '</td></tr>'
		} >> "$FILE_TO_SAVE"
	done
	echo '</table></div>' >> "$FILE_TO_SAVE"

	IFS=$OIFS
#	cat /tmp/inxi.tmp >> "$FILE_TO_SAVE"
#	rm -f /tmp/inxi.tmp
}

function sh_html {
	local ctitle="$1"
	local cjob="$2"

	sh_progress
	{
		echo '<div class="content">'
		echo '<div class="ansititleansititle ansi1 ansi34">'
		echo "$ctitle"
		echo '</div>'
		echo '<div class="text_area">'
		echo '<textarea style="width:100%;" rows="20">'
		eval "$cjob"
		echo '</textarea></div></div>'
	} >> "$FILE_TO_SAVE"
}

function sh_lspci {
	sh_html 'lspci' "lspci -nn"
	sh_html 'lspci - verbose mode' "lspci -nvv"
}

function sh_lsusb_v {
	sh_html 'lsusb' "lsusb"
}

function sh_fstab {
	sh_html '/etc/fstab' "sed 's/#.*//' /etc/fstab | sed 's/#.*//' /etc/fstab | column --table --table-columns '<file system>','mount point>','<type','<options>','<dump>','<pass>'"
}

function sh_lsmod {
	sh_html 'lsmod' "lsmod"
}

function sh_ip_address {
	sh_html 'ip address' "ip address"
}

function sh_ip_route {
	sh_html 'ip route' "ip route"
}

function sh_ip_link {
	sh_html 'ip link' "ip link show"
}

function sh_ping_route {
	local gw=$(ip route show default | awk '/default/ {print $3}')
	sh_html 'ping route and 8.8.8.8' "ping -qnAc3 $gw; ping -qnAc3 8.8.8.8"
}

function sh_rfkill {
	sh_html 'rfkill list (Wifi and Bluetooth' "rfkill"
}

function sh_nmcli {
	sh_html 'nmcli general status' "nmcli --colors=no"
}

function sh_mhwd_l {
	sh_html 'mhwd -l' "mhwd -l | sed 's/\x1B\[[0-9;]*[mG]//g'"
}

function sh_mhwd_li {
	sh_html 'mhwd -li' "mhwd -li | sed 's/\x1B\[[0-9;]*[mG]//g'"
}

function sh_mhwd_lh {
	sh_html 'mhwd -lh' "mhwd -lh | sed 's/\x1B\[[0-9;]*[mG]//g'"
}

function sh_mhwd_kernel_l {
	sh_html 'mhwd-kernel -l' "mhwd-kernel -l | sed 's/\x1B\[[0-9;]*[mG]//g'"
}

function sh_mhwd_kernel_li {
	sh_html 'mhwd-kernel --listinstalled' "mhwd-kernel --listinstalled | sed 's/\x1B\[[0-9;]*[mG]//g'"
}

function sh_cpu {
	sh_html 'SYSTEM' "inxi -b -a --color"
}

function sh_report {
	if [[ -s "$FILE_TO_SAVE" ]] ; then
		sh_progress
		if kdialog --warningcontinuecancel $"O relatório foi salvo no arquivo: $FILE_TO_SAVE
Deseja visualizar o relatório agora?
		"  --continue-label $"Visualizar o relatório" --cancel-label $"Fechar" --title $"Hardinfo Relatory"; then
		    xdg-open "$FILE_TO_SAVE"
		fi
		echo "File saved in $FILE_TO_SAVE"
	fi
}

#sh_debug
kdialog --title "$APP" --msgbox "Aguarde, gerando o relatório..." &
progress_pid=$!
sh_config
sh_style
sh_cpu
sh_lspci_special
sh_lsusb
sh_lspci
sh_lsusb_v
sh_fstab
sh_lsmod
sh_ip_address
sh_ip_route
sh_ip_link
sh_ping_route
sh_rfkill
sh_nmcli
sh_mhwd_l
sh_mhwd_li
sh_mhwd_lh
sh_mhwd_kernel_l
sh_mhwd_kernel_li
kill $progress_pid
sh_report
