#!/bin/bash
# -*- ENCODING: UTF-8 -*-

# picainfo
#
# Collect hardware and operating system information that is useful to seek solutions to problems.
#
# Copyright (c) 2013-2026: Alexis Puente Montiel   < pica (a) picalibre.org >
#
# Licensed according to GNU AGPL version 3.0.
#
# It is libre/free software; you can use, redistribute and/or modify it according to the terms of GNU AGPL as published by GNU, version 3.0, 19 November 2007.
#
# It is distributed in the hope that it will be useful, but without any warranty. Read GNU AGPL version 3.0 for additional details.
#
# A copy of GNU AGPL version 3.0 is available at /usr/share/doc/<software-package-name>/agpl-3.0.txt (additionally on Internet as text at https://www.gnu.org/licenses/agpl-3.0.txt and as HTML at https://www.gnu.org/licenses/agpl-3.0-standalone.html ).
#
# Note: Additionally to the official e-mails, picalibre.org is strictly the only official site for this software project, please consider using it to download, report bugs and contribute.
#
# Depends: bash, coreutils, dialog | yad | yad-pica | zenity, gawk | mawk | original-awk, grep, gzip, sed, tar
# Recommends: menu, pica-skel, x11-xkb-utils
# Suggests: alsa-utils | pulseaudio, dpkg, dstat, findutils, hddtemp | lm-sensors, hostname, inetutils-ping | iputils-ping, iotop, iproute2 | net-tools, kmod, lshw, pciutils, procps, rfkill, sysvinit-utils (<<2.88dsf-59.3) | init-system-helpers, usbutils, util-linux, util-linux (<<2.29.2-3) | fdisk


### SCRIPT VARIABLES ########################################

CNAME="picainfo"
VERSION="1.3.1"
TITLE="PicaInfo"
ICON="/usr/share/icons/picainfo.png"

# Translations
if [ "$LANG" = "" ] ; then export $(cat /etc/default/locale | grep -a 'LANG=') ; fi
TEXTDOMAIN=picainfo
TEXTDOMAINDIR=/usr/share/locale/

if [ -e /tmp/picainfo.txt ] ; then
source /tmp/picainfo.txt
echo "HOMEDIR="$HOMEDIR
echo "COLDIR="$COLDIR
fi

# Write errors to log
ERRORLOG="$HOME/.${CNAME}.log"
if [ -e "$ERRORLOG" ] ; then
	mv -f $ERRORLOG ${ERRORLOG}.ant
fi
if [ -e "$ERRORLOG" ] ; then rm -rf "$ERRORLOG" ; fi

for i in /etc/pica-global.dist /etc/pica-global.orig /etc/pica-global /etc/pica-global.local ~/.pica-global ~/pica-global ; do
	if [ -f "$i" ] ; then
	cat "$i"
	source "$i"
	source <(cat $i | sed -e "s/=\(YES\|Yes\|yes\|y\|SÍ\|SI\|Sí\|Si\|sí\|si\|S\|s\)/=Y/g" -e "s/=\(No\|no\|n\)/=N/g" -e "s/=\"\(YES\|Yes\|yes\|y\|SÍ\|SI\|Sí\|Si\|sí\|si\|S\|s\)\"/=Y/g" -e "s/=\"\(No\|no\|n\)\"/=N/g")
	fi
done
if [ "$DEBUG" = "Y" ] ; then
	set -xv
	DEBUG="Y"
else
	ERRORLOG="/tmp/.${CNAME}_$(id -nu).log"
	if [ -e "$ERRORLOG" ] ; then mv -f $ERRORLOG ${ERRORLOG}.ant ; fi
	if [ -e "$ERRORLOG" ] ; then rm -rf "$ERRORLOG" ; fi
fi
if [ "$DEBUG" = "" ] ; then DEBUG="N" ; fi
if [ "$DEBUG" != "N" ] ; then
exec > >(tee -a "$ERRORLOG") 2>&1
echo "$0" "$*" >> "$ERRORLOG"
echo "$CNAME" "$VERSION" >> "$ERRORLOG"
echo $(date +%Y-%m-%d_%H:%M:%S) $"Start" >> "$ERRORLOG"
echo "env:" >> "$ERRORLOG"
env >> "$ERRORLOG"
echo "set:" >> "$ERRORLOG"
set >> "$ERRORLOG"
#else
#exec 2>>"$ERRORLOG"
fi

# Description:
BDESCRIP=$"Collect hardware and operating system information that is useful to seek solutions to problems."
LDESCRIP=$"$TITLE is a tool to collect hardware and operating system information that is useful to seek solutions to problems."

# Documentation:
docu_info () {
echo "$CNAME ($VERSION) - $BDESCRIP"
echo 
echo $"Usage:" $CNAME [$"OPTIONS"]
echo 
echo $"Options:"
echo -e "$ODESCRIP"
echo 
echo $"'man $CNAME' for more information."
echo 
}
ODESCRIP=" -t""\t"$"Use terminal interface.""\n"" -k""\t"$"Use 'kdialog' if possible.""\n"" -y""\t"$"Use 'yad' if possible (by default in graphical sessions).""\n"" -z""\t"$"Use 'zenity' if possible.""\n"" -x""\t"$"Show help documentation."

while getopts tkyzx OPTION ; do
	case $OPTION in
		t )   T="Y" ; G="N"      ;;
		k )   SDIALOG="kdialog"  ;;
		y )   SDIALOG="yad"      ;;
		z )   SDIALOG="zenity"   ;;
		x )   docu_info ; exit 0 ;;
	esac
done


### DIALOG ########################################

#DEFAULTDIALOGLIST="yad zenity kdialog"
DEFAULTDIALOGLIST="yad zenity"

#if [ -t 0 ] ; then
if [ "$(setxkbmap 1>/dev/null 2>&1 ; echo $? )" -ne 0 ] ; then
	G="N" ; T="Y"
fi

if [ "$G" != "N" ] ; then
	
	if   [ "$SDIALOG" = "kdialog" ] ; then
		DIALOGLIST="kdialog yad zenity"
	elif [ "$SDIALOG" = "yad" ] ; then
		DIALOGLIST="yad zenity kdialog"
	elif [ "$SDIALOG" = "zenity" ] ; then
		DIALOGLIST="zenity yad kdialog"
	else
		DIALOGLIST="$DEFAULTDIALOGLIST"
	fi
	
	if [ "$DIALOGLIST" = "" ] ; then
		if [ "$DEFAULTDIALOGLIST" != "" ] ; then
			DIALOGLIST="$DEFAULTDIALOGLIST"
		else
			DIALOGLIST="yad zenity kdialog"
		fi
	fi
	
	if [ "$(command -v $DIALOGLIST)" = "" ] ; then
		G="N" ; T="Y"
	fi

fi


if [ "$G" = "N" ] ; then

	if [ "$(command -v dialog)" = "" ] ; then
		echo $"ERROR: No supported dialog is installed. $TITLE cannot continue."
		exit 1
	else
		
		DIALOG="dialog --cr-wrap --keep-window --output-fd 1"
		#DIALOG="dialog --cr-wrap --keep-window --output-fd 1 --no-lines --no-shadow"
		if [ "$TITLE" != "" ] ; then
			WICON="--backtitle $(echo $TITLE | sed 's| |-|g')"
		fi
		WTITLE="--title"
		WTEXT=""
		H="0"
		W="0"
		LH="0"
		CALENDAR="--calendar"
		DATEFORMAT="--date-format %Y-%m-%d"
		WTIME="--timebox"
		TIMEFORMAT="--time-format %H:%M:%S"
		DSEL="--dselect"
		FSEL="--fselect"
		WRITE="--inputbox"
		PREWRITE=""
		COLUMN=""
		PRINTCOLUMN1=""
		PRINTCOLUMN2=""
		HIDECOLUMN1=""
		HIDECOLUMN2=""
		#INPUTSEPARATOR="--column-separator"
		#OUTPUTSEPARATOR="--separate-output"
		MONOLIST="--menu"
		#MONOLIST="--radiolist"
		MULTIPLELIST="--separate-output --checklist"
		PRE=""
		POST="off"
		PRES=""
		POSTS="on"

		INFO="--msgbox"
		WAIT="--infobox"

		QYN="--yesno"
		QYNC="--yesno"
		WYN="--yesno"
		WCC="--yesno"
		WYNC="--yesno"
		WM="--msgbox"
		ERROR="--msgbox"

		FORM="--form"
		FORMT=""
		FORMSELPRE=""
		FORMSELPOST=""
		#FORMCALPRE=""
		#FORMCALPOST=""
		#FORMCLPRE=""
		#FORMCLPOST=""
		PRELAB1="1 1"
		POSTLAB1="1 10 999 0"
		PRELAB2="2 1"
		POSTLAB2="2 10 999 0"
		PRELAB3="3 1"
		POSTLAB3="3 10 999 0"

		YES="--yes-label"
		NO="--no-label"
		OK="--ok-label"
		CONTINUE="--ok-label"
		CANCEL="--cancel-label"
		EXTRABUTTON="--extra-button --extra-label"
	
	fi

else

	for i in $DIALOGLIST ; do

		if [ "$(command -v $i)" != "" ] ; then
	
			if [ "$i" = "yad" ] ; then
		
				if [ "$(yad --version | tail -n 1 | cut -d ' ' -f 1 | cut -d '.' -f 1)" -lt 2 ] ; then
					DIALOG="yad --center"  # --selectable-labels (bug in Yad < 2.0) https://github.com/v1cont/yad/blob/master/NEWS
				else
					DIALOG="yad --center --selectable-labels"  # --on-top
				fi
				if [ "$ICON" != "" ] ; then
					WICON="--window-icon $ICON"
				fi
				WTITLE="--title"
				WTEXT="--text"
				H=""
				if [ "$(yad --version | tail -n 1 | grep -aE ^'7.2')" != "" ] ; then
					W="--width=640"
				else
					W="--width=640 --text-width=640 --fixed"  # https://github.com/v1cont/yad/issues/107  https://github.com/v1cont/yad/issues/140
				fi
				LH=""
				CALENDAR="--calendar"
				DATEFORMAT="--date-format %Y-%m-%d"
				WTIME="--separator=\n --columns 3 --form"
				#TIMEFORMAT="--time-format %H:%M:%S"
				DSEL="--file --directory --filename"  # Deleted *-selection alias for --file in Yad 4.0
				FSEL="--file --filename"              # Deleted *-selection alias for --file in Yad 4.0
				WRITE="--entry"
				PREWRITE="--entry-text"
				COLUMN="--column="
				PRINTCOLUMN1="--print-column=01"
				PRINTCOLUMN2="--print-column=02"
				HIDECOLUMN1="--hide-column=01"
				HIDECOLUMN2="--hide-column=02"
				#INPUTSEPARATOR="--item-separator"
				#OUTPUTSEPARATOR="--separator="
				#MONOLIST="--entry"
				MONOLIST="--separator=\n --list --search-column=1 --no-headers"
				MONOLISTH="--separator=\n --list --search-column=1"
				MULTIPLELIST="--separator= --list --checklist --search-column=2 --no-headers"
				MULTIPLELISTH="--separator= --list --checklist --search-column=2"
				PRE="FALSE"
				POST=""
				PRES="TRUE"
				POSTS=""
				# standard gtk keybindings for next and previous search are ctrl+g and ctrl+shift+g while you can redefine them in gtk-keys.css for your theme  # https://github.com/v1cont/yad/issues/127
			
				INFO="--image=/usr/share/icons/picalibre/rm/play.png --button="$"OK"
				WAIT="--image=/usr/share/icons/picalibre/rm/play.png --no-buttons"
			
				QYN="--image=/usr/share/icons/picalibre/rm/question.png --button="$"Yes"":0 --button="$"No"":1"
				QYNC="--image=/usr/share/icons/picalibre/rm/question.png --button="$"Yes"":0 --button="$"No"":1 --button="$"Cancel"":1"
				WYN="--image=/usr/share/icons/picalibre/rm/warning.png --button="$"Yes"":0 --button="$"No"":1"
				WCC="--image=/usr/share/icons/picalibre/rm/warning.png --button="$"Continue"":0 --button="$"Cancel"":1"
				WYNC="--image=/usr/share/icons/picalibre/rm/warning.png --button="$"Yes"":0 --button="$"No"":1 --button="$"Cancel"":1"
				WM="--image=/usr/share/icons/picalibre/rm/warning.png --button="$"OK"
				ERROR="--image=/usr/share/icons/picalibre/rm/error.png --button="$"OK"
			
				FORM="--separator=\n --form"
				FORMT="--field"
				FORMSELPRE="--field"
				FORMSELPOST=":FL"
				FORMCALPRE="--field"
				FORMCALPOST=":DT"
				FORMCLPRE="--field"
				FORMCLPOST=":CB"
				PRELAB1=""
				POSTLAB1=""
				PRELAB2=""
				POSTLAB2=""
				PRELAB3=""
				POSTLAB3=""
			
				YES="--button"
				NO="--button"
				OK="--button"
				CONTINUE="--button"
				CANCEL="--button"
				EXTRABUTTON="--button"
		
			elif [ "$i" = "zenity" ] ; then
		
				DIALOG="zenity"
				if [ "$ICON" != "" ] ; then
					WICON="--window-icon $ICON"
				fi
				WTITLE="--title"
				WTEXT="--text"
				H=""
				W="--width=640"
				LH=""
				CALENDAR="--calendar"
				DATEFORMAT="--date-format %Y-%m-%d"
				WTIME="--separator : --forms"
				#TIMEFORMAT="--time-format %H:%M:%S"
				DSEL="--file-selection --directory --filename"
				FSEL="--file-selection --filename"
				WRITE="--entry"
				PREWRITE="--entry-text"
				COLUMN="--column="
				PRINTCOLUMN1="--print-column=01"
				PRINTCOLUMN2="--print-column=02"
				HIDECOLUMN1="--hide-column=01"
				HIDECOLUMN2="--hide-column=02"
				#INPUTSEPARATOR=""
				#OUTPUTSEPARATOR="--separator=\n"
				MONOLIST="--separator=\n --list --hide-header"
				MONOLISTH="--separator=\n --list"
				MULTIPLELIST="--separator=\n --list --checklist --hide-header"
				MULTIPLELISTH="--separator=\n --list --checklist"
				PRE="FALSE"
				POST=""
				PRES="TRUE"
				POSTS=""
			
				INFO="--info"
				WAIT="--info"
			
				QYN="--question"
				QYNC="--question"
				WYN="--question"
				WCC="--question"
				WYNC="--question"
				WM="--warning"
				ERROR="--error"
			
				FORM="--forms"
				FORMT="--add-entry"
				FORMSELPRE="--add-entry"
				FORMSELPOST=""
				FORMCALPRE="--add-calendar"
				FORMCALPOST=""
				if [ "$(zenity --version | cut -d '.' -f 1 )" -ge 3 ] || [ "$(zenity --version | sed 's|\.||g')" -ge 3102 ] ; then
					FORMCLPOST="--combo-values"
					FORMCLPRE="--add-combo"
				fi
				PRELAB1=""
				POSTLAB1=""
				PRELAB2=""
				POSTLAB2=""
				PRELAB3=""
				POSTLAB3=""
			
				# Zenity <3 does not support custom buttons.
				if [ "$(zenity --version | cut -d '.' -f 1 )" -ge 3 ] ; then
					YES="--ok-label"
					NO="--cancel-label"
					OK="--ok-label"
					CONTINUE="--ok-label"
					CANCEL="--cancel-label"
					#EXTRABUTTON=""
				fi
		
#			elif [ "$i" = "kdialog" ] ; then
#		
#				# CAUTION: All 'kdialog' (TDE, KDE Plasma 4, KDE Plasma 5, KDE Plasma 6,...) are included here!
#			
#				DIALOG="kdialog"
#				if [ "$ICON" != "" ] ; then
#					WICON="--icon $ICON"
#				fi
#				WTITLE="--title"
#				WTEXT=""
#				H=""
#				W=""
#				LH=""
#				# No "--calendar" in TDE-kdialog + No "--date-format" both TDE and Plasma = There is no usable version of kdialog for calendars
#				#CALENDAR="--calendar"
#				#DATEFORMAT="--date-format %Y-%m-%d"
#				#WTIME="--timebox"
#				#TIMEFORMAT="--time-format %H:%M:%S"
#				DSEL="--getexistingdirectory"
#				FSEL="--getsavefilename"
#				#FSEL="--getopenfilename"
#				WRITE="--inputbox"
#				PREWRITE=""
#				COLUMN=""
#				PRINTCOLUMN1=""
#				PRINTCOLUMN2=""
#				HIDECOLUMN1=""
#				HIDECOLUMN2=""
#				#INPUTSEPARATOR=""
#				#OUTPUTSEPARATOR="--separate-output"
#				MONOLIST="--menu"
#				#MONOLIST="--combobox"
#				#MONOLIST="--radiolist"
#				MULTIPLELIST="--separate-output --checklist"
#				PRE=""
#				POST="off"
#				PRES=""
#				POSTS="on"
#			
#				INFO="--msgbox"
#				WAIT="--msgbox"
#			
#				QYN="--yesno"
#				QYNC="--yesnocancel"
#				WYN="--warningyesno"
#				WCC="--warningcontinuecancel"
#				WYNC="--warningyesnocancel"
#				WM="--sorry"
#				ERROR="--error"
#			
#				# No "--form" in any kdialog version
#				#FORM="--form"
#				#FORMT=""
#				#FORMSELPRE=""
#				#FORMSELPOST=""
#				#FORMCALPRE=""
#				#FORMCALPOST=""
#				#FORMCLPRE=""
#				#FORMCLPOST=""
#				#PRELAB1=""
#				#POSTLAB1=""
#				#PRELAB2=""
#				#POSTLAB2=""
#				#PRELAB3=""
#				#POSTLAB3=""
#			
#				# TDE-kdialog does not support custom buttons.
#				if [ "$(kdialog --version | grep -a Qt | grep -aEo "[0-9]" | head -n 1)" -ge 4 ] || [ "$(kdialog --version | grep -aEi kdialog | grep -aEo "[0-9]" | head -n 1)" -gt 1 ] ; then
#					YES="--yes-label"
#					NO="--no-label"
#					OK="--continue-label"
#					CONTINUE="--continue-label"
#					CANCEL="--cancel-label"
#					EXTRABUTTON="--cancel-label"
#				fi
		
			else
		
				true
		
			fi
		
		if [ "$DIALOG" != "" ] ; then break ; fi
		
		fi

	done

fi

if [ "$DIALOG" = "" ] ; then
	echo $"ERROR: No supported dialog is installed. $TITLE cannot continue."
	exit 1
fi


### ROOT CHECK ########################################

#if [ "$(cat /etc/passwd | grep -aE ^$(whoami): | cut -d ':' -f 3 )" -ne 0 ] ; then
if [ "$(id -u)" -ne 0 ] ; then
	$DIALOG $WICON $WTITLE "$TITLE" $ERROR $WTEXT $"$TITLE must be launched as SuperUser.\n\nCannot continue." $H $W
	exit 1
fi
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/trinity/bin


### BASICFUN ########################################

check_exit () {

TAILERRORLOG=$(cat "$ERRORLOG" | grep -aE "[a-zA-Z0-9\=\[]" | tail -n 10 )

echo $(date +%Y-%m-%d_%H:%M:%S)

$DIALOG $WICON $WTITLE "$TITLE" $ERROR $WTEXT $"Stop due to error.\n\n$TAILERRORLOG\n" $H $W

exit 1

}

# "try again or exit" dialog window
input_error () {

echo $(date +%Y-%m-%d_%H:%M:%S)

$DIALOG $WICON $WTITLE "$TITLE" $WYN   $WTEXT $"Something went wrong.\n\nDo you want to try again?" $H $W

if [ "$?" -ne 0 ] ; then
	exit 1
else
	$REFUN
fi

}


### WELCOME ########################################
#
#if [ "$WELCOME" != "N" ] ; then
#
#$DIALOG $WICON $WTITLE "$TITLE" $INFO  $WTEXT $"Welcome to $TITLE.\n\n$TITLE is a tool to collect hardware and operating system information that is useful to seek solutions to problems.\n\n$TITLE is libre/free software, licensed according to 'GNU AGPL version 3.0'.\n\nIt is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY.\n\nMore information at www.picalibre.org\n" $H $W
#
##[ "$?" -eq 0 ] || { exit 0 ; }
#if [ "$?" -ne 0 ] ; then
#	exit 0
#fi
#
#fi


### CLEAN ########################################

#if [ -e "$HOME/$CNAME" ] ; then
#	$DIALOG $WICON $WTITLE "$TITLE" $QYN   $WTEXT $"$HOME/$CNAME must be deleted to continue.\n\nDo you want to continue?" $H $W
#
##	[ "$?" -ne 0 ] && exit 0 || { rm -rf "$HOME/$CNAME" ; }
#	if [ "$?" -ne 0 ] ; then
#		exit 0
#	else
#		rm -rf "$HOME/$CNAME"
#	fi
#
#fi


### START PROCESSING WINDOW ########################################
#
##$DIALOG $WICON $WTITLE "$TITLE" $OK " " $WAIT  $WTEXT $"This message will be automatically closed when $TITLE finishes.\n\nPlease wait.\n" $H $W &
#$DIALOG $WICON $WTITLE "$TITLE" $OK " " $WAIT  $WTEXT $"Please wait..." $H $W &
#PIDDIALOG=$( echo $! )


########################################

## Create a collect folder/directory
#HOMEDIR="$HOME"
#COLDIR="$HOME/$CNAME"
#echo "HOMEDIR=\"$HOMEDIR\"" | tee -a /tmp/picainfo.txt
#echo "COLDIR=\"$COLDIR\"" | tee -a /tmp/picainfo.txt
#if [ -e $COLDIR ] ; then
#	mv -f $COLDIR ${COLDIR}.ant
#fi
#rm -rf $COLDIR
#mkdir -pv "$COLDIR"


### AS ROOT ########################################

as_root_user () {

mkdir -pv ${COLDIR}/root

echo -e "* User:\t\t" $(whoami) >> ${COLDIR}/root/intro.txt
echo -e "* Groups:\t" $(groups) >> ${COLDIR}/root/intro.txt
echo -e "* id:\t" $(id) >> ${COLDIR}/root/intro.txt
if [ "$WAYLAND_DISPLAY" = "" ] ; then echo "* X $DISPLAY ( $XDG_SESSION_TYPE )" >> ${COLDIR}/root/intro.txt ; else echo "* Wayland $WAYLAND_DISPLAY ( $XDG_SESSION_TYPE )" >> ${COLDIR}/root/intro.txt ; fi
echo -e "* DM (DS):\t" $DESKTOP_SESSION >> ${COLDIR}/root/intro.txt
echo -e "* DM (top):\t" $(top -n 1 -b | awk '{print $12}' | grep -aEi dm | sort -u | tr "\n" " ") >> ${COLDIR}/root/intro.txt
echo -e "* DM (pid):\t" $(find /var/run/ | grep -aEi "\.pid$" | grep -aEi 'dm') >> ${COLDIR}/root/intro.txt
echo -e "* DE:\t\t" $(echo $XDG_CURRENT_DESKTOP $(top -n 1 -b | awk '{print $12}' | grep -a start | sed 's|start||g' | tr "\n" " ")) >> ${COLDIR}/root/intro.txt
echo -e "* Kernel:\t" $(uname -mr) >> ${COLDIR}/root/intro.txt
echo -e "* SO:\t" >> ${COLDIR}/root/intro.txt
lsb_release -a >> ${COLDIR}/root/intro.txt
cat /etc/os-release >> ${COLDIR}/root/intro.txt
cat /etc/issue.net >> ${COLDIR}/root/intro.txt
cat /etc/picalibre_version >> ${COLDIR}/root/intro.txt
cat /etc/debian_version >> ${COLDIR}/root/intro.txt
cat /etc/devuan_version >> ${COLDIR}/root/intro.txt
echo -e " " >> ${COLDIR}/root/intro.txt
hostname >> ${COLDIR}/root/intro.txt
cat /etc/hostname >> ${COLDIR}/root/intro.txt
echo -e " " >> ${COLDIR}/root/intro.txt
echo -e " " >> ${COLDIR}/root/intro.txt
find /sys/devices/ -type f | grep -aEi 'bat|power' | grep -a uevent | while read i ; do echo ___$i ; cat $i ; done | tee -a ${COLDIR}/root/intro.txt
echo $(cat /sys/class/dmi/id/sys_vendor | awk '{print $1}' | tr "\n" " " ; cat /sys/class/dmi/id/product_name) >> ${COLDIR}/root/intro.txt
echo -e "* Processors:\t" >> ${COLDIR}/root/intro.txt
cat /proc/cpuinfo | grep -aEi 'processor|model name' >> ${COLDIR}/root/intro.txt
echo -e "* Mem (MB):\t" $(free -m | grep -aEi "^Mem" | awk '{print $2}') >> ${COLDIR}/root/intro.txt
echo -e "* Disks:" >> ${COLDIR}/root/intro.txt
lsblk -o NAME,KNAME,RM,SIZE,RO,TYPE,FSTYPE,MOUNTPOINT,LABEL,UUID >> ${COLDIR}/root/intro.txt
echo -e " " >> ${COLDIR}/root/intro.txt
ls -l /dev/disk/by-id/ >> ${COLDIR}/root/intro.txt
echo -e " " >> ${COLDIR}/root/intro.txt
echo -e "* Disk space:" >> ${COLDIR}/root/intro.txt
df -h | tee -a "$ERRORLOG" | grep -aEiv "[0-9]" | head -n 1 >> ${COLDIR}/root/intro.txt
df -h | tee -a "$ERRORLOG" | grep -aEi "^/dev/" >> ${COLDIR}/root/intro.txt
echo -e " " >> ${COLDIR}/root/intro.txt
df -i | tee -a "$ERRORLOG" | grep -aEiv "[0-9]" | head -n 1 >> ${COLDIR}/root/intro.txt
df -i | tee -a "$ERRORLOG" | grep -aEi "^/dev/" >> ${COLDIR}/root/intro.txt
echo -e " " >> ${COLDIR}/root/intro.txt
echo -e "* Permissions:" >> ${COLDIR}/root/intro.txt
ls -FAlh --full-time / >> ${COLDIR}/root/intro.txt
echo -e " " >> ${COLDIR}/root/intro.txt
ls -FAlh --full-time /home/ >> ${COLDIR}/root/intro.txt

cp -a -L $HOME/.xsession ${COLDIR}/root/
cp -a -L $HOME/.xsession-errors ${COLDIR}/root/
cp -a -L /etc/X11/xorg.conf ${COLDIR}/root/

lshw >> ${COLDIR}/root/lshw.txt
lspci -nn -k >> ${COLDIR}/root/lspci.txt
lsusb >> ${COLDIR}/root/lsusb.txt
lsusb -t >> ${COLDIR}/root/lsusb-t.txt
dmesg >> ${COLDIR}/root/dmesg.txt
find /proc -type f | grep -aEiv "^/proc/[0-9]" | while read f ; do if [ "$(timeout 1 grep -a -c ^ $f)" -eq 1 ] ; then printf "$f""\t""$(cat $f)""\n" ; fi ; done >> ${COLDIR}/root/proc.txt
for i in $(ls /sys/class) ; do for j in $(ls /sys/class/$i) ; do DRIVER=$(readlink -m /sys/class/$i/$j/device/driver | awk -F '/' '{print $NF}') ; printf "$i/$j\t$DRIVER\n" >> ${COLDIR}/root/driver-sys-class.txt ; if [ "$DRIVER" != "" ] ; then printf "\n\nDRIVER=$DRIVER\n" >> ${COLDIR}/root/driver-modinfo.txt ; modinfo $DRIVER >> ${COLDIR}/root/driver-modinfo.txt ; fi ; done ; done
top -n 1 -b >> ${COLDIR}/root/top.txt
ps aux >> ${COLDIR}/root/psaux.txt
iotop -bktq -n 1 >> ${COLDIR}/root/iotop1.txt
dstat -c -d -g -i -l -m -n -p -r -s -t -y 1 4 >> ${COLDIR}/root/dstatT.txt
dstat -fv 1 4 >> ${COLDIR}/root/dstatFV.txt
sensors >> ${COLDIR}/root/sensors.txt
hddtemp /dev/sd* >> ${COLDIR}/root/hddtemp.txt
ls -l /dev >> ${COLDIR}/root/lsdev.txt
lsmod >> ${COLDIR}/root/lsmod.txt
service --status-all >> ${COLDIR}/root/service.txt

cp -a -L /var/lib/alsa/asound.state ${COLDIR}/root/var_lib_alsa_asound.state
cp -a -L $HOME/.config/asound.state ${COLDIR}/root/home_config_asound.state
cp -a -L $HOME/.asoundrc ${COLDIR}/root/home_asoundrc
aplay -l >> ${COLDIR}/root/aplay_l.txt
arecord -l >> ${COLDIR}/root/arecord_l.txt
pactl info >> ${COLDIR}/root/pactl.txt
pactl list >> ${COLDIR}/root/pactl.txt
pulsemixer -l >> ${COLDIR}/root/pulsemixer_l.txt

free -m >> ${COLDIR}/root/ram.txt
df -h | tee -a "$ERRORLOG" >> ${COLDIR}/root/df.txt
df -i | tee -a "$ERRORLOG" >> ${COLDIR}/root/df_i.txt
w >> ${COLDIR}/root/w.txt

#ls /proc/ | grep -aEiv "^[0-9]" | while read i ; do echo _______________/proc/"$i" >> ${COLDIR}/root/proc.txt ; cat /proc/"$i" >> ${COLDIR}/root/proc.txt ; done
#ls /proc/ | grep -aEiv "^[0-9]" | while read i ; do cp -a -L /proc/"$i" >> ${COLDIR}/root/proc_"$i".txt ; done

cp -a -L /etc/fstab ${COLDIR}/root/
cp -a -L /etc/mtab ${COLDIR}/root/
blkid >> ${COLDIR}/root/blkid.txt
blkid -o list >> ${COLDIR}/root/blkid_list.txt
lsblk -o NAME,KNAME,RM,SIZE,RO,TYPE,FSTYPE,MOUNTPOINT,LABEL,UUID >> ${COLDIR}/root/lsblk.txt
lsblk -r -n -o KNAME,RM,TYPE | grep -a ' disk' | awk '{print $1}' | while read i ; do parted -s /dev/$i print >> ${COLDIR}/root/parted.txt ; done
lsblk -r -n -o KNAME,RM,TYPE | grep -a ' disk' | awk '{print $1}' | while read i ; do parted -s /dev/$i print free >> ${COLDIR}/root/parted_f.txt ; done
lsblk -r -n -o KNAME,RM,TYPE | grep -a ' disk' | awk '{print $1}' | while read i ; do sfdisk -l /dev/$i >> ${COLDIR}/root/sfdisk_l.txt ; done
lsblk -r -n -o KNAME,RM,TYPE | grep -a ' disk' | awk '{print $1}' | while read i ; do sfdisk -F /dev/$i >> ${COLDIR}/root/sfdisk_F.txt ; done
ls -l /dev/disk/by-id/ >> ${COLDIR}/root/by-id.txt

(ping -c 3 ftp.fau.de || ping -c 3 mirrorservice.org || ping -c 3 ftp.ch.debian.org) >> ${COLDIR}/root/ping.txt
hostname -I >> ${COLDIR}/root/hostname.txt
cp -a -L /etc/resolv.conf ${COLDIR}/root/
cp -a -L /etc/network/interfaces ${COLDIR}/root/
mkdir ${COLDIR}/root/etc_NetworkManager_system-connections/
cp -a -L /etc/NetworkManager/system-connections/* ${COLDIR}/root/etc_NetworkManager_system-connections/
ifconfig -a >> ${COLDIR}/root/ifconfig.txt
ip a >> ${COLDIR}/root/ip.txt
nmcli dev show >> ${COLDIR}/root/nm_l.txt
nmcli -f DEVICE,SLAVE,TYPE,STATE,ACTIVE,AUTOCONNECT,AUTOCONNECT-PRIORITY,READONLY,NAME,FILENAME connection show >> ${COLDIR}/root/nm_t.txt
rfkill list >> ${COLDIR}/root/rfkill.txt

dpkg -l >> ${COLDIR}/root/dpkg.txt
cp -a -L /etc/apt/ ${COLDIR}/root/

cp -a -L /boot/grub/menu.lst ${COLDIR}/root/
cp -a -L /boot/grub/grub.cfg ${COLDIR}/root/

ls -FAl --full-time / >> ${COLDIR}/root/lsroot.txt
ls -FAl --full-time /tmp/ >> ${COLDIR}/root/lstmp.txt
ls -FAl --full-time --ignore='[!.]*' ${HOMEDIR} >> ${COLDIR}/root/lshome.txt
ls -FAl --full-time --ignore='[!.]*' /root/ >> ${COLDIR}/root/lshomeroot.txt

find /var/log/ -type d | while read d ; do mkdir -pv ${COLDIR}/root/$d ; done
find /var/log/ -type f | while read f ; do if [ "$(stat -c %s $f)" -lt 1000 ] ; then cp -a -L $f ${COLDIR}/root/$f ; else tail -n 20 $f >> ${COLDIR}/root/$f ; fi ; done
tail -n 2000 /var/log/syslog >> ${COLDIR}/root/syslog
#cp -a -L /var/log/apt/history.log ${COLDIR}/root/
#cp -a -L /var/log/apt/term.log ${COLDIR}/root/
#cp -a -L /var/log/dpkg.log ${COLDIR}/root/
#cp -a -L /var/log/syslog ${COLDIR}/root/
#cp -a -L /var/log/messages ${COLDIR}/root/
#cp -a -L /var/log/kern.log ${COLDIR}/root/
#cp -a -L /var/log/daemon.log ${COLDIR}/root/
#cp -a -L /var/log/Xorg.0.log ${COLDIR}/root/
#cp -a -L /var/log/Xorg.1.log ${COLDIR}/root/
#cp -a -L /var/log/Xorg.* ${COLDIR}/root/
#cp -a -L /var/log/user.log ${COLDIR}/root/
#cp -a -L /var/log/dmesg ${COLDIR}/root/
#cp -a -L /var/log/debug ${COLDIR}/root/
#cp -a -L /var/log/boot ${COLDIR}/root/
#cp -a -L /var/log/auth.log ${COLDIR}/root/
#mkdir -pv ${COLDIR}/root/var-log-all
#cp -a -L /var/log/* ${COLDIR}/root/var-log-all/
if [ "$(command -v journalctl)" != "" ] ; then
journalctl -b | tail -n 2000 >> ${COLDIR}/root/journalctl-b.txt
journalctl | tail -n 2000 >> ${COLDIR}/root/journalctl.txt
fi

mkdir ${COLDIR}/root/crash/ ; CON=0 ; for i in $(find /var/crash/ -type f -printf '%TY-%Tm-%Td %TT %TZ|%n|%u|%g|%m|%k|%p|%l\n' | sort -n | column -t -s "|" | awk '{print $NF}') ; do f=$(echo "$i" | sed "s|.*/||g") ; if [ "$(stat -c %s $i)" -lt 1000 ] ; then cp -a -L $i ${COLDIR}/root/crash/$f ; else if [ "$CON" -lt 250000 ] ; then if [ "$(command -v xz)" != "" ] ; then tar -cJvf ${COLDIR}/root/crash/$f.tar.xz $i ; CON=$(expr $CON + "$(stat -c %s ${COLDIR}/root/crash/$f.tar.xz)") ; else tar -czvf ${COLDIR}/root/crash/$f.tar.gz $i ; CON=$(expr $CON + "$(stat -c %s ${COLDIR}/root/crash/$f.tar.gz)") ; fi ; else touch ${COLDIR}/root/crash/$f.excl ; fi ; fi ; done

if [ "$COLDIR" != "" ] ; then
	# chmod 777 "$COLDIR"
	# find "$COLDIR"/ -type f | while read i ; do chmod 666 "$i" ; done
	# find "$COLDIR"/ -type d | while read i ; do chmod 777 "$i" ; done
	for i in $(cat /etc/passwd | awk -F ':' '{print $3,$1}' | sort -n | grep -aEi "00[0-9] " | awk '{print $2}') ; do AUSER=$(users | sed 's| |\n|g' | grep -aE "^$i$" | head -n 1) ; if [ "$AUSER" != "" ] ; then if [ "$(id -nu $i)" != "" ] ; then chown -R $(id -nu $i):$(id -ng $i) "$COLDIR" ; else chown -R 1000:1000 "$COLDIR" ; fi ; break ; fi ; done
fi

}

as_root_user

if [ -e "/tmp/picainfo.txt" ] ; then
	rm -f /tmp/picainfo.txt
fi

exit 0
