#!/usr/bin/env bash
# hello.sh: a greeting for the SSH login. It prints a framed banner with a greeting for the time of day, the NAS
# model, its uptime and load, the free space of the data volumes and a tip of the day, then ends.
# Run it: bash hello.sh   (NOW, SEED, PROC and DF may replace the clock, the tip's number, /proc and df -k)
# It uses nothing newer than Bash 3.2, the version of QTS's own /bin/sh.
set -uo pipefail

PROC=${PROC:-/proc}
NOW=${NOW:-$(date '+%Y-%m-%d %H:%M:%S')}
SEED=${SEED:-$(date '+%j')}
A=$'\e[38;5;214m' G=$'\e[32m' R=$'\e[31m' D=$'\e[2m' B=$'\e[1m' N=$'\e[0m'
W=76 # the width of the frame

edge() { # LEFT RIGHT TITLE: the top or the bottom edge
  local line
  if [[ -n $3 ]]; then
    printf -v line '%*s' $((W - 5 - ${#3})) ''
    line="═ $3 ${line// /═}"
  else
    printf -v line '%*s' $((W - 2)) ''
    line=${line// /═}
  fi
  printf '%s%s%s%s%s\n' "$A" "$1" "$line" "$2" "$N"
}

row() { # TEXT: one line inside the frame; \e[NG puts the right edge at column W whatever the text holds
  printf '%s║%s   %s\e[%dG%s║%s\n' "$A" "$N" "$1" "$W" "$A" "$N"
}

bar() { # PERCENT WIDTH: green up to 69 %, amber up to 89 %, red from 90 %
  local p=$1 w=$2 full colour=$G fill rest
  ((p >= 70)) && colour=$A
  ((p >= 90)) && colour=$R
  full=$((p * w / 100))
  printf -v fill '%*s' "$full" ''
  printf -v rest '%*s' $((w - full)) ''
  printf '%s%s%s%s%s' "$colour" "${fill// /█}" "$D" "${rest// /░}" "$N"
}

gib() { # KIB: the size in GiB with one decimal
  local t=$((($1 * 10 + 524288) / 1048576))
  printf '%d.%d' $((t / 10)) $((t % 10))
}

hour=$((10#${NOW:11:2}))
if ((hour >= 5 && hour < 12)); then
  greeting="Good morning"
elif ((hour >= 12 && hour < 18)); then
  greeting="Good afternoon"
elif ((hour >= 18 && hour < 22)); then
  greeting="Good evening"
else
  greeting="Working late"
fi

tips=(
  "getsysinfo cputmp prints the temperature of the CPU."
  "cat /proc/mdstat shows the state of every RAID group."
  "getcfg System Version prints the version of QTS."
  "ls /share lists the shared folders of the NAS."
  "df -h shows the size and the free space of every volume."
  "uptime prints how long the NAS has run and its load."
)
tip=${tips[$((10#$SEED % ${#tips[@]}))]}

model=$(getsysinfo model 2>/dev/null) || model=$(uname -n)
read -r up _ <"$PROC/uptime"
up=${up%.*}
read -r l1 l5 l15 _ <"$PROC/loadavg"
cpus=$(grep -c '^processor' "$PROC/cpuinfo")
cores=$(awk -F': ' '/^physical id/ {p = $2} /^core id/ {print p "-" $2}' "$PROC/cpuinfo" | sort -u | wc -l)

edge ╔ ╗ "Hello"
row ""
row "$A░▒▓█$N $B$greeting, ${USER:-$(id -un)}$N $A█▓▒░$N$(printf '\e[%dG' $((W - 19)))$D${NOW:0:16}$N"
row ""
row "$(printf '%-12s' Model)$B$model$N"
row "$(printf '%-12s' 'Up for')$((up / 86400)) d $((up % 86400 / 3600)) h $((up % 3600 / 60)) min"
row "$(printf '%-12s' CPU)$cores cores, $cpus logical processors"
row "$(printf '%-12s' Load)$l1  $l5  $l15  $D(1, 5 and 15 minutes)$N"
row ""
row "${B}Free space on the data volumes$N"
while read -r _ size used avail pct mnt; do
  [[ $mnt == /share/* && $mnt != /share/*/* ]] || continue
  row "$(printf '%-16s' "${mnt#/share/}")$(bar "${pct%\%}" 20)  $G$(gib "$avail") GiB$N free of $(gib "$size") GiB"
done < <(if [[ -n ${DF:-} ]]; then cat "$DF"; else df -k; fi)
row ""
row "${A}Tip$N  $tip"
row ""
edge ╚ ╝ ""
