#!/bin/sh
#
# com - [-n] [FILE...] - compile anything
#
##% echo this is com % \#
##% echo $FILE $STEM
#
# ---
# inspired by a tool of Tom Duff
# see: https://iq0.com/duffgram/com.html
# see: http://iq0.com/duffgram/com.c
#
# com looks for the sequence ##% (without preceding whitespace) in each file,
# and sends the rest of the line off to the shell.
# The variable $FILE is set to the filename, and $STEM is set to the filename
# without suffix.
# Used to allow information about how to compile a program to be stored with
# the program.
# The -n flag causes com not to act, but to print out the action it would have
# taken.
#
# +chris+

cmd="sh -e -u -x"
if [ "$1" = -n ]; then
  cmd="cat"
  shift
fi

# shellcheck disable=SC2046,SC2312
if [ "$#" -eq 0 ]; then
  if [ -r .comfile ]; then
    set -- $(cat .comfile)
  else
    echo "Usage: $(basename "$0") [-n] [FILE...]" 1>&2
    exit 1
  fi
elif [ "${cmd}" != cat ]; then
  echo "$@" >.comfile
fi

export FILE STEM
for FILE; do
  STEM=${FILE%.*}
  if ! grep -q '^[^ ]*##%' "${FILE}"; then
    echo "File '${FILE}' contains no ##% instructions."
    exit 1
  fi
  sed -n '/^[^ ]*##%/s:^[^ ]*##% *::gp' "${FILE}" | ${cmd}
done
