#!/bin/sh
if [ -z $1 ]; then
    echo "Usage: $0 <test-path>"
    exit 1
fi

if ! echo "$1" | grep -qE "\.test$"; then
    echo "Usage: $0 <test-path>"
    echo "The file at <test-path> must end with '.test'"
    exit 1
fi

echo "Validating JSON of '$1'..."
if ! jq . "$1" >/dev/null; then
    exit 1
fi

destination_path=/etc/3d-p/iep-collector/tests.d/"$(basename "$1")"
echo "Installing '$(basename "$1")'..."

if [ -f "$destination_path" ]; then
    echo "A test named '$(basename "$1")' already exists and will be overwritten"
fi

if ! cp "$1" "$destination_path"; then
    exit 1
fi
echo "Installed '$(basename "$1")'"
