#!/bin/sh

echo "Getting attributes from connect-slot-bar hook"

# Read 'newplugattribute' attribute of the plug
if ! output=$(snapctl get --plug :bar newplugattribute); then
    echo "Expected connect-plug-foo be able to read the value of the 'newplugattribute' attribute of the plug"
    exit 1
fi
expected_output="foo"
if [ "$output" != "$expected_output" ]; then
    echo "Expected output to be '$expected_output', but it was '$output'"
    exit 1
fi

# Read own 'target' attribute
if ! output=$(snapctl get :bar target); then
    echo "Expected connect-slot-bar to be able to read the value of own 'target' attribute"
    exit 1
fi
expected_output="slottarget"
if [ "$output" != "$expected_output" ]; then
    echo "Expected output to be '$expected_output', but it was '$output'"
    exit 1
fi

# Read 'target' attribute of the plug
if ! output=$(snapctl get --plug :bar target); then
    echo "Expected connect-slot-bar to be able to read the value of 'target' attribute of the plug"
    exit 1
fi
expected_output="plugtarget"
if [ "$output" != "$expected_output" ]; then
    echo "Expected output to be '$expected_output', but it was '$output'"
    exit 1
fi

# Failure on unknown slot
if snapctl get :unknown target; then
    echo "Expected snapctl get to fail on unknown slot"
    exit 1
fi

# Attributes cannot be set in connect- hooks
if snapctl set :bar target=slottarget; then
    echo "Expected snapctl set to fail when run from connect-slot hook"
    exit 1
fi
