#!/bin/bash

function extend (){
    local str="$1"
    let spaces=60-${#1}
    while [ $spaces -gt 0 ]; do
        str="$str "
        let spaces=spaces-1
    done
    echo "$str"
}

function sec2time (){
    local input=$1

    if [ $input -lt 60 ]; then
        echo "$input seconds"
    else
        ((days=input/86400))
        ((input=input%86400))
        ((hours=input/3600))
        ((input=input%3600))
        ((mins=input/60))

        local daysPlural="s"
        local hoursPlural="s"
        local minsPlural="s"

        if [ $days -eq 1 ]; then
            daysPlural=""
        fi

        if [ $hours -eq 1 ]; then
            hoursPlural=""
        fi

        if [ $mins -eq 1 ]; then
            minsPlural=""
        fi

        echo "$days day$daysPlural, $hours hour$hoursPlural, $mins minute$minsPlural"
    fi
}

proccount=$(ps -Afl | wc -l)
proccount=$(expr ${proccount} - 2)
#GROUPZ=$(groups)
USER=$(whoami)
lastlogin=$(last ${USER} -F -i -2 | awk 'NR==2')
loginIP=$(echo ${lastlogin} | awk '{ print $3 }')
logintime=$(echo ${lastlogin} | awk '{ print $4,$5,$6,$7,$8 }')
if [[ $(echo ${lastlogin} | awk '{ print $9 }') == "-" ]]; then
    logouttime=$(echo ${lastlogin} | awk '{ print $10,$11,$12,$13,$14 }')
else
    logouttime=$(echo ${lastlogin} | awk '{ print $9,$10,$11 }')
fi
loadaverage=$(uptime | grep -E -o "load.+" | sed 's/load average: //g')
currentloginusers=$(uptime | grep -P -o "\d+ user" | grep -P -o "\d+")
uptime="$(sec2time $(cut -d "." -f 1 /proc/uptime))"
uptime="$uptime ($(date -d "@"$(grep btime /proc/stat | cut -d " " -f 2) +"%Y-%m-%d %H:%M:%S"))"
cpu=$(grep -i "Model name" /proc/cpuinfo 2>/dev/null | awk 'NR==1' | awk -F'[:]' '{ print $2 }' | sed 's/\s\+/ /g' | sed 's/^\s//g')
cpunumbers=$(grep -i "Model name" /proc/cpuinfo 2>/dev/null | wc -l)
mem=$(free -m | awk 'NR==2 { printf "Total: %sMB, Used: %sMB, Free: %sMB, Cache: %sMB",$2,$3,$4,$6; }')
diskspace=$(df -h ~ | awk 'NR==2 { printf "Total: %sB, Used: %sB, Free: %sB",$2,$3,$4; }')

clear
echo -e "\033[1;32m
\033[0;35m++++++++++++++++++++++++++: \033[0;37mSystem Data\033[0;35m :++++++++++++++++++++++++++++
+     \033[0;37mHostname \033[0;35m= \033[1;32m$(hostname)
\033[0;35m+      \033[0;37mAddress \033[0;35m= \033[1;32m$(hostname -I)
\033[0;35m+       \033[0;37mKernel \033[0;35m= \033[1;32m$(uname -r)
\033[0;35m+          \033[0;37mCPU \033[0;35m= \033[1;32m${cpu} x ${cpunumbers}
\033[0;35m+++++++++++++++++++++++: \033[0;37mLoad Information\033[0;35m :++++++++++++++++++++++++++
\033[0;35m+       \033[0;37mUptime \033[0;35m= \033[1;32m${uptime}
\033[0;35m+       \033[0;37mMemory \033[0;35m= \033[1;32m${mem}
\033[0;35m+   \033[0;37mDisk Usage \033[0;35m= \033[1;32m${diskspace}
\033[0;35m+ \033[0;37mLoad Average \033[0;35m= \033[1;32m${loadaverage}
\033[0;35m+    \033[0;37mProcesses \033[0;35m= \033[1;32m${proccount} of $(ulimit -u) MAX
\033[0;35m+++++++++++++++++++++++++++: \033[0;37mUser Data\033[0;35m :+++++++++++++++++++++++++++++
\033[0;35m+     \033[0;37mUsername \033[0;35m= \033[1;32m${USER} ( ${currentloginusers} online )
\033[0;35m+   \033[0;37mLast Login \033[0;35m= \033[1;32m${logintime} \033[0;37mto \033[1;32m${logouttime}
\033[0;35m+   \033[0;37mLogin From \033[0;35m= \033[1;32m${loginIP}
\033[0;35m+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\033[0m"
