#!/bin/bash
#########################################################
#                                                       #
# This is BashStyle-NG 7.8                              #
#                                                       #
# Licensed under GNU GENERAL PUBLIC LICENSE v3          #
#                                                       #
# Copyright 2007 - 2009 Christopher Bratusek            #
#                                                       #
#########################################################

white=$(bs-ng-echo $white)
orange=$(bs-ng-echo $orange)
iceblue=$(bs-ng-echo $iceblue)
green=$(bs-ng-echo $green)

source $BSNG_PREFIX/share/bashstyle-ng/rc/functions/check_opt

_help()
{
	echo -e "\n${white}Usage:\n"
	echo -e "${orange}systeminfos ${white}|${green} --cpu\t\t${iceblue} [Display CPU Model and Freq]\
	\n${orange}systeminfos ${white}|${green} --kernel\t${iceblue} [Display Kernel Version, Release and Machine]\
	\n${orange}systeminfos ${white}|${green} --memory\t${iceblue} [Display Total, Free and Used RAM]\
	\n${orange}systeminfos ${white}|${green} --partitions\t${iceblue} [Display Major, Minor, Blocks and Node for all Paritions]\
	\n${orange}systeminfos ${white}|${green} --pci\t\t${iceblue} [Display Infos about all PCI Devices (and their kernel-module)]\
	\n${orange}systeminfos ${white}|${green} --usb\t\t${iceblue} [Display Infos about all USB Devices (and their kernel-module)]\
	\n${orange}systeminfos ${white}|${green} --bios\t${iceblue} [Display SMBIOS DMI Infos]\
	\n${orange}systeminfos ${white}|${green} --mounts\t${iceblue} [Display all mounted devices]\n"
}

_cpu()
{
	echo -e "${white}CPU:\n"
	echo -e "${orange}Model:${iceblue} $(grep "model name" /proc/cpuinfo | sed -e 's/.*: //g')"
	echo -e "${orange}MHz  :${iceblue} $(grep "cpu MHz" /proc/cpuinfo | sed -e 's/.*: //g')\n"
}

_kernel()
{
	echo -e "${white}Kernel:\n"
	echo -e "${orange}Release:${iceblue} $(uname -r)"
	echo -e "${orange}Version:${iceblue} $(uname -v)"
	echo -e "${orange}Machine:${iceblue} $(uname -m)\n"

}

_memory()
{
	echo -e "${white}RAM:\n"
	echo -e "${orange}Total:${iceblue} $(((`showmem --free`) + (`showmem --used`))) MB"
	echo -e "${orange}Free :${iceblue} $(showmem --free) MB"
	echo -e "${orange}Used :${iceblue} $(showmem --used) MB\n"

}

_partitions()
{
	echo -e "${white}Partitions:${orange}\n"
	echo -e "major minor blocks device-node ${iceblue}\
	\n$(cat /proc/partitions | sed -e '1,2d')" | column -t
	echo ""

}

_pci()
{
	echo -e "${white}PCI Devices:\n${iceblue}"
	lspci -vkmm
	echo ""

}

_usb()
{
	echo -e "${white}USB Devices:\n${iceblue}"
	lsusb -v
	echo ""

}

_mounts()
{
	echo -e "${white}Mounts:\n${orange}\
	\ndevice-node on mount-point type filesystem options\n" ${iceblue} "\n\n$(mount)" | column -t
	echo ""

}

_bios()
{
	if [[ $EUID != 0 ]]; then
		echo -e "How to auth as root? (sudo or su)\n"
		read auth

		if [[ $auth == sudo ]]; then
			echo -e "${white}SMBIOS/DMI Infos:${iceblue}\n"
			sudo dmidecode -q

		elif [[ $auth == su ]]; then
			echo -e "${white}SMBIOS/DMI Infos:${iceblue}\n"
			su -c "dmidecode -q"

		else	echo "wrong auth command"
		fi
	else	echo -e "${white}SMBIOS/DMI Infos:${iceblue}\n"
		dmidecode -q

	fi
}

case $1 in
	*cpu*)
		_cpu
	;;

	*kernel*)
		_kernel
	;;

	*mem*)
		_memory
	;;

	*partitions*)
		_partitions
	;;

	*pci*)

		check_opt lspci systeminfos::pci		
		_pci
	;;

	*usb*)
		
		check_opt lsusb systeminfos::usb
		_usb
	;;

	*mounts*)
		_mounts
	;;

	*bios*)
		
		check_opt dmidecode systeminfos::bios
		_bios
	;;

	*all*)
		_cpu
		_kernel
		_memory
		_partitions
		#_pci
		#_usb
		_mounts
		#_bios
	;;

	*)
		_help
	;;
esac

