#!/bin/sh
#
# Copyright Dan Langille, 2006
# Original work from Andre Albsmeier and available for anyone to use provided
# you retain this notice in the script.
#
# See http://www.freebsddiary.org/tape-testing.php for more
# information
#

# Print some write statistics
#
dlt_write_statistics () {
  local tmp

  set -- `camcontrol cmd $tape -v -t 3 -c "4d 0 42 0 0 0 0 0 40 0" -i 64 \
    "{skip} *i4 \
     {skip} *i4 \
     {Corrected errors with substantial delay	} i4 \
     {skip} *i4 \
     {Corrected errors with possible delay      } i4 \
     {skip} *i4 \
     {Total errors                              } i4 \
     {skip} *i4 \
     {Total errors corrected                    } i4 \
     {skip} *i4 \
     {Total times correction algorithm processed} i4 \
     {skip} *i4 \
     {Total bytes processed                     } i2 i3 i3 \
     {skip} *i4 \
     {Total uncorrected errors                  } i4"
  `
  echo "		Corrected errors with substantial delay: $1" >&2
  echo "		Corrected errors with possible delay   : $2" >&2
  echo "		Total errors                           : $3" >&2
  echo "		Total errors corrected                 : $4" >&2
  echo "		Total times correction algorithm used  : $5" >&2
  tmp=`echo "($6 * 16777216 + $7) * 16777216 + $8" | bc`
  echo "		Total bytes processed                  : $tmp" >&2
  if [ $tmp != 0 ]; then
    tmp=`echo "$4 * 1073741824 / $tmp" | bc`
  else
    tmp='N/A'
  fi
  echo -n " $tmp" >> /tmp/backup_$tape-errors_per_gb
  echo "		Total corrected errors / GB            : $tmp" >&2
  echo "		Total uncorrected errors               : $9" >&2

  set -- `camcontrol modepage $tape -m 15 -P 0 | grep 'Data Compression Enabled:'`
  tmp="          "
  [ "x$4" = x0 ] && tmp="(disabled)"

  set -- `camcontrol cmd $tape -v -t 3 -c "4d 0 72 0 0 0 0 0 4c 0" -i 76 \
    "{skip} *i4 \
     {skip} *i4 \
     {Read compression ratio (* 100 %)       } i2 \
     {skip} *i4 \
     {Write compression ratio (* 100 %)      } i2 \
     {skip} *i4 \
     {Total host Mbytes reads                } i4 \
     {skip} *i4 \
     {Total host kbytes read residual        } i4 \
     {skip} *i4 \
     {On tape Mbytes read                    } i4 \
     {skip} *i4 \
     {On tape kbytes read residual           } i4 \
     {skip} *i4 \
     {Host requested Mbytes written          } i4 \
     {skip} *i4 \
     {Host requested kbytes written residual } i4 \
     {skip} *i4 \
     {On tape Mbytes written                 } i4 \
     {skip} *i4 \
     {On tape kbytes written residual        } i4"
  `

  echo "		Write compression ratio $tmp     : $2%" >&2
  echo "		Host requested Mbytes written          : $7" >&2
  echo "		Host requested kbytes written residual : $8" >&2
  echo "		On tape Mbytes written                 : $9" >&2
  shift 9
  echo "		On tape kbytes written residual        : $1" >&2
}


# Print some read statistics
#
dlt_read_statistics () {
  local tmp

  set -- `camcontrol cmd $tape -v -t 3 -c "4d 0 43 0 0 0 0 0 40 0" -i 64 \
    "{skip} *i4 \
     {skip} *i4 \
     {Corrected errors with substantial delay	} i4 \
     {skip} *i4 \
     {Corrected errors with possible delay      } i4 \
     {skip} *i4 \
     {Total errors                              } i4 \
     {skip} *i4 \
     {Total errors corrected                    } i4 \
     {skip} *i4 \
     {Total times correction algorithm processed} i4 \
     {skip} *i4 \
     {Total bytes processed                     } i2 i3 i3 \
     {skip} *i4 \
     {Total uncorrected errors                  } i4"
  `
  echo "		Corrected errors with substantial delay: $1" >&2
  echo "		Corrected errors with possible delay   : $2" >&2
  echo "		Total errors                           : $3" >&2
  echo "		Total errors corrected                 : $4" >&2
  echo "		Total times correction algorithm used  : $5" >&2
  tmp=`echo "($6 * 16777216 + $7) * 16777216 + $8" | bc`
  echo "		Total bytes processed                  : $tmp" >&2
  if [ $tmp != 0 ]; then
    tmp=`echo "$4 * 1073741824 / $tmp" | bc`
  else
    tmp='N/A'
  fi
  echo -n " $tmp" >> /tmp/backup_$tape-errors_per_gb
  echo "		Total corrected errors / GB            : $tmp" >&2
  echo "		Total uncorrected errors               : $9" >&2

  set -- `camcontrol modepage $tape -m 15 -P 0 | grep 'Data Compression Enabled:'`
  tmp="          "
  [ "x$4" = x0 ] && tmp="(disabled)"

  set -- `camcontrol cmd $tape -v -t 3 -c "4d 0 72 0 0 0 0 0 4c 0" -i 76 \
    "{skip} *i4 \
     {skip} *i4 \
     {Read compression ratio (* 100 %)       } i2 \
     {skip} *i4 \
     {Write compression ratio (* 100 %)      } i2 \
     {skip} *i4 \
     {Total host Mbytes reads                } i4 \
     {skip} *i4 \
     {Total host kbytes read residual        } i4 \
     {skip} *i4 \
     {On tape Mbytes read                    } i4 \
     {skip} *i4 \
     {On tape kbytes read residual           } i4 \
     {skip} *i4 \
     {Host requested Mbytes written          } i4 \
     {skip} *i4 \
     {Host requested kbytes written residual } i4 \
     {skip} *i4 \
     {On tape Mbytes written                 } i4 \
     {skip} *i4 \
     {On tape kbytes written residual        } i4"
  `

  echo "		Read compression ratio $tmp      : $1%" >&2
  echo "		On tape Mbytes read                    : $5" >&2
  echo "		On tape kbytes read residual           : $6" >&2
}

tape=$1

echo "The tape is '$1'"
echo "READING"
dlt_read_statistics
echo "WRITING"
dlt_write_statistics
