#!/bin/bash

#function for terminal colors that supports color names and nested colors
color() {
    color="$1"
    shift
    text="$@"
    case "$color" in
        # text attributes
#        end) num=0;;
        bold) num=1;;
        special) num=2;;
        italic) num=3;;
        underline|uline) num=4;;
        reverse|rev|reversed) num=7;;
        concealed) num=8;;
        strike|strikethrough) num=9;;
        # foreground colors
        black) num=30;;
        D_red) num=31;;
        D_green) num=32;;
        D_yellow) num=33;;
        D_orange) num=33;;
        D_blue) num=34;;
        D_magenta) num=35;;
        D_cyan) num=36;;
        gray) num=37;;
        D_gray) num=30;;
        red) num=31;;
        green) num=32;;
        yellow) num=33;;
        orange) num=33;;
        blue) num=34;;
        magenta) num=35;;
        cyan) num=36;;
        # background colors
        B_black) num=40;;
        BD_red) num=41;;
        BD_green) num=42;;
        BD_yellow) num=43;;
        BD_orange) num=43;;
        BD_blue) num=44;;
        BD_magenta) num=45;;
        BD_cyan) num=46;;
        BL_gray) num=47;;
        B_gray) num=5;;
        B_red) num=41;;
        B_green) num=42;;
        B_yellow) num=43;;
        B_orange) num=43;;
        B_blue) num=44;;
        B_magenta) num=45;;
        B_cyan) num=46;;
        B_white) num=47;;
#        +([0-9])) num="$color";;
#        [0-9]+) num="$color";;
        *) num="$color";;
#        *) echo "$text"
#             return;;
    esac


    mycode='\033['"$num"'m'
    text=$(echo "$text" | sed -e 's,\[0m,\[0m\\033\['"$num"'m,g')
    echo -e "$mycode$text\033[0m"
}

singular_plural() {
    num="$1"
    singular="$2"
    plural="$3"
    if [[ "$num" -eq 1 ]]; then
        echo "1 $singular"
    else
        echo "$num $plural"
    fi
}

q="48;2;255;128;0"
q1=`color "$q" '    '`
q2='                                                                                '
me=$(whoami 2> /dev/null || who am i | awk '{print $1}')
hostname=$(hostname | awk '{printf "%-40s", $0}')
hostname=$(color blue "$hostname")
hostname=$(color reverse "$hostname")
#weather=$(color B_yellow $(curl http://wttr.in/?format=3 -s | cut -f2-3 -d" "))
#weather="$(printf '%-12s' $weather)"
uptime=$(uptime | sed 's/^.* up //' | sed 's/[0-9]* user.*$//' | tr -d , | awk '{printf "%-40s", $0}')
uptime=$(color reverse "$uptime")
kernel="$(uname -sr | awk '{printf "%-40s", $0}')"
kernel=$(color B_red "$kernel")
date=$(date | awk '{printf "%-40s", $0}')
date=$(color B_green "$date")

if top -bn1 | head -6 | grep 'KiB Mem' > /dev/null 2> /dev/null; then
    memory=$(top -bn1 | head -6 | grep 'KiB Mem' | awk '{print $6/1000 "M / " $4/1000 "M"}')
elif top -bn1 | head -6 | grep 'MiB Mem' > /dev/null 2> /dev/null; then
    memory=$(top -bn1 | head -6 | grep 'MiB Mem' | awk '{print $6/1000 "G / " $4/1000 "G"}')
else
    memory=$(top -b | grep Memory | awk '{print $4 " / " $2}')
fi
memory=$(echo "$memory" | awk '{printf "%-40s", $0}')
memory=$(color B_magenta "$memory")
processes=$(ps -A | wc -l | awk '{printf "%-40s", $0 " running"}')
processes=$(color B_cyan "$processes")
#ip_address=$(/sbin/ifconfig eth0 | /bin/grep "inet addr" | /usr/bin/cut -d ":" -f 2 | /usr/bin/cut -d " " -f 1 | awk '{printf "%-40s", $0}')
if type ip > /dev/null 2> /dev/null; then
    interface=$(ip addr show | awk '/inet.*brd/{print $NF; exit}')
    ip_address=$(ip addr show "$interface" | grep 'inet ' | awk '{print $2}' | cut -f1 -d/ | paste -s -d, | sed 's/,/, /g')
else
    ip_address=$(ifconfig -a | grep inet | awk '{print $2}' | tail -1)
fi
ip_address=$(echo "$ip_address" | awk '{printf "%-40s", $0}')
ip_address=$(color "$q" "$ip_address")
tmux_sessions=$(singular_plural $(tmux list-sessions 2>/dev/null | wc -l) session sessions)
tmux_windows=$(singular_plural $(tmux list-windows -a 2>/dev/null | wc -l) window windows)
tmux_clients=$(singular_plural $(tmux list-clients 2>/dev/null | wc -l) client clients)
tmux_status=$(echo "${tmux_sessions}, ${tmux_windows}, ${tmux_clients}" | awk '{printf "%-40s", $0}')
tmux_status=$(color B_green "$tmux_status")
logins=$(singular_plural $(who | awk '{print $1}' | sort -u | wc -l) "unique user" "unique users")
logins_me=$(singular_plural $(who | grep "^$me " | wc -l) "login" "logins")
login_status=$(echo "${logins}, $me has ${logins_me}" | awk '{printf "%-40s", $0}')
login_status=$(color red "$login_status")
login_status=$(color reverse "$login_status")
message1=$(echo "$1" | awk '{printf "%-58s", $0}')
message2=$(echo "$2" | awk '{printf "%-58s", $0}')
message1=$(color underline "$message1")
message2=$(color bold "$message2")
color "$q" "$q2"
color "$q" "$q2"
echo "\
$q1                                                                        $q1
$q1                                                                        $q1
$q1       $message1       $q1
$q1                                                                        $q1
$q1       hostname......... $hostname       $q1
$q1       ip address....... $ip_address       $q1
$q1       kernel........... $kernel       $q1
$q1       date and time.... $date       $q1
$q1       system uptime.... $uptime       $q1
$q1       free memory...... $memory       $q1
$q1       # of processes... $processes       $q1
$q1       tmux status...... $tmux_status       $q1
$q1       login status..... $login_status       $q1
$q1                                                                        $q1
$q1       $message2       $q1
$q1                                                                        $q1
$q1                                                                        $q1" \
| while read line; do
    :
    q0=`color B_white "$line"`
    color black "$q0"
done
color "$q" "$q2"
color "$q" "$q2"
