
INV_SYSTEM=""
INV_PROC=""
INV_NPROC=0
INV_DISKS=""
INV_MEM=""		# in megabyte


sysinv()
{
	sysinv_systype
	sysinv_sysmem
	sysinv_disks
}

sysinv_list()
{
	ihdr "SYSTEM INVENTORY"
	msg  "System             : ${INV_SYSTEM:-<unknown>}"
	msg  "Processor          : ${INV_PROC:-<unknown>}"
	msg  "Num. of Processors : ${INV_NPROC:-<unknown>}"
	msg  "System Memory      : ${INV_MEM:-<unknown>} MB"
	msg  "Disk devices       : ${INV_DISKS:-<none>}"
}

sysinv_systype()
{
	local sys=""
	local cpu=""
	local -i n_cpus=0

	if test -f "$CPUINFO"; then
		sys=`grep -i "^system variation" "$CPUINFO" 2>/dev/null | \
		     cut -d: -f2`
		sys=`trim "$sys" | tr [A-Z] [a-z] 2>/dev/null`
		
		cpu=`grep -i "^cpu model" "$CPUINFO" 2>/dev/null | cut -d: -f2`
		cpu=`trim "$cpu" | tr [a-z] [A-Z] 2>/dev/null`

		n_cpus=`grep -i "^cpus detected" "$CPUINFO" 2>/dev/null | \
		        cut -d: -f2 2>/dev/null`
		n_cpus=`trim "$n_cpus"`
		
		[ -n "$sys"    ] && INV_SYSTEM=$sys
		[ -n "$cpu"    ] && INV_PROC=$cpu
		[ -n "$n_cpus" ] && INV_NPROC=$n_cpus
	else
		warn "$CPUINFO: file not found"
	fi
}

sysinv_sysmem()
{
	local -i mem=0
	
	if test -f "$MEMINFO"; then
		mem=`grep -i "^memtotal" "$MEMINFO" 2>/dev/null | \
		     cut -d: -f2 | tr -c -d [0-9]`

		INV_MEM=$(($mem / 1024))
	else
		warn "$MEMINFO: file not found"
	fi
}

sysinv_disks()
{
	local ida=""		# SmartArrays
	local ide=""		# IDE disks
	local dac=""		# DAC960's
	local scsi=""		# SCSI disks
	local dev=""
	local d=""

	if test -d "/dev/ida"; then
		ida=`ls /dev/ida 2>/dev/null | \
		     sed -ne '/c[0-9]d[0-9]\+$/p' 2>/dev/null`
	fi

	if test -d "/dev/rd"; then
		dac=`ls /dev/rd 2>/dev/null | \
		     sed -ne '/c[0-9]d[0-9]\+$/p' 2>/dev/null`
	fi

	scsi=`ls /dev/sd[a-z] 2>/dev/null`

	dev=`ls /dev/hd[a-z] 2>/dev/null`
	for d in `echo $dev | sed -ne 's@/dev/@@p' 2>/dev/null`
	do
		if test -r "/proc/ide/$dev/media"; then
			grep -iq disk /proc/ide/$dev/media 2>/dev/null
			if test $? -eq 0; then
				ide="$ide /dev/$dev"
			fi
		fi
	done

	INV_DISKS=`trim "$ida $dac $scsi $ide"`
}

sysinv_get_bootdev()
{
	local dev=""
	local parm=""
	local -i len=0
	local -a cmdline

	if test -f "$CMDLINE"; then
		cmdline=(`cat $CMDLINE 2>/dev/null`)

		for parm in ${cmdline[*]}; do
			len=`expr match "$parm" "^root=" 2>/dev/null`
			if test $len -eq 5; then
				dev=`echo $parm | cut -d= -f2 2>/dev/null`
				break
			fi
		done
		if test "$dev" = "/dev/nfs"; then
			dev="eth0"		# !!! FIX !!!!
		fi
	else
		warn "$CMDLINE: file not found"
	fi

	echo ${dev##*/}		# remove leading path
}

sysinv_get_bootsrc()
{
	local dev=`trim "$1"`
	local src=""

	case "$dev" in
	hd*|sd*|sc*)
	  src="/"
	  ;;
	eth*) 
	  src=`mount 2>/dev/null | grep '[[:space:]]\+/[[:space:]]\+' | \
	       cut -d' ' -f1 | cut -d: -f1`
	  ;;
	esac

	echo $src
}

sysinv_get_serial()
{
	local sn=""
	local site=""
	local week=""		# not defining a integer since we may need to
	local build=""		# preserve the leading zero
	local -i year=0
	local -i ret=0
	local -a sn_parts

	if test -f "$CPUINFO"; then
		sn=`grep -i "^system serial number" "$CPUINFO" 2>/dev/null | \
		    cut -d: -f2`
		#
		#
		#
		sn_parts=(`echo $sn | sed -ne 's@^\([[:alpha:]][[:alpha:]]\)\([0-9]\)\([0-5][0-9]\)\([0-9][0-9][0-9][0-9][0-9]\)$@\1 \2 \3 \4@p'`)
		site=`echo ${sn_parts[0]} | tr [a-z] [A-Z] 2>/dev/null`
		year=${sn_parts[1]}
		week=${sn_parts[2]}
		build=${sn_parts[3]}

		#
		#
		case "$site" in
		AB|AY) ;;
		CA|CT|CX) ;;
		DY) ;;
		GA) ;;
		HK|HL|HM|HY) ;;
		KA|KB|KL) ;;
		ML|MR) ;;
		NI) ;;
		PC|PY) ;;
		SH) ;;
		TA|TY) ;;
		WF|WM) ;;
		ZK|ZS) ;;
		*) ret=1 ;;
		esac
		#
		if test $week -lt 1 -o $week -gt 52; then
			ret=2
		fi
	else
		warn "$CPUINFO: file not found"
		ret=2
	fi

	if test $ret -eq 0 -o $ret -eq 1; then
		echo $site$year$week$build
	fi
	return $ret
}

is_alpha_system()
{
	local machine=`uname -m 2>/dev/null`

	if test "$machine" = "alpha"; then
		return 0
	else
		return 1
	fi
}
