#!/bin/sh

# This script abstracts my most common use of 'bar'. I.e. spilling the
#  contents of a file or blockdev to stdout while progressbar'ing.
# Requires clpbar (app-misc/clpbar) (http://clpbar.sourceforge.net)

pebkac() {
  [ "${*}" ] && printf "%s\n\n" "${*}" >&2
  echo "Usage: $(basename "${0}") <filename>" >&2
  echo "" >&2
  echo "Prints a pretty ASCII progress bar to stderr while" >&2
  echo " catting the contents of the specified file to stdout." >&2
  exit 1
}

file="${1}"
[ -z "${file}" -o -d "${file}" ] && pebkac
type="$(stat -c "%F" "${file}")"

case "${type}" in
regular\ file) size="$(stat -c "%s" "${file}")" ;;
block\ special\ file) size="$(( $(/sbin/blockdev --getsz "${file}") * 512 ))" ;;
*) pebkac "Unrecognized type '${type}'" ;;
esac

exec bar -dan -s "${size}" -if "${file}"