#!/bin/sh
# Before running this script please ensure that your PATH is
# typical as you use for compilation/istallation. I use
# /bin /sbin /usr/bin /usr/sbin /usr/local/bin, but it may
# differ on your system.

PATH=/sbin:/usr/sbin:/bin:/usr/bin:$PATH

tst_cmd_available()
{
	if type command > /dev/null 2>&1; then
		command -v $1 > /dev/null 2>&1 || return 1
	else
		which $1 > /dev/null 2>&1
		if [ $? -eq 0 ]; then
			return 0
		else
			return 1
		fi
	fi
}

tst_cmd_run()
{
	local cmd="$1"
	shift
	tst_cmd_available $cmd && eval "$cmd $@"
}

is_enabled()
{
	[ -f "$1" ] && [ "$(cat $1)" = "Y" -o "$(cat $1)" = "1" ]
}

echo 'If some fields are empty or look unusual you may have an old version.'
echo 'Compare to the current minimal requirements in Documentation/Changes.'

echo
echo "`ls /etc/*release 2> /dev/null`"
cat /etc/*release 2> /dev/null
if [ $? = 1 ]; then
   echo "`ls /usr/lib/*release 2> /dev/null`"
   cat /usr/lib/*release 2> /dev/null
fi
if [ $? = 1 ]; then
   echo '/etc/issue:'
   cat /etc/issue 2> /dev/null
fi
if [ $? = 1 ]; then
	echo 'lsb_release -a'
	lsb_release -a 2> /dev/null
fi

echo
echo 'uname:'
uname -a

echo
echo '/proc/cmdline'
cat /proc/cmdline

echo

echo "Gnu C                 " `gcc --version 2>/dev/null | head -n 1`
echo "Clang                 " `clang --version 2>/dev/null | head -n 1`

make --version 2>&1 | awk -F, '{print $1}' | awk \
      '/GNU Make/{print "Gnu make              ",$NF}'

ld -v 2>&1 | awk -F\) '{print $1}' | awk \
      '/BFD/{print "binutils              ",$NF}'

mkswap -V 2>&1 | awk '{print "util-linux            ", $NF}'

mount --version 2>&1 | awk '{$1=$2=""; print "mount               ", $0}'

insmod -V  2>&1 | awk 'NR==1 {print "modutils              ",$NF}'

bcachefs version 2>&1 | grep "^[0-9]"  | awk \
'NR==1 {print "bcachefs              ", $1}'

mkfs.btrfs -V 2>&1 | grep "^mkfs.btrfs" | sed 's/,//' | awk \
'NR==1 {print "btrfs                 ", $5}'

tune2fs 2>&1 | grep "^tune2fs" | sed 's/,//' | awk \
'NR==1 {print "e2fsprogs             ", $2}'

mkfs.exfat -V 2>&1 | grep "^exfatprogs" | sed 's/,//' | awk \
'NR==1 {print "exfat                 ", $4}'

mkfs.ntfs -V 2>&1 | grep "^mkntfs" | sed 's/,//' | awk \
'NR==1 {$1="";print "ntfs                 ", $0}'

mkfs.vfat 2>&1 | grep "^mkfs\." | sed 's/,//' | awk \
'NR==1 {print "vfat                  ", $2}'

mkfs.xfs -V 2>&1 | grep "^mkfs.xfs" | sed 's/,//' | awk \
'NR==1 {print "xfs                   ", $3}'

cardmgr -V 2>&1| grep version | awk \
'NR==1{print "pcmcia-cs             ", $3}'

isdnctrl 2>&1 | grep version | awk \
'NR==1{print "isdn4k-utils          ", $NF}'

printf "Linux C Library        $($(ldd /bin/sh | \
awk '/libc/{print $3}') 2>&1 | \
grep -i -e libc.*version -e musl.*libc -e ^version)\n"

ldd -v > /dev/null 2>&1 && ldd -v || ldd --version |head -n 1 | awk \
'NR==1{print "Dynamic linker (ldd)  ", $NF}'

ls -l /usr/lib/lib{g,stdc}++.so  2>/dev/null | awk -F. \
       '{print "Linux C++ Library      " $4"."$5"."$6}'

ps --version 2>&1 | awk 'NR==1{print "Procps                ", $NF}'

ifconfig --version 2>&1 | grep tools | awk \
'NR==1{print "Net-tools             ", $NF}'

ip -V 2>&1 | awk \
'NR==1{print "iproute2              ", $NF}'

ping -V 2>&1 | awk \
'NR==1{print "iputils               ", $NF}'

ethtool --version | grep version 2>&1 | awk \
'NR==1{print "ethtool               ", $NF}'

# Kbd needs 'loadkeys -h',
loadkeys -h 2>&1 | awk \
'(NR==1 && ($3 !~ /option/)) {print "Kbd                   ", $3}'

# while console-tools needs 'loadkeys -V'.
loadkeys -V 2>&1 | awk \
'(NR==1 && ($2 ~ /console-tools/)) {print "Console-tools         ", $3}'

expr --v 2>&1 | awk 'NR==1{print "Sh-utils              ", $NF}'

echo
if [ -e /proc/modules ]; then
    X=`cat /proc/modules | sed -e "s/ .*$//"`
    echo "Modules Loaded         "$X
fi

echo
echo 'cpuinfo:'
tst_cmd_run lscpu || cat /proc/cpuinfo

echo
echo 'free reports:'
free

echo
echo 'memory (/proc/meminfo):'
cat /proc/meminfo

echo
echo 'available filesystems:'
echo $(cut -f2 /proc/filesystems | sort -u)

echo
echo 'mounted filesystems (/proc/mounts):'
cat /proc/mounts

echo
echo 'mounted filesystems (df):'
if `df -hT >/dev/null 2>/dev/null`; then
	df -hT
else
	df
fi

echo
echo 'tainted (/proc/sys/kernel/tainted):'
cat /proc/sys/kernel/tainted

echo
printf 'Secure Boot: '
if tst_cmd_available mokutil; then
	mokutil --sb-state
elif [ -f /sys/firmware/efi/efivars/SecureBoot* ]; then
	val=$(od --address-radix=n --format=u1 /sys/firmware/efi/efivars/SecureBoot* | awk 'NR==1 {print $5}')
	if [ "$val" = "0" ]; then
		echo 'disabled'
	elif [ "$val" = "1" ]; then
		echo 'enabled'
	else
		echo 'failed to detect, tying dmesg'
		dmesg | grep -i secure.*boot
	fi
elif [ ! -f /sys/firmware/efi/efivars/SecureBoot* ]; then
	echo 'EFI variables not supported on SUT'
fi
tst_cmd_run 'dmesg | grep -i secure.*boot'

echo
echo 'Lockdown (/sys/kernel/security/lockdown):'
cat /sys/kernel/security/lockdown

echo
printf "AppArmor: "
if is_enabled /sys/module/apparmor/parameters/enabled; then
	echo 'enabled'
	tst_cmd_run aa-status
else
	echo 'disabled'
fi

echo
if ! tst_cmd_run sestatus; then
	printf 'SELinux mode: '
	tst_cmd_run getenforce || echo 'unknown'
fi
