#!/usr/bin/perl
# Figure out (try to) where lspci is located
if (-e "/sbin/lspci") { $lspci="/sbin/lspci"; }
if (-e "/bin/lspci") { $lspci="/bin/lspci"; }
if (-e "/usr/sbin/lspci") { $lspci="/usr/sbin/lspci"; }
if (-e "/usr/bin/lspci") { $lspci="/usr/bin/lspci"; }
if (-e "/usr/local/sbin/lspci") { $lspci="/usr/local/sbin/lspci"; }
if (-e "/usr/local/bin/lspci") { $lspci="/usr/local/bin/lspci"; }
if (-e "/opt/sbin/lspci") { $lspci="/opt/sbin/lspci"; }
if (-e "/opt/bin/lspci") { $lspci="/opt/bin/lspci"; }
if (!$lspci) { 
  system("echo pciutils is not installed or lspci wasn't found!");
  exit;
}

if ($ARGV[0]){
    $switch = $ARGV[0];
  } else {
  $switch = "-";
}

if (-x "/sbin/zenity") { $do_gui="1"; }
if (-x "/bin/zenity") { $do_gui="1"; }
if (-x "/usr/sbin/zenity") { $do_gui="1"; }
if (-x "/usr/bin/zenity") { $do_gui="1"; }
if (-x "/usr/local/sbin/zenity") { $do_gui="1"; }
if (-x "/usr/local/bin/zenity") { $do_gui="1"; }
if (-x "/opt/sbin/zenity") { $do_gui="1"; }
if (-x "/opt/bin/zenity") { $do_gui="1"; } 

if ($switch eq "-gui") {
 if ($do_gui) {
  system('tail -f /etc/passwd | zenity --name=sysinfo --progress --pulsate --auto-close --window-icon=/usr/share/pixmaps/xchat.png --title="Loading SYSINFO" --text="Gathering Stats..." &');
 }
}

# CPU Load (only tested with one cpu)
  $cpu_load = `export TERM=dumb; top -b -d .1 -n1 | grep "Cpu(s)" | awk '{print \$4}' | tail -n1 | sed 's/%//'`; chomp($cpu_load);
  $cpu_load = sprintf("%.0f",($cpu_load));
  $cpu_load="($cpu_load% load)";
  chomp($cpu_load);

# Kernel Version
  $uname = `uname -r`; chop ($uname);

# CPU
  $cpu = cat("/proc/cpuinfo");
  if ($cpu =~/sparc/){
    $cpu =~ /(.*sparc.*)/;
    @cpumodel = split/:/,$1;
    $cpumodel = $cpumodel[1];
    $cpumodel =~ s/ //;
    $cpu =~ /(.*bogomips.*)/;
    @cpumhz = split/:/,$1;
    $cpumhz = sprintf ("%.f",$cpumhz[1]);
    $cpuinf = "$cpumodel @ ${cpumhz}MHz"; 
  } elsif ($cpu =~/ 486/){
    $cpu =~ /(.*model name.*)/;
    @cpumodel = split/:/,$1;
    $cpumodel = $cpumodel[1];
    $cpumodel =~ s/ //;
    $cpu =~ /(.*bogomips.*)/;
    @cpumhz = split/:/,$1;
    $cpumhz = sprintf ("%.f",$cpumhz[1]);
    $cpuinf = "$cpumodel @ ${cpumhz}MHz";
  } else {
    $cpu =~ /(.*model name.*)/;
    @cpumodel = split/:/,$1;
    $cpumodel = $cpumodel[1];
    $cpumodel =~ s/ //;
    $cpu =~ /(.*cpu MHz.*)/;
    @cpumhz = split/:/,$1;
    $cpumhz = sprintf ("%.f",$cpumhz[1]);
    $cpuinf = "$cpumodel @ ${cpumhz}MHz";
  }
  $cpu_cnt = `cat /proc/cpuinfo | grep 'processor' | wc -l | sed 's/ *//'`; chomp($cpu_cnt);
  $cache = `cat /proc/cpuinfo | grep 'cache size' | head -n 1 | sed -e 's/^.*: //'`; chomp($cache);
  $bogomips = `cat /proc/cpuinfo | grep 'bogomips' | head -n 1  | sed -e 's/^.*: //'`; chomp($bogomips);

# CPU Temp
  if (-x "sensors") { $do_temp=1; } else { $do_temp=0;}
    if ($do_temp == "1") {
      $cpu_temp = `sensors | grep Temp3 | awk '{print \$2}' | sed 's/.*+//'`; chop($cpu_temp);
      $cpu_temp1 = `sensors -f | grep Temp3 | awk '{print \$2}' | sed 's/.*+//'`; chop($cpu_temp1);
      $cpu_rpm = `sensors | grep fan1 | awk '{print \$2,\$3}' | sed 's/.*+//'`; chop($cpu_rpm);
}

# Memory
  $mem_total = `free | grep Mem | awk '{printf (\"%dMm\", \$2/1024 )}'`; chop($mem_total);
  $mem_free = `free | grep Mem | awk '{printf (\"%.0fMm\", ( \$3 -(\$6 + \$7) )/1024)}'`; chop($mem_free);
  $mem_per = `free -m | grep Mem | awk '{printf (\"%.0f\", (\$2 - \$3 + \$6 + \$7) * (100/ \$2))}'`; 
  $buffered_mem= `free | grep Mem | awk '{printf (\"%.0fM\", (\$6)/1024)}'`;
  $cached_mem= `free | grep Mem | awk '{printf (\"%.0fM\", (\$7)/1024)}'`;

# Hard Drive
  $hdspacet = `df -P | awk '{ sum+=\$2/1024^2 }; END { printf (\"%.02f\", sum )}'`;
  $hdspaceu = `df -P | awk '{ sum+=\$3/1024^2 }; END { printf (\"%.02f\", sum )}'`;
  $hdspacef = `df -P | awk '{ sum+=\$4/1024^2 }; END { printf (\"%.02f\", sum )}'`;
  $hdpercent = sprintf("%.0f",(100/${hdspacet}*${hdspacef}));
  $hdspacet = "${hdspacet}G"; $hdspaceu = "${hdspaceu}G"; $hdspacef = "${hdspacef}G";

# Network (default and others)
  $tmp="/tmp/.rinfo";
  system(`/sbin/route -n > $tmp`);
  open(ROUTE, "$tmp") or die;
  @rinfo = <ROUTE>;
  close(ROUTE) or die;
    foreach $line (@rinfo) {
      if($line=~/0.0.0.0/) {
        ($dest, $gate, $mask, $flags, $metric, $ref, $use, $face) = split(/\s+/, $line, 8);
        chomp($dest, $gate, $mask, $flags, $metric, $ref, $use, $face);
      } 
      if ($line=~/192.168/) {
        ($dest1, $gate1, $mask1, $flags1, $metric1, $ref1, $use1, $face1) = split(/\s+/, $line, 8);
        chomp($dest1, $gate1, $mask1, $flags1, $metric1, $ref1, $use1, $face1);
      };
  }
  system(`rm -rf $tmp`);
  if ($face1 eq $face)  {$face1=""}
  $ppp = `$lspci | grep Communication | sed 's/.*: //' | sed 's/ (.*//'`; chop($ppp);
  $eth = `$lspci | grep Ethernet | sed 's/.*: //' | sed 's/ (.*//'`; chop($eth);
  if ($face eq "ppp0") {$eth_name=$ppp; $inet="ppp0";} elsif ($face eq "ppp1") {$eth_name=$ppp; $inet="ppp1";} elsif ($face eq "ppp2") {$eth_name=$ppp; $inet="ppp2";}
  if ($face eq "eth0") {$eth_name=$eth; $inet="eth0";} elsif ($face eq "eth1") {$eth_name=$eth; $inet="eth1";} elsif ($face eq "eth2") {$eth_name=$eth; $inet="eth2";}
  if ($face1 eq "eth0") {$eth_name1=$eth; $inet1="eth0";} elsif ($face1 eq "eth1") {$eth_name1=$eth; $inet1="eth1";} elsif ($face1 eq "eth2") {$eth_name1=$eth; $inet1="eth3";}
  if ($face && $face1) {$facep=$face; $facee=$face1; $face="(${face}*, $face1)"} else {$face="(${face}*)";}
  if ($face eq "") { $face="Unknown Device";}

# Network Traffic
if ($inet && $inet1) {
  $packet_inp = `cat /proc/net/dev | grep $inet | awk -F: '/:/ {print \$2}' | awk '{printf \$1}'`;
  $packet_outp = `cat /proc/net/dev | grep $inet | awk -F: '/:/ {print \$2}' | awk '{print \$9}'`;
  $packet_ine = `cat /proc/net/dev | grep $inet1 | awk -F: '/:/ {print \$2}' | awk '{printf \$1}'`;
  $packet_oute = `cat /proc/net/dev | grep $inet1 | awk -F: '/:/ {print \$2}' | awk '{print \$9}'`;
  $packet_in = $packet_inp+$packet_ine;
  $packet_out = $packet_outp+$packet_oute;

    if($packet_inp < 1024**3) { $packet_inp = sprintf("%.02f",$packet_inp / 1024**2)."M"; } else { $packet_inp = sprintf("%.02f", $packet_inp / 1024**3)."G"; }
    if($packet_outp < 1024**3) { $packet_outp = sprintf("%.02f",$packet_outp / 1024**2)."M"; } else { $packet_outp = sprintf("%.02f", $packet_outp / 1024**3)."G"; }
    if($packet_ine < 1024**3) { $packet_ine = sprintf("%.02f",$packet_ine / 1024**2)."M"; } else { $packet_ine = sprintf("%.02f", $packet_ine / 1024**3)."G"; }
    if($packet_oute < 1024**3) { $packet_oute = sprintf("%.02f",$packet_oute / 1024**2)."M"; } else { $packet_oute = sprintf("%.02f", $packet_oute / 1024**3)."G"; }
    if($packet_in < 1024**3) { $packet_in = sprintf("%.02f",$packet_in / 1024**2)."M"; } else { $packet_in = sprintf("%.02f", $packet_in / 1024**3)."G"; }
    if($packet_out < 1024**3) { $packet_out = sprintf("%.02f",$packet_out / 1024**2)."M"; } else { $packet_out = sprintf("%.02f", $packet_out / 1024**3)."G"; }

  } elsif ($inet) {
  $packet_in = `cat /proc/net/dev | grep $inet | awk -F: '/:/ {print \$2}' | awk '{printf \$1}'`;
  $packet_out = `cat /proc/net/dev | grep $inet | awk -F: '/:/ {print \$2}' | awk '{print \$9}'`;
    if($packet_in < 1024**3) { $packet_in = sprintf("%.02f",$packet_in / 1024**2)."M"; } else { $packet_in = sprintf("%.02f", $packet_in / 1024**3)."G"; }
    if($packet_out < 1024**3) { $packet_out = sprintf("%.02f",$packet_out / 1024**2)."M"; } else { $packet_out = sprintf("%.02f", $packet_out / 1024**3)."G"; }
  } else {
  $packet_in = "0.00M";
  $packet_out = "0.00M";
}

# uptime stuff
  $uptime = `uptime`;
  chop ($uptime);
  $uptime =~ /.+ up (.+),.+[0-9]+ user/;
  $uptime = $1;

# best uptime related
  $buchk = `ls -asl ~/ | grep .buptime | wc -l`;
  if ($buchk == "0") { system("cat /proc/uptime | awk '{printf \$1}' > ~/.buptime_uptime"); system("echo \"$uptime\" > ~/.buptime"); }
  $cu = `cat /proc/uptime | awk '{printf \$1}'`; chomp($cu); 
  $bu = `cat ~/.buptime_uptime`; chomp($bu);
  $buptime = `cat ~/.buptime | sed 's/ //'`; chomp($buptime);

# Distro related
  if (-e "/etc/gentoo-release") {$distro = `cat /etc/gentoo-release | sed 's/version //'`;}
  if (-e "/etc/slackware-version") {$distro = `cat /etc/slackware-version | sed 's/version //'`;}
  if (-e "/etc/redhat-release") {$distro = `cat /etc/redhat-release | sed 's/version //'`;}
  if (-e "/etc/mandrake-release") {$distro = `cat /etc/mandrake-release | sed 's/version //'`;}
  if (-e "/etc/debian_version") {$distro = "Debian ".`cat /etc/debian_version | sed 's/version //'`."";}
  if (-e "/etc/SuSE-release") {$distro = `cat /SuSE-release | sed 's/version //'`;}
  if (-e "/etc/turbolinux-release") {$distro = `cat /etc/turbolinux-release | sed 's/version //'`;}
  if (-e "/etc/libranet_version") {$distro = "Libranet-`cat /etc/libranet_version | sed 's/version //'`";}
  if (-e "/etc/arch-release") {$distro = `cat /etc/arch-release | sed 's/version //'`;}
  if ($distro eq "") {$distro = "Unknown";} 
  chomp ($distro);

if ($switch eq "-gui") {
 if (!$do_gui) { system("echo \"Zenity is not installed\""); exit; }
 if ($do_gui) {
  system("killall -9 zenity; killall -9 tail"); $pid=0;
   $pid=open READER, "zenity --name=sysifo --list --column=\"Options\" --window-icon=/usr/share/pixmaps/xchat.png --title=\"SYSINFO\"  \"1 : all stats\" \"2 : short stats (same as all, minus de/wm etc)\" \"3 : cpu(s) info, +bogomips/load %\" \"4 : disk space\" \"5 : ram, +buffered/cached\" \"6: network dev(s), +in/out traffic for all\" \"7 : os/kernel version, +total processes\" \"8 : cpu temp\" \"9 : uptime, +best uptime\" \"10 : de/wm, xorg/xfree, vid card/res,depth&dri\" --width=385 --height=350 --text=\"Select the stats to view\"|" or die "Can't run the program";
  while(<READER>){
    $output.=$_;
  }
  close READER;
  if (!$output) { exit; }
  if ($output =~/1/) { $switch="- a"; } if ($output =~/2/) { $switch="- s"; }
  if ($output =~/3/) { $switch="- c"; } if ($output =~/4/) { $switch="- d"; }
  if ($output =~/5/) { $switch="- m"; } if ($output =~/6/) { $switch="- n"; }
  if ($output =~/7/) { $switch="- o"; } if ($output =~/8/) { $switch="- t"; }
  if ($output =~/9/) { $switch="- u"; } if ($output =~/10/) { $switch="- v"; }
 }
}

if ($switch =~/b/ or $switch =~/[e-g]/ or $switch =~/[i-l]/ or $switch =~/[p-r]/ or $switch =~/[w-z]/){
  $final = "";

} elsif ($switch =~/d/){
  $final = "[\cBDisk\cB]  $hdspaceu Used, $hdspacet Total ($hdpercent% Free)";

} elsif ($switch =~/m/){
  $final = "[\cBMemory\cB]  $mem_free Used, $mem_total Total ($mem_per% Free) ($buffered_mem Buffered, $cached_mem Cached)"; 

} elsif ($switch =~/s/){
  if ($cu > $bu) {system("echo \"$uptime\" > ~/.buptime"); system("echo \"$cu\" > ~/.buptime_uptime"); }
  if ($cache) {$cache="($cache) ";}
    $final = "[\cBOS\cB]  $distro; Kernel $uname :: [\cBUptime\cB] $uptime :: [\cBCPU\cB] $cpu_cnt-$cpuinf ${cache}$cpu_load :: [\cBMemory\cB] $mem_free Used, $mem_total Total ($mem_per% Free) :: [\cBDisk\cB] $hdspaceu Used, $hdspacet Total ($hdpercent% Free) :: [\cBNetwork\cB] $eth_name $face (Total: $packet_in In, $packet_out Out)";

} elsif ($switch =~/t/){
  if (!$cpu_temp and !$cpu_temp1 and !$cpu_rpm) {
    $final ="[\cBCPU  Temp\cB] No Sensors Found";
  } else {
    $final = "[\cBCPU  Temp\cB] $cpu_temp/$cpu_temp1 (fan: $cpu_rpm)";
}

} elsif ($switch =~/v/){
  if (!$vid_card and !$screen_res and !$screen_depth) {
    $final = "[\cBVideo\cB]  No Display Found";
  } else {
  $final = "[\cBVideo\cB]  ${de}/$x running on $vid_card @ $screen_res ($screen_depth bit$dri)";
}

} elsif ($switch =~/n/){
  if ($face && $face1) {
    $pinfo="($facep: $packet_inp In, $packet_outp Out :: $facee: $packet_ine In, $packet_oute Out)";
  } elsif ($face) {
    $pinfo="(Total: $packet_in In, $packet_out Out)";
}
  $final = "[\cBNetwork\cB]  $eth_name $pinfo";

} elsif ($switch =~/h/){
  $z="/sysinfo";
  $final = "[\cBcommands\cB]
   $z -a (all)
  $z -c (cpu/load%, +bogomips)
  $z -d (hdd's)
  $z -m (ram, +buffered/cached)
  $z -n (network, +default connection & lan traffic)
  $z -o (operating system/kernel version, +running processes)
  $z -s (short, same as -a but without video info)
  $z -t (cpu temperature)
  $z -u (uptime, +best uptime)
  $z -v (video/resolution, +Xorg/Xfree version; +de/wm)
  $z -gui (pops up a gtk2 menu with display options, alt to above switches)";

} elsif ($switch =~/o/){
  $pc = `ps ax | wc -l | awk '{print \$1 - 4}'`; chomp($pc);
  $final = "[\cBOS\cB]  $distro; Kernel $uname ($pc processes)";

} elsif ($switch =~/c/){
  if ($cache && $bogomips) {$cache="($cache/$bogomips bogomips)";} elsif ($cache) {$cache="($cache)";}
    $final="[\cBCPU\cB]  $cpu_cnt-$cpuinf $cache $cpu_load";

} elsif ($switch =~/u/){
  if ($cu > $bu) { system("echo \"$uptime\" > ~/.buptime"); system("echo \"$cu\" > ~/.buptime_uptime"); }
  $buptime = `cat ~/.buptime`; chomp($buptime);
  $final = "[\cBUptime\cB]  $uptime (Best: $buptime)";
} else {
  if ($cu > $bu) { system("echo \"$uptime\" > ~/.buptime"); system("echo \"$cu\" > ~/.buptime_uptime"); }
  if ($cache) {$cache="($cache) ";}
    if ($do_display == "1") {
      $final = "[\cBOS\cB]  $distro; Kernel $uname :: [\cBUptime\cB] $uptime :: [\cBCPU\cB] $cpu_cnt-${cpuinf} ${cache}${cpu_load} :: [\cBMemory\cB] $mem_free Used, $mem_total Total ($mem_per% Free) :: [\cBVideo\cB] $vid_card @ $screen_res ($screen_depth bit) :: [\cBDisk\cB] $hdspaceu Used, $hdspacet Total ($hdpercent% Free) :: [\cBNetwork\cB] $eth_name $face (Total: $packet_in In, $packet_out Out)";
    } else {
      $final = "[\cBOS\cB]  $distro; Kernel $uname :: [\cBUptime\cB] $uptime :: [\cBCPU\cB] $cpu_cnt-$cpuinf ${cache}$cpu_load :: [\cBMemory\cB] $mem_free Used, $mem_total Total ($mem_per% Free) :: [\cBDisk\cB] $hdspaceu Used, $hdspacet Total ($hdpercent% Free) :: [\cBNetwork\cB] $eth_name $face (Total: $packet_in In, $packet_out Out)";
    }	
  }

$final =~ s/ //;
print "$final\n";

sub cat {
  my @file; 
  open MYFILE, $_[0] or die "$!"; 
  @file = <MYFILE>; 
  close MYFILE; 
  return (wantarray) ? @file : join("",  @file);
}
