#!/usr/bin/perl

# Linux System Information Tool
# Coded by Trizen
# http://trizen.go.ro

$appname = 'lsit';
$app_ver = '0.1.3';

$z       = "\e[0m";
for ( $nr = 0 ; $nr <= $#ARGV ; ++$nr ) {
    push @argvs, $ARGV[$nr];
    if ( $ARGV[$nr] eq '--lines' ) {
        $lines = 1;
    }
    if ( $ARGV[$nr] =~ /-(H|ha)/ ) {
        $lshal = 1;
    }
    if ( $ARGV[$nr] =~ /-d/ ) {
        $dmesg = 1;
    }
    if ( $ARGV[$nr] =~ /-b/ and not $ARGV[$nr] =~ /^--bl/ ) {
        $bashset = 1;
    }
    if ( $ARGV[$nr] =~ /-c/ and not $ARGV[$nr] =~ /^--cy/ ) {
        $lscpu = 1;
    }
    if ( $ARGV[$nr] =~ /-p/ and not $ARGV[$nr] =~ /^--pu/ ) {
        $lspci = 1;
    }
    if ( $ARGV[$nr] =~ /-u/ ) {
        $lsusb = 1;
    }
    if ( $ARGV[$nr] =~ /-m/ ) {
        $lsmod = 1;
    }
    if ( $ARGV[$nr] =~ /-o/ ) {
        $others = 1;
    }
    if ( $ARGV[$nr] =~ /-a/ ) {
        $others  = 1;
        $lsusb   = 1;
        $dmesg   = 1;
        $lspci   = 1;
        $lsmod   = 1;
        $lscpu   = 1;
        $lshal   = 1;
        $bashset = 1;
    }
}
@colors = "
\e[0;34m --blue
\e[0;31m --red
\e[0;32m --green
\e[0;33m --yellow
\e[0;30m --black
\e[0;35m --purple
\e[0;36m --cyan
\e[0;37m --white
";
foreach $color (@colors) {
    next if $c1;
    foreach $arg (@argvs) {
        next if $c1;
        if ( $color =~ /(.+) $arg/ ) {
            $c1 = $1;
            $c2 = $c1;
            $c2 =~ s/\[0/[1/;
        }
    }
}
unless ($c1) {
    $c1 = "\e[0;31m";
    $c2 = $c1;
    $c2 =~ s/\[0/[1/;
}
$argvs = join( '', @argvs );
if ($argvs) {
    if ( $argvs =~ /[-]+nocolor/ ) {
        $c1 = '';
        $c2 = '';
        $z  = '';
    }
}
if ( $ARGV[0] ) {
    if ( $ARGV[0] =~ /^[-]+(h|help)$/ ) {
        &help;
    }
}
else {
    &help;
}

sub help {
    die "
${c2}Appname:$z $appname
${c2}Version:$z $app_ver

${c2}Usage:$z $0 [...]

${c2}Colors:$z
\t\e[1;34m--blue$z
\t\e[1;31m--red$z
\t\e[1;32m--green$z
\t\e[1;33m--yellow$z
\t\e[1;35m--purple$z
\t\e[1;36m--cyan$z
\t\e[1;37m--white$z
\t\e[1;30m--black$z
\t
${c2}Options:$z
\t$c2-a$z   :  all
\t$c2-b$z   :  bash settings
\t$c2-c$z   :  lscpu output
\t$c2-d$z   :  dmesg output
\t$c2-H$z   :  lshal output
\t$c2-m$z   :  lsmod output
\t$c2-p$z   :  lspci output
\t$c2-u$z   :  lsusb output
\t$c2-o$z   :  other informations

${c2}Other options:$z
\t$c2--lines$z        :  with '-' lines
\t$c2--nocolor$z      :  no color for text
\t$c2--screenshot$z   :  take a screenshot (with scrot)

";
}
(%wm) = (
    'awesome',   'Awesome',   'beryl',         'Beryl',
    'blackbox',  'Blackbox',  'compiz',        'Compiz',
    'dwm',       'DWM',       'enlightenment', 'Enlightenment',
    'fluxbox',   'Fluxbox',   'fvwm',          'FVWM',
    'i3',        'i3',        'weewm',         'WeeWM',
    'sawfish',   'Sawfish',   'qlwm',          'qLWM',
    'echinus',   'Echinus',   'w9wm',          'w9wm',
    'hackedbox', 'Hackedbox', 'scrotwm',       'Scrot WM',
    'twm',       'twm',       'vtwm',          'VtWM',
    '9wm',       '9wm',       'swm',           'swm',
    'flwm',      'FlWM',      'jwm',           'JWM',
    'icewm',     'IceWM',     'musca',         'Musca',
    'openbox',   'Openbox',   'pekwm',         'PekWM',
    'ratpoison', 'ratpoison', 'wmaker',        'WMaker',
    'wmfs',      'Wmfs',      'wmii',          'wmii',
    'xmonad',    'xmonad'
);
if ($lines) {
    print '-' x 80 . "\n";
}
print "\t" x 2 . '*' x 40 . "\n";
print "\t" x 2 . "     ${c2}Linux System Information Tool$z\n";
print "\t" x 2 . '*' x 40 . "\n";
if ($lshal) {
    @lshal = `lshal`;
    if (@lshal) {
        push @strings, 'title = [HAL devices]';
    }
    foreach $line (@lshal) {
        $line =~ s/ = /: /;
        $line =~ s/^[\s]+//;
        next if $line =~ /0x[\w]+|freedesktop|false|true|''/;
        push @strings, "$line";
    }
}
if ($dmesg) {
    @dmesg    = `dmesg`;
    $lastline = '';
    if (@dmesg) {
        push @strings, 'title = [Dmesg]';
    }
    foreach $line (@dmesg) {
        next if $line =~ /\.\.\.|0x[\w]+|\*[\d]+|fff/;
        $line =~ s/^[\s]+//;
        next unless $line;
        next if $line eq $lastline;
        push @strings, $line;
        $lastline = $line;
    }
}
if ($lspci) {
    @lspci = `lspci`;
    if (@lspci) {
        push @strings, 'title = [PCI buses and devices]';
    }
    foreach $line (@lspci) {
        chomp $line;
        $line =~ s/^([\d\.\:]*)(.?)([\d\.\:]*) //g;
        push @strings, $line;
    }
}
if ($lsusb) {
    @lsusb = `lsusb`;
    if (@lsusb) {
        push @strings, 'title = [USB Ports]';
    }
    foreach $line (@lsusb) {
        chomp $line;
        push @strings, $line;
    }
}
if ($lsmod) {
    @lsmod = `lsmod`;
    if (@lsmod) {
        push @strings, 'title = [Kernel Modules]';
    }
    foreach $line (@lsmod) {
        chomp $line;
        if ( $line =~ /([^\s]+)[\s]*[\d]+[\s]*(.+)/ ) {
            $name = $1;
            $mods = $2;
            next if $mods eq '0 ';
            push @strings, "${name}:$mods";
        }
    }
}
if ($lscpu) {
    @lscpu = `lscpu`;
    $lscpu = join( '', @lscpu );
    unless ($lscpu) {
        $lscpu = '';
    }
    open FILE, '/proc/cpuinfo';
    while ( defined( $_ = <FILE> ) ) {
        if ( $_ =~ /([^:]+):(.+)/ ) {
            $opt   = $1;
            $value = $2;
            $opt =~ s/(.)(.+)/\U$1\E\L$2\E/;
            $opt =~ s/[\s]+$//;
            $opt =~ s/_/ /g;
            unless ( $lscpu =~ /$opt/i ) {
                push @lscpu, "${opt}: $value";
            }
        }
    }
    close FILE;
    if (@lscpu) {
        push @strings, 'title = [CPU]';
    }
    foreach $line (@lscpu) {
        chomp $line;
        push @strings, $line;
    }
}
if ($bashset) {
    $set = `/bin/bash -c set > /tmp/set.tmp`;
    open FILE, '/tmp/set.tmp';
    @sets = <FILE>;
    close FILE;
    if (@sets) {
        push @strings, 'title = [Bash settings]';
    }
    foreach $set (@sets) {
        chomp $set;
        $set =~ s/\(\)//g;
        next if $set =~ /\}|\]|\:|\$/;
        if ( $set =~ /^([^=]+)=(.+)/ ) {
            $opt   = $1;
            $value = $2;
            next if $opt   =~ /^_$|COOKIE|^PS([\d]+)|^XDG|^XAUTH|BASH_EXE/;
            next if $value =~ /^[\d]{1}$/;
            $opt = "\L$opt\E";
            $opt =~ s/^(.)(.+)/\U$1\E\L$2\E/;
            push @strings, "${opt}: $value";
        }
    }
}
if ($others) {
    push @strings, 'title = [Other informations]';
    $arch = `uname -m`;
    if ( -e '/etc/arch-release' ) {
        $distro = "Arch Linux $arch";
    }
    else {
        chomp( $distro = `uname -o` );
        $distro .= " $arch";
    }
    unless ( $arch or $distro ) {
        $distro = "Unknown OS\n";
    }
    if ($distro) {
        push @strings, "OS: $distro";
    }
    $psx = `ps -A`;
    while ( ( $wm_process, $wm_name ) = each %wm ) {
        if ( $psx =~ /[\W]+$wm_process/ ) {
            $WM = $wm_name;
            last;
        }
    }
    if ($WM) {
        push @strings, "WM: $WM";
    }
    elsif ( $psx =~ /xfce4-session/ ) {
        push @strings, "DE: XFCE4\n";
    }
    elsif ( $psx =~ /lxsession/ ) {
        push @strings, "DE: LXDE\n";
    }
    elsif ( $psx =~ /gnome-session/ ) {
        push @strings, "DE: GNOME\n";
    }
    elsif ( $psx =~ /ksmserver/ ) {
        push @strings, "DE: KDE\n";
    }
    push @strings, 'Kernel: ' . `uname -r`;
    push @strings, 'Hostname: ' . `uname -n`;
    open FILE, '/etc/inittab';
    @file = <FILE>;
    close FILE;
    foreach $line (@file) {
        if ( $line =~ /^id:([\d]+):initdefault/ ) {
            push @strings, "Default init: $1";
        }
    }
    $logname = `whoami`;
    push @strings, "Logname: $logname";
    $ram = `free`;
    if ( $ram =~ /cache:[\s]+([\d]+)/ ) {
        $usedram = $1;
        $usedram = $usedram / 1024;
        $usedram =~ s/([\d]+)([\.]*)(.*)$/$1/;
    }
    else {
        $usedram = '';
    }
    if ( $ram =~ /Mem:[\s]+([\d]+)/ ) {
        $totalram = $1;
        $totalram = $totalram / 1024;
        $totalram =~ s/([\d]+)([\.]*)(.*)$/$1/;
    }
    unless ($usedram) {
        if ( $ram =~ /Mem:([\s]+)([\d]+)([\s]*)([\d]+)/ ) {
            $usedram = $4;
            $usedram = $usedram / 1024;
            $usedram =~ s/([\d]+)([\.]*)(.*)$/$1/;
        }
    }
    push @strings, "RAM: $usedram MB / $totalram MB";
    @df = `df -TPh`;
    foreach $df (@df) {
        if ( $df =~
m[([\w]+)[\s]*([\w\.]+)[\s]*([\w\.]+)[\s]*([\w\.]+)[\s]*([\d]+)\% /(.*)]
            and not $1 =~ /tmpfs/ )
        {
            $type        = $1;
            $totalsize   = $2;
            $used        = $3;
            $usedprocent = " ($5%)";
            $mountpoint  = "/$6";
        }
        if ($mountpoint) {
            $mountpoint =~ s[(.*)/(.+)$][$2];
            $mountpoint =~ s/(.+)/\u\L$1\E/g;
            if ( $mountpoint eq '/' ) {
                $mountpoint = 'Root';
            }
            if ( $usedprocent eq ' (100%)' ) {
                $usedprocent = '';
            }
            push @ps,
              " $c2 ${mountpoint}:$z $used / $totalsize$usedprocent ($type)";
        }
    }
    @ps            = sort @ps;
    $lastpartition = '';
    foreach $partition (@ps) {
        next if $partition eq $lastpartition;
        push @partitions, $partition;
        $lastpartition = $partition;
    }
    open CPU, '/proc/cpuinfo';
    @cpu = <CPU>;
    close CPU;
    foreach $cpu (@cpu) {
        if ( $cpu =~ /model name[\s]*: (.+)/ ) {
            $CPU = $1;
            if ( $CPU =~ /([^\@]+)/ ) {
                $CPU = $1;
                $CPU =~ s/[\s]+$//;
            }
            next;
        }
        if ( $cpu =~ /cpu MHz[\s]*: ([\d\.]+)/ ) {
            $freq = $1;
            last;
        }
    }
    unless ($CPU) {
        chomp( $CPU = `uname -p` );
    }
    if ($CPU) {
        push @strings, "CPU: $CPU";
    }
    if ($freq) {
        if ( $freq == 0 ) {
            $freq = '';
        }
    }
    if ($freq) {
        push @strings, "CPU Freq: $freq";
    }
    if ( `uptime` =~ /up[\s]*(.+)\, / ) {
        $uptime = $1;
        if ( not $uptime =~ /day/ ) {
            $uptime =~ s/,(.+)//;
        }
        else {
            $uptime =~ s/^([^\,]+),[\s]+([^\,]+).+/$1 and $2/;
        }
    }
    push @strings, "Uptime: $uptime";
    open FILE, "$ENV{'HOME'}/.gtkrc-2.0";
    while ( defined( $_ = <FILE> ) ) {
        if ( $_ =~ /gtk-theme-name=['"]([^"']+)/ ) {
            push @strings, "GTK Theme: $1";
        }
        if ( $_ =~ /gtk-font-name=['"]([^"']+)/ ) {
            push @strings, "Font name: $1";
        }
        if ( $_ =~ /gtk-icon-theme-name=['"]([^"']+)/ ) {
            push @strings, "Icon theme: $1";
        }
    }
    close FILE;
    if ( $distro =~ /Arch Linux/ or -e '/etc/frugalware-release' ) {
        $pkgs = `pacman -Q | wc -l`;
    }
    elsif ( -r '/var/log/packages/' ) {
        $pkgs = `ls /var/log/packages/ | wc -l`;
    }
    if ($pkgs) {
        push @strings, "Packages: $pkgs";
    }
    if ( -e '/usr/bin/sensors' ) {
        @sensors = `sensors`;
        foreach $line (@sensors) {
            if ( $line =~ /Temp:/ ) {
                push @strings, "$line\n";
            }
        }
    }
    @hddtemp_paths = (
        '/usr/sbin/hddtemp', '/usr/bin/hddtemp',
        '/sbin/hddtemp',     '/bin/hddtemp'
    );
    foreach $hddtemp_path (@hddtemp_paths) {
        if ( -e $hddtemp_path ) {
            $hddtemp = $hddtemp_path;
            last;
        }
    }
    if ( $logname eq "root\n" and $hddtemp ) {
        $df = `df`;
        if ( $df =~ m[/dev/(.)da([\d]*)] ) {
            $dev  = "/dev/$1da$2";
            $temp = `$hddtemp $dev`;
            $temp =~ s/^[^:]+:[\s]*//;
            push @strings, $temp;
        }
    }
}
foreach $string (@strings) {
    chomp $string;
    if ( $string =~ /title = \[(.+)\]/ ) {
        if ($lines) {
            print '-' x 80 . "\n";
        }
        print "\n" . 'x' x 80 . "\n";
        print "$c2 \t\t\t\t $1 $z\n";
        print 'x' x 80 . "\n\n";
        next;
    }
    if ( $string =~ /^([^:]+):[\s]*(.+)/ ) {
        if ($lines) {
            print '-' x 80 . "\n";
        }
        print "$c2* $1:$z $2\n";
    }
}
if ($lines) {
    print '-' x 80 . "\n";
}
if ($others) {
    if (@partitions) {
        print "$c2* Partitions:$z\n";
    }
    $count = @partitions;
    for ( $nr = 0 ; $nr <= $count ; ++$nr ) {
        if ( $partitions[$nr] ) {
            print ">$partitions[$nr]$z\n";
        }
    }
    if ($lines) {
        print '-' x 80 . "\n";
    }
}
print "\n";
foreach $argv (@argvs) {
    if ( $argv eq '--screenshot' ) {
        exec 'scrot -cd 5';
    }
}
exit;

