#! /bin/bash
# Last edited on 2012-12-08 21:29:39 by stolfilocal

PROG_NAME=${0##*/}
PROG_DESC="concatenate several sources, separated by file names and delimiter lines."
PROG_HELP=(
  "${PROG_NAME} [ -sep {STRING} ] {FILENAME}... > {OUTFILE}"
)

# Concatenates zero or more files into a single file. In the output,
# each file is preceded by the string "#FILE {FILENAME}", and followed
# by the separator {STRING}, both in separate lines.
# 
# The default separator is a line of 70 "~"s. Beware: if a chunk does
# not end with newline, the separator {STRINGS} will not be on a
# separate line.
# 
# See also "splitsep".

sep='~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
while [[ ( $# -ge 1 ) && ( "/$1" =~ /-.* ) ]]; do 
  if [[ ( $# -ge 2 ) && ( "/$1" == "/-sep" ) ]]; then
    sep="$2"; shift; shift;
  else
    echo "bad or incomplete option "'"'"$1"'"' 1>&2;
    echo "usage: ${PROG_HELP[@]}" 1>&2; exit 1;
  fi
done

files=( "$@" )
# echo "writing ${files[0]} ${files[1]} ... ${files[${#files} - 1]}" 1>&2;

for f in ${files[@]} ; do
  if [[ -r "${f}" ]]; then
    echo "#FILE ${f}"
    cat $f
    echo "${sep}"
  else
    echo "file '${f}' not found, skipped" 1>&2; 
  fi
done

