#!/usr/app/TurboShell/bin/bash
# A small tool: the state of volumes, disks and the log, on the screen and in report.txt
set -euo pipefail

limit=$(sed -n 's/^limit=//p' config.ini)
title=$(sed -n 's/^title=//p' config.ini)
bad=0

say() { # OK-OR-LOOK TEXT
  printf '  %-4s  %s\n' "$1" "$2"
  if [[ $1 == LOOK ]]; then
    bad=$((bad + 1))
  fi
}

report() {
  local percent mount name raw errors
  echo "$title"
  echo "Volumes"
  while read -r _ _ _ _ percent mount; do
    [[ $percent == *% ]] || continue
    if ((${percent%\%} >= limit)); then
      say LOOK "$mount $percent used"
    else
      say ok "$mount $percent used"
    fi
  done <df.txt

  echo "Disks"
  while read -r _ name _ _ _ _ _ _ _ raw _; do
    case $name in
      Reallocated_Sector_Ct | Current_Pending_Sector)
        if ((raw > 0)); then say LOOK "$name $raw"; else say ok "$name $raw"; fi
        ;;
    esac
  done <smartctl.txt

  echo "Log"
  errors=$(grep -c ' ERROR ' nas.log || true)
  if ((errors > 0)); then say LOOK "$errors errors"; else say ok "no errors"; fi
}

report >report.txt
cat report.txt
echo "$bad lines need a look"
((bad == 0))
