#!/bin/sh
# Script by Ypnose - http://ywstd.fr

####################

LC_ALL=C
export LC_ALL

####################

check_command() {
  # Binaries ONLY. 'command' builtin returns functions or aliases.

  IFS=':'
  # shellcheck disable=SC2250
  for p in $PATH; do
    if [ ! -d "$p" ]; then
      continue
    fi
    if [ -x "${p}/${1}" ]; then
      return 0
    fi
  done
  return 1
}

display_hostname() {
  if check_command hostname; then
    SYS_HOST="$(hostname)"
  elif check_command uname; then
    SYS_HOST="$(uname -n)"
  fi
  printf "HOST:   %s\n" "${SYS_HOST:-UNKNOWN}"
}

display_date() {
  if check_command date; then
    SYS_DATE="$(date '+%Y-%m-%d %H:%M:%S')"
    printf "DATE:   %s" "${SYS_DATE}"
  fi
  printf "\n"
}

display_oskernel() {
  if check_command uname; then
    SYS_OS="$(uname -s)"
    SYS_KER="$(uname -r)"
    printf "OS:     %s\nKERNEL: %s\n" "${SYS_OS}" "${SYS_KER}"
  fi
}

display_uptimeload() {
  if check_command uptime; then
    uptime | awk '
  BEGIN { FS = "," }
  {
      gsub(/.*up +| +load averages?: /,"")
      printf("UPTIME: %s\nLOAD:   %s%s%s\n",
             $1, $(NF-2), $(NF-1), $NF)
  }
'
  fi
}

display_ram() {
  # shellcheck disable=SC2312
  [ "$(uname -s)" != "Linux" ] && return 1
  awk '
  BEGIN { b=1; u=5; t=5 }
  /^MemTotal:/ { memtotal = $2 / 1024; next }
  /^MemAvailable:/ { memavailable = $2 / 1024; next }
  /^SwapTotal:/ { swaptotal = $2 / 1024; next }
  /^SwapFree:/ { swapfree = $2 / 1024; next }
  END {
      memused = memtotal - memavailable
      memusedpc = memused * 100 / memtotal

      printf("RAM:    [");
      while (b <= 20) {
          # Used RAM
          if (u <= memusedpc) {
              printf("%s", "*")
              u=u+5
          }
          else
              printf("%s", " ")
          b++
      }
      printf("] ")
      # If the total memory is greater than 4096 MiB, we convert
      # the output to GiB
      if (memtotal >= 4096) {
          memtotal = memtotal / 1024
          memused = memused / 1024
          printf("%.1f/%.1f GiB", memused, memtotal)
      }
      else
          printf("%d/%d MiB", memused, memtotal)
      printf(" [%d%%]\n", memusedpc)

      if (swaptotal != "0") {
          b=1; u=5
          swapused = swaptotal - swapfree
          swapusedpc = swapused * 100 / swaptotal
          printf("SWAP:   [");
          while (b <= 20) {
              # Used swap
              if (u <= swapusedpc) {
                  printf("%s", "*")
                  u=u+5
              }
              else
                  printf("%s", " ")
              b++
          }
          printf("] ")
          # If the total swap is greater than 4096 MiB, we convert
          # the output to GiB
          if (swaptotal >= 4096) {
              swaptotal = swaptotal / 1024
              swapused = swapused / 1024
              printf("%.1f/%.1f GiB", swapused, swaptotal)
          }
          else
              printf("%d/%d MiB", swapused, swaptotal)
          printf(" [%d%%]\n", swapusedpc)
      }
  }
' /proc/meminfo
}

# Modify message
if ! check_command awk; then
  printf "%s\n" "awk binary not found on the system" >&2
  exit 1
fi

display_hostname
display_date
display_oskernel
display_uptimeload
display_ram
printf "\n"

exit
