#!/bin/bash
# 2021
# 2021-07-05 corrected printing

while [ "$1" != "" ]; do
	echo ''
	smartctl -a "$1" | awk -v disk="$1" 'BEGIN {
		gsub(".*/", "", disk)
		abbr["Reallocated_Sector_Ct"]	= "Reall_Sec_Ct"
		abbr["Reallocated_Event_Count"]	= "Reall_Evn_Ct"
		abbr["Current_Pending_Sector"]	= "Cur_Pen_Sect"
		abbr["Offline_Uncorrectable"]	= "Offln_Uncorr"
		abbr["Reported_Uncorrect"]	= "Repor_Uncorr"
	}

	/test remain|Reallo|Pend| 1  Exte|Self.test.*prog|failure|without error|Uncorr/ {
		n++; no = 0; line = $0"\n"

		if (n > 10) no = 1
		if ($0 ~ "was completed without error") no = 1
		if ($0 ~ "no self-test has ever") no = 1
		if ($0 ~ "Self_test_in_progress") no = 0
		if ($0 ~ "( 24[1-9])") no = 1

		if ( (line ~ "Reallocated_Sector_Ct") || (line ~ "Reallocated_Event_Count") || (line ~ "Current_Pending_Sector") || (line ~ "Offline_Uncorrectable") || (line ~ "Reported_Uncorrect") ) {
			no = 1
			cline = cline"  "sprintf("%3d %-12s %3d", $1, abbr[$2], $NF)
		}
		if (!no) printf "%-5s %-s", disk, line
	} END {
		gsub("^ *", "", cline)
		printf "%-5s %-s\n", disk, cline
	}'
	shift
done
