#!/bin/sh

set -e

if [ ! -x ./main ]; then
    make main
fi

if [ "$1" = "" ]; then
    echo "Usage: $0 <directory>"
    exit 1
fi

if [ ! -d "$1" ]; then
    echo "$0: $1 is not a directory"
    exit 1
fi

OUTPUT="$(echo "$1" | tr -d /).txt"
echo "$0: outputting to $OUTPUT"

touch "$OUTPUT"
find $1 -type f | while read i; do
    J="$(basename "$i")"
    if grep -q "$J" "$OUTPUT"; then
        continue
    fi
    if TMP="$(./main -5 "$i")"; then
        echo "$TMP" >> "$OUTPUT"
        echo "$TMP"
    else
        echo "@@@ FAILURE on $i @@@"
    fi
done