#!/bin/sh
#
# fnp - print input, but include filenames as headings
# prints concatenated standard input from files to standard output, prepending
# a filename title with an underline made of hyphen characters to each one.
#
# usage:
#   fnp FILE1 [FILE2...]
#   prog1 | fnp
#   prog1 | fnp FILE1 - FILE2

# assume stdin if no options given
if [ "$#" -eq 0 ]; then
  set -- -
fi

# iterate through arguments
for arg; do

  # we'll print the filename "-stdin-" rather than - just to be slightly more
  # explicit
  case ${arg} in
    -) fn=-stdin- ;;
    *) fn=${arg} ;;
  esac

  if [ -n "${tail}" ]; then
    printf '\n'
  fi
  tail=1

  # form the underline; is there a nicer way to do this in POSIX sh?
  ul=$(printf %s "${fn}" | tr '[:print:]' -)
  printf '%s\n%s\n\n' "${fn}" "${ul}"
  cat -- "${arg}"
done
