#!/bin/sh
#
# Licensed under GPL v2 or later at your option
# Copyright 2004 by Michiel de Boer <infobash@rebelhomicide.demon.nl>
# Copyright 2006 by Emil Obermayr <nobs@tigress.com>
#
# this version is stripped down to no-color
#
# get full original version at http://rebelhomicide.demon.nl/scripts/

PORT=$1;
SERVER=$2;
TARGET=$3;

export LC_ALL="C"

HN="$(hostname)"
OSKERN="$(uname -s) $(uname -r)"
if [ "$TDE_FULL_SESSION" = "true" ]; then
	if [ "$TDE_SESSION_VERSION" = 4 ];  then
		KDE="$(kde4-config --version | sed -n '2p' | sed 's/://;s/ *$//')"
	else
		KDE="$(tde-config --version | sed -n '2p' | sed 's/://;s/ *$//')"
	fi
fi

CPU=$(awk -F':' '/model name/{name=$2}
 /cpu MHz/{mhz=int($2)}
 /bogomips/ {bogo=int($2) 
}
END{
 gsub (/ *\(tm\) */, " ", name);
 gsub (/ *\(TM\) */, " ", name);
 gsub (/ *Processor */, " ", name);
 gsub (/ *$/, "", name);
 gsub (/^ */, "", name);
 printf "CPU: %s at %d MHz (%d bogomips)", name, mhz, bogo;
}
' /proc/cpuinfo )
  
HDD=$(df -lP| awk '($1~/\/dev/){
  use+=$3/1024^2;
  tot+=$2/1024^2;
 }
 END{print "HD: " int(use) "/" int(tot) "GB"}')

MEM=$(awk '($1=="MemTotal:"){tot=int($2/1024)}
 ($1=="MemFree:"){free=int($2/1024)}
 END{
use=tot-free
print "RAM: " use "/" tot "MB"}
' /proc/meminfo)

PROC="$(($(ps aux | wc -l)-1))"

UPT=$(awk '{u="s";
n=$1;
if (n>60){
 n2=n%60;
 n/=60;
 u="min";
 if (n>60){
  n2=n%60;
  n/=60;
  u="h";
  if (n>24){
   n2=n%24;
   n/=24;
   u="d"; 
   }
  }
 }
printf ("%d.%d%s up",n, n2, u);
}' /proc/uptime )

out="Sysinfo for '$HN': $OSKERN running $KDE, $CPU, $HDD, $MEM, $PROC proc's, ${UPT}" 

if [ "x$PORT" = "x" ] ; then
    echo "$out"
else
    if [ "x$TARGET" = "x" ] ; then
	dcop $PORT default error "$out"
    else
	dcop $PORT default say $SERVER "$TARGET" "$out"
    fi
fi
