#!/bin/bash

set -e -u -o pipefail
shopt -s inherit_errexit nullglob


fail() {
    printf '%s: error: %q %s\n' "${0##*/}" "$PWD/$1" "$2" >&2
    exit 1
}


fields=()
for file in fields/*; do
    if [[ $file == *.txt ]]; then
        [[ ! -x $file ]] || fail "$file" 'ends in .txt but is also executable'
        field=$(cat "$file")
    else
        field=$("$file")
    fi

    [[ $field != *$'\t'* ]] || fail "$file" 'contains a tab character'
    fields+=( "${field//$'\n'/$'\r'}" )
done
[[ ${#fields[@]} -gt 0 ]] || fail fields/ 'is empty'

# shellcheck disable=SC2145
split-browser-cmd autotype "${fields[@]}"$'\r'
