#!/usr/app/TurboShell/bin/bash
# Małe narzędzie: stan woluminów, dysków i logu, na ekranie i w 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-ALBO-UWAGA TEKST
  printf '  %-5s  %s\n' "$1" "$2"
  if [[ $1 == UWAGA ]]; then
    bad=$((bad + 1))
  fi
}

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

  echo "Dyski"
  while read -r _ name _ _ _ _ _ _ _ raw _; do
    case $name in
      Reallocated_Sector_Ct | Current_Pending_Sector)
        if ((raw > 0)); then say UWAGA "$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 UWAGA "błędy: $errors"; else say ok "brak błędów"; fi
}

report >report.txt
cat report.txt
echo "wierszy do sprawdzenia: $bad"
((bad == 0))
