#!/bin/sh

set -ue

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

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

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

# Read 'before-connect' attribute of the plug
if ! output=$(snapctl get --plug :producer before-connect); then
    echo "Expected connect-slot-producer to be able to read the value of 'before-connect' attribute of the plug"
    exit 1
fi
expected_output="plug-changed(consumer-value)"
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 consumer-attr-1; then
    echo "Expected snapctl get to fail on unknown slot"
    exit 1
fi

# Attributes cannot be set in connect- hooks
if snapctl set :producer consumer-attr-4=foo; then
    echo "Expected snapctl set to fail when run from connect-slot hook"
    exit 1
fi

touch "$SNAP_COMMON/connect-slot-producer-done"
