#!/bin/sh

TRY_TOP="${TRY_TOP-$(git rev-parse --show-toplevel --show-superproject-working-tree)}"
TRY="$TRY_TOP/try"

cleanup() {
    cd /

    if [ -d "$try_workspace" ]
    then
        rm -rf "$try_workspace" >/dev/null 2>&1
    fi

    if [ -d "$try_example_dir" ]
    then
        rm -rf "$try_example_dir"
    fi
}

trap 'cleanup' EXIT

try_workspace="$(mktemp -d)"
cd "$try_workspace" || return 9

cp "$TRY_TOP/test/resources/file.txt.gz" "$try_workspace/"

# Set up expected output
touch expected1.txt
echo 'test' >expected2.txt

echo 'fail' >file_2.txt
touch target

try_example_dir=$(mktemp -d)
"$TRY" -D "$try_example_dir" "touch file_1.txt; echo test >file_2.txt; rm file.txt.gz; rm target; mkdir target; mkdir new_dir" || return 1
"$TRY" summary "$try_example_dir" >summary.out || return 2

# Check that the summary correctly identifies every change
grep -qx -e "$PWD/file_1.txt (added)"         summary.out || return 3
grep -qx -e "$PWD/file_2.txt (modified)"      summary.out || return 4
grep -qx -e "$PWD/file.txt.gz (deleted)"      summary.out || return 5
grep -qx -e "$PWD/target (replaced with dir)" summary.out || return 6
grep -qx -e "$PWD/new_dir (created dir)"      summary.out || return 7
