#!/bin/bash
#============================================================================
# Title       : diskheader
# Description : Shows hexdump of the first 1k block of a disk
# Author      : Bart Sjerps <bart@dirty-cache.com>
# License     : GPLv3+
# ---------------------------------------------------------------------------

# Translate -? or --help into manpage
case $1 in
  -\?|--help) man $(basename $0) ; exit ;;
esac

#============================================================================
# Initialization - Logging - Etc.
# ---------------------------------------------------------------------------
die() { echo "$(basename $0): [die] $@" >&2 ; exit 10 ; }

test -x /usr/bin/xxd || die "Requires xxd (vim-common)"

#============================================================================
# Main section - parsing options etc
# ---------------------------------------------------------------------------
while getopts ":hb:c:w:" OPT; do
  case "$OPT" in
      b) blksz=${OPTARG} ;;
      c) count=${OPTARG} ;;
      w) width=${OPTARG} ;;
  esac
done
shift $(expr $OPTIND - 1)

test -z "$1" && die "no disk specified"
test -b "$1" || die "$1 is not a block device"

dd if="$1" bs=${blksz:-1024} count=${count:-1} status=none | xxd -c ${width:-32}
