#!/bin/sh
#
# apf - prepend arguments from a file to the given arguments for a command

self=apf

# require at least two arguments
if [ "$#" -lt 2 ]; then
  printf >&2 '%s: Need an arguments file and a command\n' "${self}"
  exit 2
fi

# first argument is the file containing the null-delimited arguments
argf="$1"
cmd="$2"
shift 2

# if there were arguments given on the command line, we need to be careful and
# prepend our ones first
if [ "$#" -gt 0 ]; then

  # iterate through any remaining arguments
  for carg; do

    # if this is the first command argument, then before we add it, we'll
    # add all the ones from the file first if it exists
    if [ -n "${argf}" ]; then

      # reset the positional parameters
      set --

      # put our file arguments in first before we continue with the loop
      if [ -e "${argf}" ]; then
        while IFS= read -r farg; do
          case ${farg} in
            '#'*) continue ;;
            *[![:space:]]*) ;;
            *) continue ;;
          esac
          set -- "$@" "${farg}"
        done <"${argf}"
      fi

      # unset the argfile so we don't repeat this bit
      unset -v argf
    fi

    # stack the original invocation argument back onto the positional
    # parameters
    set -- "$@" "${carg}"
  done

# if there weren't, we can just read the file and slap them in
elif [ -e "${argf}" ]; then
  while IFS= read -r farg; do
    case ${farg} in
      '#'*) continue ;;
      *[![:space:]]*) ;;
      *) continue ;;
    esac
    set -- "$@" "${farg}"
  done <"${argf}"
fi

# run the command with the changed arguments
exec "${cmd}" "$@"
