#!/bin/bash

set -eux
# set -o pipefail

PYTHON="$1"

# This one checks characters with their high bit set
"$PYTHON" coordinate --commands-file high-bit --use-threads

# This one checks even divisibility in ./coord-xargs-like
printf '1\n2\n' | ./coord-xargs-like -n 2 'echo' | "$PYTHON" coordinate --commands-file /dev/stdin --use-threads

# Check that the usage message doesn't error out.
"$PYTHON" coordinate --help

# Check 3 simple commands
printf 'tag1:date\ntag2:cat /etc/lsb-release\ntag3:hostname\n' | "$PYTHON" coordinate --commands-file - --use-processes

# Check exit statuses that aren't zero.
! printf 'tag1:/bin/sh -c "exit 0"\ntag2:/bin/sh -c "exit 1"\ntag3:/bin/sh -c "exit 2"\n' | \
    "$PYTHON" coordinate --commands-file /dev/stdin --use-threads --concurrency 3

# Check interleaved output
"$PYTHON" coordinate --commands-file ./spits --use-threads || true

# Check reverse order completion.
"$PYTHON" coordinate --verbose --commands-file ./sleeps --use-processes --concurrency 9

# Check that we are -not- on a tty - stdin should be /dev/null.
! printf 'tag:tty\n' | "$PYTHON" coordinate --commands-file /dev/stdin --use-threads

find /usr/local/cpython-3.7/share -type f -print | \
    head -10 | \
    ./coord-xargs-like -n 2 wc -l | \
    "$PYTHON" coordinate --concurrency '*4' --commands-file /dev/stdin --use-threads --verbose

find /usr/local/cpython-3.7/share -type f -print | \
    head -10 | \
    ./coord-xargs-like -n 3 wc -l | \
    "$PYTHON" coordinate --concurrency '*4' --commands-file /dev/stdin --use-threads --verbose

rm -rf output-lines
mkdir -p output-lines
"$PYTHON" coordinate --commands-file ./spits --use-threads --to-directory output-lines --erase-on-success || true
# shellcheck disable=SC2046,SC2012
(set -eu; cd output-lines && [ $(ls | wc -l) = 1 ])

rm -rf output-lines
mkdir -p output-lines
./exit-code-test

rm -rf output-lines
"$PYTHON" coordinate --commands-file ./spits --use-threads --to-directory output-lines --erase-on-success || true
# shellcheck disable=SC2046,SC2012
(set -eu; cd output-lines && [ $(ls | wc -l) = 1 ])

rm -rf output-lines
"$PYTHON" coordinate --commands-file ./spits --use-processes --to-directory output-lines || true
# shellcheck disable=SC2046,SC2012
(set -eu; cd output-lines && [ $(ls | wc -l) = 3 ])

rm -rf output-lines