#!/bin/bash

set -u

{
    printf 'tag1:/bin/sh -c "echo foo; exit 0"\n'
    printf 'tag2:/bin/sh -c "echo bar; exit 1"\n'
    printf 'tag3:/bin/sh -c "echo baz; exit 2"\n'
} \
    | ./coordinate --commands-file /dev/stdin --use-threads --concurrency 3 --to-directory output-lines --erase-on-success
exit_code="$?"

if [ "$exit_code" = 2 ]
then
    : good
else
    echo "$0: Exit code was not 2, but should be: $exit_code" 1>&2
    exit 1
fi

# shellcheck disable=SC2046
file_count=$(cd output-lines && find . -type f -print | wc -l)
if [ "$file_count" != 2 ]
then
    echo "$0: Number of files in output-lines was not 2, but should be" 1>&2
    exit 1
fi