#!/bin/bash
# System Info

BOARD_NAME=${CONFIG_BOARD_NAME:-${CONFIG_BOARD}}
MAIN_MENU_TITLE="${BOARD_NAME} | Extended System Information"
export BG_COLOR_MAIN_MENU="normal"

. /etc/functions.sh
. /etc/gui_functions.sh
. /etc/luks-functions.sh
. /tmp/config

TRACE_FUNC

battery_status="$(print_battery_state)"

usb="$(lsusb)"
pci="$(lspci)"

camera="None|Unknown"
if echo "$usb" | grep -s "04f2:b2db"; then camera="Yes"; fi # t430
if echo "$usb" | grep -s "04f2:b2ea"; then camera="Yes"; fi # x230
if echo "$usb" | grep -s "5986:02d2"; then camera="Yes"; fi # x230

bluetooth="None|Unknown"
if echo "$usb" | grep -s "0a5c:21e6"; then bluetooth="BCM20702 Bluetooth 4.0"; fi

wifi="None|Unknown"
if echo "$pci" | grep -s "8086:0085"; then wifi="Intel Centrino Advanced-N 6205"; fi
if echo "$pci" | grep -s "168c:0034"; then wifi="Qualcomm Atheros AR9462"; fi
if echo "$pci" | grep -s "168c:0030"; then wifi="Qualcomm Atheros AR93xx"; fi

mouse="None|Unknown"
if grep -s "TouchPad" /sys/class/input/mouse*/device/name; then mouse="Synaptic TouchPad"; fi

known_devices="$(echo -e "Camera: ${camera}\nBluetooth: ${bluetooth}\nWifi: ${wifi}\nMouse: ${mouse}")"

echo -e "PCI             USB" >/tmp/devices_usb_pci
for l in $(seq 16); do
	row1="$(echo "$pci" | sed -n ${l}p | cut -d " " -f 5)"
	row2="$(echo "$usb" | sed -n ${l}p | cut -d " " -f 6)"
	row3="$(echo "$known_devices" | sed -n ${l}p)"
	echo "${row1}   |   ${row2}    ${row3}" >>/tmp/devices_usb_pci
done

memtotal=$(cat /proc/meminfo | grep 'MemTotal' | tr -s ' ' | cut -f2 -d ' ')
memtotal=$((${memtotal} / 1024 / 1024 + 1))
cpustr=$(cat /proc/cpuinfo | grep 'model name' | uniq | sed -r 's/\(R\)//;s/\(TM\)//;s/CPU //;s/model name.*: //')
kernel=$(uname -s -r)

FB_OPTIONS=""
if whiptail --version | grep "fbwhiptail"; then FB_OPTIONS="--text-size 12"; fi
# make sure we know the EC version too, populate if not already set
if [ -z "$EC_VER" ]; then
	EC_VER=$(ec_version)
fi

# build optional EC_VER line like gui_functions does
ec_ver_line=""
[ -n "$EC_VER" ] && ec_ver_line="\nEC_VER: ${EC_VER}"

# gather disk info via helper (avoids busybox 2TB limit)
disk_info="$(disk_info_sysfs)"

whiptail_type $BG_COLOR_MAIN_MENU $FB_OPTIONS --title 'System Info' \
	--msgbox "${BOARD_NAME}\nFW_VER: ${FW_VER}${ec_ver_line}\nKernel: ${kernel}\nCPU: ${cpustr}  RAM: ${memtotal} GB $battery_status\n${disk_info}\n\n$(cat /tmp/devices_usb_pci)" 0 80
