#!/bin/bash
#	License: Licensed under the TPL. Same as the GPL, except all
#			changes should be emailed to trans13nt@jyll.net.
#	Author:	Teri aka TrAns13nT
#	Contact Info:	irc.oftc.net #linuxchics TrAns13nT
#			yahoo! gentoo_ownz
#			email trans13nt@jyll.net
#	Please submit bug reports to any of these locations
#	Disclaimer:	If my script breaks it, it ain't my fault.
#			If you break my script, it ain't my fault.
#			If it brings you emotional distress, it aon't my fault.
#			So pretty much, it ain't my fault. Have fun, ymmv.
#
#	Requirements:	/proc entries, pcituils, sed, awk, cat, grep, ifconfig, who, uptime
#			uname, hostname, ls, free, bc, echo
#	optional:	nvclock(requires nvidia drivers isntalled if script is ran as a user)
#			xdpyinfo(xfree), lm_sensors
#	Notes:	Not multi anything compitable right now.
#		It's a constant work in progress as I add and revise code.
#			Stay updated!!!
#	Future Additions:	integration with media.sh ?
#				make the chat function call other functions
#					for the info it wants
#				improve on video card definitions in chat function
#				make things at least dual compatible
#					dual compat functions:	sound

function cpu
{
	vendor=`grep vendor_id /proc/cpuinfo|sed -e 's/vendor_id[[:space:]]*: //'`
	    if [ "$vendor" = "AuthenticAMD" ]
		then
		    amd		
	    elif [ "$vendor" = "GenuineIntel" ]
		then
		    intel
	    fi
}

function amd
{
get=`grep name /proc/cpuinfo|sed -e 's/model name[[:space:]]*: AMD //'`
get2=`echo $get|cut -d\  -f2`
    if [ "$get2" = "processor" ]
        then
    	    name=`echo $get|cut -d\  -f1`
    else
	    name=`echo $get|cut -d\  -f1,2`
    fi
l2cache=`grep cache /proc/cpuinfo|sed -e 's/cache size[[:space:]]*: //'`
speed=`grep MHz /proc/cpuinfo|sed -e 's/cpu MHz[[:space:]]*: //'`
bogo=`grep bogo /proc/cpuinfo|sed -e 's/bogomips[[:space:]]*: //'`
echo -e "CPU Type     : AMD $name"
echo -e "L1 Cache     : 128 KB"
echo -e "L2 Cache     : $l2cache"
echo -e "Speed        : $speed MHz"
echo -e "BogoMips     : $bogo"
}

function intel
{
cpunum=`grep processors /proc/cpuinfo|wc -l|awk '{print $1}'`
    get=`grep name /proc/cpuinfo|sed -e 's/model name[[:space:]]*: //'`
    get2=`echo $get|cut -d\  -f1`
	if [ "$get2" = "Intel(R)" ]
	    then
		name="Pentium4"
	elif [ "$get2" = "Pentium" ]
	    then
		name="Pentium"
	elif [ "$get2" = "Celeron" ]
	    then
		name="Pentium Celeron"
	fi
    if [ "$name" = "Pentium4" ]
	then
	    l1cache="20 KB"
    elif [ "$name" = "Pentium" ]
	then
	    l1cache="32 KB"
    elif [ "$name" = "Pentium Celeron" ]
	then
	    l1cache="32 KB"
    fi
    if [ "$name" = "Pentium" ]
	then
	    name2=`grep name /proc/cpuinfo|sed -e 's/model name[[:space:]]*: //'|cut -d\  -f2`
    fi
    l2cache=`grep cache /proc/cpuinfo|sed -e 's/cache size[[:space:]]*: //'`
    speed=`grep MHz /proc/cpuinfo|sed -e 's/cpu MHz[[:space:]]*: //'`
    bogo=`grep bogo /proc/cpuinfo|sed -e 's/bogomips[[:space:]]*: //'`
    echo -e "CPU Type     : $name $name2"
    echo -e "L1 Cache     : $l1cache"
    echo -e "L2 Cache     : $l2cache"
    echo -e "Speed        : $speed MHz"
    echo -e "BogoMips     : $bogo"
}

function nvidia
{
if [ -f /usr/bin/nvclock ]
    then
        card=`nvclock -s|grep "Card:"|sed -e 's/Card:[[:space:]]*//' -e 's/nVidia //'`
        gpu=`nvclock -s|grep Core|sed -e 's/Core speed:[[:space:]]*//'`
        mem=`nvclock -s|grep Mem|sed -e 's/Memory speed:[[:space:]]*//'`
echo -e "nVidia Card  : $card"
echo -e "        GPU  : $gpu"
echo -e "        Mem  : $mem"
fi
}

function network
{
        inbyte=`/sbin/ifconfig eth0|grep bytes|awk '{print $3,$4 } '|sed -e 's/(//' -e 's/)//'`
        outbyte=`/sbin/ifconfig eth0|grep bytes|awk '{print $7,$8 } '|sed -e 's/(//' -e 's/)//'`
        ip=`/sbin/ifconfig eth0|grep inet|sed -e 's/[[:space:]]*inet addr://'|cut -d\  -f1`
echo -e "eth0 ip      : $ip"
echo -e "eth0 In      : $inbyte"
echo -e "eth0 Out     : $outbyte"
}	    

function load
{
users=`who|wc -l|sed -e 's/[[:space:]]*//'`
one=`cat /proc/loadavg|cut -d\  -f1`
five=`cat /proc/loadavg|cut -d\  -f2`
fifteen=`cat /proc/loadavg|cut -d\  -f3`
up=`uptime|tr -d ,`
    if [ "`echo $up|grep day`" ]
	then
	    up1=`echo "$up"|awk '{print $3,$4,$5}'`
    else
	    up1=`echo "$up"|awk '{print $3}'`
    fi
echo -e "Uptime       : $up1"
echo -e "Users        : $users"
echo -e "Load         : $one - $five - $fifteen"
}    

function kernel
{
run=`uname -r`
echo -e "Kernel       : $run"
}

function hostName
{
hostN=`hostname`
echo -e "Hostname     : $hostN"
}

function memory
{
ramtot=`free -m|grep Mem|sed -e 's/Mem:[[:space:]]*//'|cut -d\  -f1`
ramuse=`free -m| grep -|sed -e 's/-\/+ buffers\/cache:[[:space:]]*//'|cut -d\  -f1`
ramfree=`echo "$ramtot-$ramuse"|bc`
swaptot=`free -m|grep Swap|sed -e 's/Swap:[[:space:]]*//'|cut -d\  -f1`
swapfree1=`cat /proc/meminfo|grep SwapFree|sed -e 's/SwapFree:[[:space:]]*//' -e 's/ kB//'|cut -d\  -f1`
swapfree=`echo "$swapfree1/1024"|bc`
swapuse=`echo "$swaptot-$swapfree"|bc`
echo -e "                  Total    Used   Free"
echo -e "Memory       :    $ramtot MB  $ramuse MB   $ramfree MB"
echo -e "Swap         :    $swaptot MB  $swapuse MB   $swapfree MB"
}

function xfree # this function requires X to be running
{
if [ -f /usr/bin/X11/xdpyinfo ]
    then
	version=`xdpyinfo|grep "XFree86 version"|sed -e 's/XFree86 version: //'`
	res=`xdpyinfo|grep dimensions|sed -e 's/[[:space:]]*dimensions:[[:space:]]*//' -e 's/ pixels (.*)//'`
echo -e "XFree        : v $version"
echo -e "Resolution   : $res"
fi
}

function chat
{
kernel=`uname -sr`
cpu=`grep name /proc/cpuinfo | sed -e 's/model name[[:space:]].*: //'`
speed=`grep MHz /proc/cpuinfo | sed -e 's/cpu MHz[[:space:]].*: //' -e 's/\..*//'`
bogo=`grep bogo /proc/cpuinfo | sed -e 's/bogomips[[:space:]].*: //' -e 's/\..*//'`
mtotal=`free -m|grep Mem|sed -e 's/Mem:[[:space:]]*//'|cut -d\  -f1`
mused=`free -m| grep -|sed -e 's/-\/+ buffers\/cache:[[:space:]]*//'|cut -d\  -f1`
perc=`echo "scale=1;$mused/$mtotal"|bc`
    if [ $perc = ".1" ]
	then
	    led="*|/=========|*"
    elif [ $perc = ".2" ]
	then
	    led="*|//========|*"
    elif [ $perc = ".3" ]
	then
	    led="*|///=======|*"
    elif [ $perc = ".4" ]
	then
	    led="*|////======|*"
    elif [ $perc = ".5" ]
	then
	    led="*|/////=====|*"
    elif [ $perc = ".6" ]
	then
	    led="*|//////====|*"
    elif [ $perc = ".7" ]
	then
	    led="*|///////===|*"
    elif [ $perc = ".8" ]
	then
	    led="*|////////==|*"
    elif [ $perc = ".9" ]
	then
	    led="*|/////////=|*"
    elif [ $perc = "1.0." ]
	then
	    led="*|//////////|*"
    fi
bustot=`ls /proc/ide|grep ide|wc -l|awk '{print $1}'`
hdsz="0"
drvsz="0"
while [ $bustot != "0" ]
do
    idecon=`echo "$bustot-1"|bc`
    drvnum=`ls /proc/ide/ide$idecon|grep hd|wc -l|awk '{print $1}'`
	if [ $drvnum = "1" ]
	    then
		devnm=`ls /proc/ide/ide$idecon|grep hd`
		devtp=`cat /proc/ide/$devnm/media`
		    if [ $devtp = "cdrom" ]
			then
			    drvsz="0"
		    elif [ $devtp = "disk" ]
			then
			    drvsz1=`cat /proc/ide/$devnm/capacity`
			    drvsz=`echo "$drvsz1/(2*990*990)"|bc`
		    fi
		hdsz=`echo "$drvsz+$hdsz"|bc`
	elif [ $drvnum = "2" ]
	    then
		
		fstnm=`ls /proc/ide/ide$idecon|grep hd|head -n1`
		fsttp=`cat /proc/ide/$fstnm/media|awk '{print $1 }'`
        	    if [ $fsttp = "cdrom" ]                                                                                                                                  
			then                                                                                                                                                   
                	    drvsz="0"                                                                                                                                          
        	    elif [ $fsttp = "disk" ]                                                                                                                                 
            		then                                                                                                                                                   
	        	    drvsz1=`cat /proc/ide/$fstnm/capacity`                                                                                                             
	        	    drvsz=`echo "$drvsz1/(2*990*990)"|bc`                                                                                                            
		    fi                                             
		hdsz=`echo "$drvsz+$hdsz"|bc`
		
		scdnm=`ls /proc/ide/ide$idecon|grep hd|tail -n1`
		scdtp=`cat /proc/ide/$scdnm/media`
        	    if [ $scdtp = "cdrom" ]                                                                                                                                  
			then                                                                                                                                                   
                	    drvsz="0"
		    elif [ $scdtp = "disk" ]                                                                                                                                 
            		then                                                                                                                                                   
	        	    drvsz1=`cat /proc/ide/$scdnm/capacity`                                                                                                             
	        	    drvsz=`echo "$drvsz1/(2*990*990)"|bc`                                                                                                            
		    fi                                             
		hdsz=`echo "$drvsz+$hdsz"|bc`
	fi	
    bustot=`echo "$bustot-1"|bc`
done
res=`xdpyinfo|grep dimensions|sed -e 's/[[:space:]]*dimensions:[[:space:]]*//' -e 's/ pixels (.*)//'`
up=`uptime|tr -d ,`                                                                                                           
    if [ "`echo $up|grep day`" ]                                                                                             
        then                                                                                                                  
            up1=`echo "$up"|awk '{print $3,$4,$5}'`                                                                           
    else                                                                                                                      
            up1=`echo "$up"|awk '{print $3}'`                                                                                 
    fi
procc=`cut -f4 -d ' ' /proc/loadavg`
proct=`grep proc /proc/stat|cut -f2 -d ' '`
one=`cat /proc/loadavg|cut -d\  -f1`                                                                                          
five=`cat /proc/loadavg|cut -d\  -f2`                                                                                         
fifteen=`cat /proc/loadavg|cut -d\  -f3`   
vcdman=`/sbin/lspci|grep "VGA comp"|sed -e 's/.*:.*..* VGA compatible controller: //'|awk '{print $1}'`
    if [ $vcdman = "nVidia" ]
	then
	    vcard=`/sbin/lspci|grep "VGA comp"|sed -e 's/.*:.*..* VGA compatible controller: //'|awk '{print $1,$4,$5,$6}'|sed -e 's/\[//' -e 's/\]//'` # work on at least on nVidia GeForce3, I'd assume any GeForce card at least as well
    elif [ $vcdman = "Trident" ]
	then
	    vcard=`/sbin/lspci|grep "VGA comp"|sed -e 's/.*:.*..* VGA compatible controller: //'|awk '{print $1,$3}'` # works on at least one Trident card
    elif [ $vcdman = "ATI" ]
	then
	    vcard=`/sbin/lspci|grep "VGA comp"|sed -e 's/.*:.*..* VGA compatible controller: //'|awk '{print $1,$4,$5,$6}'` # works on at least one ATI Rage XL
    elif [ $vcdman = "Matrox" ]
	then
	    vcard=`/sbin/lspci|grep "VGA comp"|sed -e 's/.*:.*..* VGA compatible controller: //'|awk '{print $1,$5}'` # works on at least one Matrox G400
    fi
nin=`/sbin/ifconfig eth0|grep bytes|awk '{print $3,$4 } '|sed -e 's/(//' -e 's/)//'`
nout=`/sbin/ifconfig eth0|grep bytes|awk '{print $7,$8 } '|sed -e 's/(//' -e 's/)//'`
echo -e "sysInfo: $kernel | $cpu - $speed MHz - $bogo Bogomips | Mem: $mused/$mtotal MB $led | HD Size: $hdsz GB | X res: $res | Uptime: $up1 | Procs: $procc/$proct | Load: $one - $five - $fifteen | Video Card: $vcard | Net: $nin/$nout"
}

function harddrv
{
bustot=`ls /proc/ide|grep ide|wc -l|awk '{print $1}'`
hdsz="0"
drvsz="0"
hdtotal="0"
cdtotal="0"
while [ $bustot != "0" ]
do
    idecon=`echo "$bustot-1"|bc`
    drvnum=`ls /proc/ide/ide$idecon|grep hd|wc -l|awk '{print $1}'`

	if [ $drvnum = "1" ]
	    then
		devnm=`ls /proc/ide/ide$idecon|grep hd`
		devtp=`cat /proc/ide/$devnm/media`
		    if [ $devtp = "cdrom" ]
			then
			    drvsz="0"
			    cdnum=`echo "$cdtotal+1"|bc`
		    elif [ $devtp = "disk" ]
			then
			    drvsz1=`cat /proc/ide/$devnm/capacity`
			    drvsz=`echo "$drvsz1/(2*990*990)"|bc`
			    hdtotal=`echo "$hdtotal+1"|bc`
		    fi
		hdsz=`echo "$drvsz+$hdsz"|bc`
	elif [ $drvnum = "2" ]
	    then
		
		fstnm=`ls /proc/ide/ide$idecon|grep hd|head -n1`
		fsttp=`cat /proc/ide/$fstnm/media|awk '{print $1 }'`
        	    if [ $fsttp = "cdrom" ]                                                                                                                                  
			then                                                                                                                                                   
                	    drvsz="0"
			    cdtotal=`echo "$cdtotal+1"|bc`
        	    elif [ $fsttp = "disk" ]                                                                                                                                 
            		then                                                                                                                                                   
	        	    drvsz1=`cat /proc/ide/$fstnm/capacity`                                                                                                             
	        	    drvsz=`echo "$drvsz1/(2*990*990)"|bc`                                                                                                            
			    hdtotal=`echo "$hdtotal+1"|bc`		    
		    fi                                             
		hdsz=`echo "$drvsz+$hdsz"|bc`
		
		scdnm=`ls /proc/ide/ide$idecon|grep hd|tail -n1`
		scdtp=`cat /proc/ide/$scdnm/media`
        	    if [ $scdtp = "cdrom" ]                                                                                                                                  
			then                                                                                                                                                   
                	    drvsz="0"
			    cdtotal=`echo "$cdtotal+1"|bc`
		    elif [ $scdtp = "disk" ]                                                                                                                                 
            		then                                                                                                                                                   
	        	    drvsz1=`cat /proc/ide/$scdnm/capacity`                                                                                                             
	        	    drvsz=`echo "$drvsz1/(2*990*990)"|bc`                                                                                                            
			    hdtotal=`echo "$hdtotal+1"|bc`
		    fi                                             
		hdsz=`echo "$drvsz+$hdsz"|bc`
	fi	
    bustot=`echo "$bustot-1"|bc`
done
echo -e "Hard Drives  : $hdtotal"
echo -e "HD Size      : $hdsz GB"
echo -e "CDROM Drives : $cdtotal"
}

function sens # this function is for lm_sensors, some modification may be nessecary in the greps, also I have 3 temp sensors,
	      #	so u made too comment out sbrt= line and comment out the echo -e "SBR Temp line. If you want output in 
	      #	farenheit, then change all *t=`sensors| lines to *t=~sensors -f|
{
if [ -f /usr/bin/sensors ]
    then
	cput=`sensors|grep "CPU Temp:"|awk '{print $3}'`
	syst=`sensors|grep "SYS Temp:"|awk '{print $3}'`
	sbrt=`sensors|grep "SBr Temp:"|awk '{print $3}'`
	echo -e "CPU Temp     : $cput"
	echo -e "SYS Temp     : $syst"
	echo -e "SBr Temp     : $sbrt"
fi
}

function sound
{
sndnum=`/sbin/lspci|grep audio|wc -l|awk '{print $1}'`
if [ $sndnum = "1" ]
    then
	crdnm=`/sbin/lspci|grep audio|sed -e 's/.*:.*..* Multimedia audio controller: //' -e 's/(.* .*)//' -e 's/\[//' -e 's/\]//'`
	echo -e "Sound Card   : $crdnm"
elif [ $sndnum = "2" ]
    then
	crdnm1=`/sbin/lspci|grep audio|head -n1|sed -e 's/.*:.*..* Multimedia audio controller: //' -e 's/(.* .*)//' -e 's/\[//' -e 's/\]//'`
	crdnm2=`/sbin/lspci|grep audio|tail -n1|sed -e 's/.*:.*..* Multimedia audio controller: //' -e 's/(.* .*)//' -e 's/\[//' -e 's/\]//'`
	echo -e "Sound Card   : $crdnm1"
	echo -e "Sound Card   : $crdnm2"
fi
}

function all
{
hostName
kernel
cpu
network
load
memory
harddrv
xfree
nvidia
sound
sens
}

function hlp
{
echo -e "<sysInfo.sh> - usage: sysInfo.sh -(option)"
echo -e "	sysInfo.sh is a script to retreive certain speciic system"
echo -e "	  information and display it to a termianl, file, or in some"
echo -e "	  chat client that aloows running of bash commands and scripts"
echo -e "	"
echo -e "	Options"
echo -e "-a	all      -  runs all default functions of the script and echo's"
echo -e "		        output to a terminal *NOT CHAT FRIENDLY*"
echo -e "-c	cpu      - output select cpu info"
echo -e "-C	chat     - output a chat friendly one liner of various info"
echo -e "-h	hostname - displays the hostname"
echo -e "-help	help     - your reading it, same as running with no options"
echo -e "-H	harddrv  - gets some hard drive info"
echo -e "-k	kernel   - displays the kernel"
echo -e "-m	memory   - displays memory and swap information"
echo -e "-n	network  - displays some network information"
echo -e "-N	nvidia   - displays nvidia card inforamtion"
echo -e "			**Only works with nvidia drivers installed, X"
echo -e "                         running, and nvclock installed"
echo -e "-s	sound    - displays sound card (up to 2 cards compatible)"
echo -e "-S	sensors  - displays 3 sensor temps, please read comments in"
echo -e "                       function sens"
echo -e "-u	uptime   - displays the uptime and load"
echo -e "-v	version  - display script version"
echo -e "-x	free     - displays the X version and resolution"
}

function version
{
echo -e "sysInfo.sh - v 0.4.2 http://pws.chartermi.net/~7ran513n7/sysInfo.sh"
}

case $1 in
"" ) hlp ;;
"-a" ) all ;;
"-c" ) cpu ;;
"-C" ) chat ;;
"-h" ) hostName ;;
"-help" ) hlp ;;
"-H" ) harddrv ;;
"-k" ) kernel ;;
"-m" ) memory ;;
"-n" ) network ;;
"-N" ) nvidia ;;
"-s" ) sound ;;
"-S" ) sens ;;
"-u" ) load ;;
"-v" ) version ;;
"-x" ) xfree ;;
esac
