#!/bin/bash
#Copyright (c) 2004, OSTSC(http://help.oss.or.kr)
#All rights reserved.

#This software is provide to find your system infomation.
#2004.12.22 
#eful@superuser.co.kr

PVER="sysinfo v0.03 (http://help.oss.or.kr)";
# ANSI COLORS
NORMAL="\033[0;39m"
# RED
RED="\033[1;31m"
# GREEN
GREEN="\033[1;32m"
# YELLOW
YELLOW="\033[1;33m"
# BLUE
BLUE="\033[1;34m"
# LCYAN
LCYAN="\033[1;36m"
# CYAN
CYAN="\033[0;36m"
# BOLD WHITE
WHITE="\033[1;37m"

######### Print help message #####
print_help () {
	echo -e ${WHITE}
	echo "Usage: sysinfo -h | --help | -v | --version | -e | --export | -a | --all"
	echo
	echo "    -h,--help       Print this help message and exit"
	echo "    -v,--version    Print version and exit"
	echo "    -e,--export     Make report file - sysinfo.txt "
	echo "    -a,--all        Print all system infomation and exit"
	echo ""
	echo "Report bugs to <eful@superuser.co.kr>."
	echo -e ${NORMAL}
}

######## Print Program Version ###
print_version () {
	echo -e ${WHITE}
	echo "sysinfo version ${PVER}"
	echo -e ${NORMAL}
}

######## root user check #######
check_root () {
	# user id check
	[ `id -u` -ne 0 ] && {
		echo -e "${RED}This program must be run by the root user${NORMAL}"
		exit 0
	}
}
	

####### Starting System Detect #######
print_info () {
	########### Print User Message ############
	#clear
	echo ""
	echo -e "${YELLOW}$PVER"
	echo -e "${LCYAN}Starting System Detect...${NORMAL}  :"
	echo "-----------------------------------------------------------"

	########## Print OS, architecture , Kernel , hostname , date ####
	ARCHTYPE=`/bin/uname -m`
	SYSTEMHOSTNAME=`hostname`
	TIMEOFREPORT=`date`
	OSNAME=`cat /proc/version | cut -d "#" -f 1|cut -d "(" -f 4| sed 's/))//g'`
	SYSINFO=`uname -a`
	KVER=`uname -r`
	echo -e "${BLUE}OS NAME : ${WHITE}$OSNAME"
	echo -e "${BLUE}Kernel Version : ${WHITE}$KVER${NORMAL}";
	echo -e "${BLUE}Architecture : ${WHITE}$ARCHTYPE"
	echo -e "${BLUE}System Hostname : ${WHITE}$SYSTEMHOSTNAME"
	echo -e "${BLUE}Time of Report : ${WHITE}$TIMEOFREPORT"
	echo -e "${BLUE}System Info : ${WHITE}$SYSINFO"

	######## Print gcc version #############
	if [ -f /usr/bin/gcc ]; then
		GCCVER=$(gcc --version | grep gcc);
		echo -en "${BLUE}GCC Version :${WHITE}"
		echo -e " ${GCCVER}"
	else 
		echo -en "${BLUE}GCC Version :${WHITE}"
		echo -e " can not find gcc"
	fi

	######## Print glibc version #############
	if [ -f /usr/bin/ldd ]; then
		GLVER=$(ldd --version | grep ldd);
		echo -en "${BLUE}Glibc Version :${WHITE}"
		echo -e " ${GLVER}"
	else 
		echo -en "${BLUE}Glibc Version :${WHITE}"
		echo -e "can not find ldd"
	fi

	########### Print CPU info ##########################
	echo -e "${BLUE}CPU & Cache${NORMAL} : ${WHITE}";
	if [ ! -f /proc/cpuinfo ]; then
		echo "*** Can't find /proc/cpuinfo! This means that /proc isn't mounted. ***"
		echo "*** Mount /proc and try again. ***"
	else
		awk -F: '/^processor/{printf "Processor"$2" is"};/^model name/{printf $2};/^vendor_id/{printf vendor};/^cpu MHz/{printf " %dMHz",int($2)};/^cache size/{printf ","$2" Cache"};/^$/{print ""}' /proc/cpuinfo 2>/dev/null
	fi
	echo -en "${NORMAL}"
	echo ""

	#### Print memory infomation #####################
	set `cat /proc/meminfo`
	NDIR=$(pwd)
	cd /tmp
	grep MemTotal /proc/meminfo >sch1.tmp
	awk '{ print $2 }' sch1.tmp >sch2.tmp
	MemTot="$(cat sch2.tmp)"
	rm *.tmp

	grep MemFree /proc/meminfo >sch1.tmp
	awk '{ print $2 }' sch1.tmp >sch2.tmp
	MemFree="$(cat sch2.tmp)"
	rm *.tmp

	Memunused=$[($MemTot - $MemFree) * 100 / $MemTot]

	grep SwapTotal /proc/meminfo >sch1.tmp
	awk '{ print $2 }' sch1.tmp >sch2.tmp
	SwapTot="$(cat sch2.tmp)"
	rm *.tmp

	grep SwapFree /proc/meminfo >sch1.tmp
	awk '{ print $2 }' sch1.tmp >sch2.tmp
	SwapFree="$(cat sch2.tmp)"
	rm *.tmp

	if [ ! "${SwapTot}" = 0 ]; then
		Swapunused=$[($SwapTot - $SwapFree) * 100 / $SwapTot]
	else
		Swapunused=$[$SwapTot]
	fi
		

	# Write memory, swap, shared, buffer
	echo -e "${BLUE}Memory info      Size(MB) Free(MB) Full %"
	echo -en " Memory        : ${WHITE}"
	echo -n $[$MemTot / 1024] "     "
	echo -n $[$MemFree / 1024] "        "
	echo "$Memunused%"

	echo -en "${BLUE} Swap          : ${WHITE}"
	echo -n $[$SwapTot / 1024] "     "
	echo -n $[$SwapFree / 1024] "      "
	echo "  $Swapunused%"

	# print df 
	echo ""
	echo -e "${BLUE}MountPoint    	Usage	Size(MB)	${GREEN}Free(MB)${WHITE}" 

	df | awk -v lngth="$l"\
 	'BEGIN {getline;l=lngth;}
 			{
 				tot=$3*l/$2+1; 
 				printf("%s%-14s%s", " ", $6, ": ");
 
				for (i=1; i<l+1; i++) {
  					if ( i < tot ) { printf("%s", " ") }
  					else { printf("%s", " ") }
  				}
 			printf(" %s     %s     %2.2f %s", $2 / 1024 , $4 / 1024 , ($2 - $4) * 100 / $2, "%");
 			print("");
 			}
 	END{printf("")}'
	cd $NDIR

	############### Print pci device infomation #########################
	############### lspci util and pcitable file dependence #############
	
	if [ -f /etc/sysinfo/pcitable ]; then
		if [ -f /sbin/lspci ]; then
			echo ""
			echo -e "${BLUE}PCI DEVICE${WHITE}"
			lspci -n > lspci.list
			awk '{print $4}' lspci.list | sed 's|:| |g' | awk '{print "0x" $1}' > id.tmp
			awk '{print $4}' lspci.list | sed 's|:| |g' | awk '{print "0x" $2}' > vd.tmp
			grep --file=id.tmp /etc/sysinfo/pcitable > sch2.tmp
			grep --file=vd.tmp  sch2.tmp > pci1.tmp
			sed 's|0x||g' pci1.tmp > pci2.tmp
			echo -e "${BLUE}VendID  DevID   Module          Description${WHITE}"
			cat pci2.tmp
			rm -f *.tmp lspci.list
		else 
			echo -e "can not find /sbin/lspci"
			echo -e "pciutils from http://atrey.karlin.mff.cuni.cz/~mj/pciutils.shtml or your Linux vendor."
		fi
	else
		echo -e "can not find pcitable file"
		echo -e "pcitable from http://cvs.mandrakesoft.com/cgi-bin/cvsweb.cgi/soft/ldetect-lst/lst/pcitable"
	fi

}

####### print output file
print_file () {
        ARCHTYPE=`/bin/uname -m`
        SYSTEMHOSTNAME=`hostname`
        TIMEOFREPORT=`date`
	OSNAME=`cat /proc/version | cut -d "#" -f 1|cut -d "(" -f 4| sed 's/))//g'`
        SYSINFO=`uname -a`
        KVER=`uname -r`
	MYIFS="="
        echo -e "$PVER" > /tmp/sysinfo.txt
        echo -e "$TIMEOFREPORT" >> /tmp/sysinfo.txt
        echo "-----------------------------------------------------------" >> /tmp/sysinfo.txt
        echo -e "OS NAME ${MYIFS} $OSNAME" >> /tmp/sysinfo.txt
        echo -e "Kernel Version ${MYIFS} $KVER" >> /tmp/sysinfo.txt
        echo -e "Architecture ${MYIFS} $ARCHTYPE" >> /tmp/sysinfo.txt
        echo -e "System Hostname ${MYIFS} $SYSTEMHOSTNAME" >> /tmp/sysinfo.txt
        echo -e "Time of Report ${MYIFS} $TIMEOFREPORT" >> /tmp/sysinfo.txt
        echo -e "System Info ${MYIFS} $SYSINFO" >> /tmp/sysinfo.txt
        if [ -f /usr/bin/gcc ]; then
                GCCVER=$(gcc --version | grep gcc);
                echo -e "GCC Version ${MYIFS} $GCCVER" >> /tmp/sysinfo.txt
        else echo -e "GCC Version ${MYIFS} can not find gcc" >> /tmp/sysinfo.txt
        fi
        if [ -f /usr/bin/ldd ]; then
                GLVER=$(ldd --version | grep ldd);
                echo -e "Glibc Version ${MYIFS} $GLVER" >> /tmp/sysinfo.txt
        else echo -e "Glibc Version ${MYIFS} can not find ldd" >> /tmp/sysinfo.txt
        fi
	mv -f /tmp/sysinfo.txt .
	echo -e "Created sysinfo.txt file${NORMAL}"
}

### main process
case $1 in
 -h|--help) print_help ;;
 -v|--version) print_version ;;
 -e|--export) print_file ;;
 -a|--all)  check_root;print_info;print_file ;;
 *) print_help
  exit 0
  ;;

esac
