#!/bin/sh

# 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"
  echo "		Corrected errors with possible delay   : $2"
  echo "		Total errors                           : $3"
  echo "		Total errors corrected                 : $4"
  echo "		Total times correction algorithm used  : $5"
  tmp=`echo "($6 * 16777216 + $7) * 16777216 + $8" | bc`
  echo "		Total bytes processed                  : $tmp"
  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"
  echo "		Total uncorrected errors               : $9"

  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%"
  echo "		Host requested Mbytes written          : $7"
  echo "		Host requested kbytes written residual : $8"
  echo "		On tape Mbytes written                 : $9"
  shift 9
  echo "		On tape kbytes written residual        : $1"
}


# 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"
  echo "		Corrected errors with possible delay   : $2"
  echo "		Total errors                           : $3"
  echo "		Total errors corrected                 : $4"
  echo "		Total times correction algorithm used  : $5"
  tmp=`echo "($6 * 16777216 + $7) * 16777216 + $8" | bc`
  echo "		Total bytes processed                  : $tmp"
  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"
  echo "		Total uncorrected errors               : $9"

  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%"
  echo "		On tape Mbytes read                    : $5"
  echo "		On tape kbytes read residual           : $6"
}
