#!/bin/bash
if [ -f /var/run/motd ]; then echo >/var/run/motd ; fi
if [ -f /var/run/motd.dynamic ]; then echo >/var/run/motd.dynamic ; fi
if [ -f /etc/motd ]; then echo >/etc/motd ; fi
if grep -Fxq "/usr/bin/motd" /etc/profile ; then clear ; else echo "/usr/bin/motd" >>/etc/profile ; fi
#parameters
interface=eth0
load=`ps aux|awk 'NR > 0 { s +=$3 }; END {print s"%"}'`
cpuspeed=$(if [ "$(which cpufreq-info)" ]; then echo `cpufreq-info | grep "current CPU frequency" | awk '{ print $5 $6}' | head -1`; else echo "N/A"; fi)
xfactor=`echo "x"`
cores=$(if [ "$(which cpufreq-info)" ]; then echo `cpufreq-info | grep "current CPU" | wc -l`; else echo "N/A"; fi)
cputotal=`echo "$cores$xfactor@$cpuspeed"`
cputemp=$(if [ -d  "/sys/devices/virtual/thermal/thermal_zone0/" ]; then echo "`cat /sys/devices/virtual/thermal/thermal_zone0/temp | awk '{ printf ("%0.1f°C\n",$1/1000 c); }'`" ; else echo "N/A" ; fi)
memory_usage=`free -m | awk '/Mem/ { printf("%3.1f%%", $3/$2*100) }'`
swap_usage=`free -m | awk '/Swap/ { printf("%3.1f%%", $3/$2*100) }'`
process=`ps -ef | wc -l`
ip=`ifconfig $interface | awk '/inet addr/{print substr($2,6)}'`
rx=`ifconfig $interface | grep "RX bytes" | awk '{ print $3 $4}'`
tx=`ifconfig $interface | grep "RX bytes" | awk '{ print $7 $8}'`
smartmon=$(if [ "$(which smartctl)" ]; then echo `smartctl -H /dev/sda | grep result | awk '{ print $6 }'`; else echo "N/A"; fi)
sd_health=`dmesg | grep "corruption" | wc -l | if [ $? -gt 0 ]; then echo "CORRUPTION"; else echo "PASSED"; fi`
hdd_temp=$(if [ "$(which smartctl)" ]; then echo `smartctl -a /dev/sda | grep Celsius | awk '{print $10 "°C"}'`; else echo "N/A"; fi)
hdd_usage=`df $(df | grep -E '(root|sd[a-z][0-9]|mmcblk0p[1-9])' | awk '{print $6}') | awk '/\// {print $(NF-1)}'`
NOW=$(date +"%m-%d-%Y-%T")
[ -r /etc/lsb-release ] && . /etc/lsb-release

if [ -z "$DISTRIB_DESCRIPTION" ] && [ -x /usr/bin/lsb_release ]; then
        # Fall back to using the very slow lsb_release utility
        DISTRIB_DESCRIPTION=$(lsb_release -s -d)
fi
printf "\n"
toilet -f pagga    $(hostname) --metal
printf "\n"
printf "Welcome to %s (%s).\n" "$DISTRIB_DESCRIPTION" "Kernel Version: $(toilet -f term --metal `uname -r`)" 
printf "\n"
printf " System load: %s\t\t CPU Speed: %s\t\t CPU Temp: %s\t\n" $(toilet -f term --metal $load $cputotal $cputemp)
printf " Mem Usage: %s\t\t Swap Usage: %s\t\t Processes: %s\t\n" $(toilet -f term --metal $memory_usage $swap_usage $process)
printf " IP: %s\t\t\t Download (RX): %s\t Upload (TX): %s\t\n" $(toilet -f term --metal $ip $rx $tx)
printf " SD (Health): %s\t\t HDD (Health): %s\t\t HDD temperature: %s\t\n" $(toilet -f term --metal $sd_health $smartmon $hdd_temp)
printf "\n"
printf "Harddrive Information:\n"
printf "\n"
printf " SD: %s\t\t\t SDA1: %s\t\t\t SDB1: %s\t\t  SDC: %s\t\n" $(toilet -f term --metal $hdd_usage)
printf "\n"
printf "Checking packages to install (this might take some time)..\n\n"
if [ "$(which apt-get)" ]; then echo "`apt-get -s -o Debug::NoLocking=true upgrade | grep ^Inst | wc -l` updates to install." ; fi
if [ "$(which npm)" ]; then nodejs=`npm -g outdated | wc -l` && echo "$nodejs npm updates to install." ; fi
if [ "$(which pip)" ]; then pypi=`pip list -q --outdated > /dev/null 2>&1 | wc -l` && echo "$pypi pip updates to install."; fi
printf "\n"
if [ "$(which uprecords)" ]; then uprecords | grep -E "(->\s+\w{1,2})" | cut -c 5- | awk '{ print  "current uptime: " $3, $4, $5 }'; fi
if [ "$(which uprecords)" ]; then uprecords | grep -E "(up\s+\w+\sday(s)?)" | cut -c 5- | awk '{ print  "total uptime:   " $2, $3, $4 }'; fi
printf "\n"
printf "Snapshot generated at: %s\t" $(toilet -f term --metal $NOW)
printf "\n\n"