#!/bin/sh

echo "Getting and setting attribute value from prepare-slot-bar hook"

# Set own (slot's) 'target' attribute
if ! snapctl set :bar target=slottarget; then
    echo "Expected prepare-slot-bar hook to be able to set the value of 'target' attribute"
    exit 1
fi

# Read own 'target' attribute
if ! output=$(snapctl get :bar target); then
    echo "Expected prepare-slot-bar hook 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 attribute of the plug set by prepare-plug- hook
if ! output=$(snapctl get --plug :bar target); then
    echo "Expected prepare-slot-bar hook 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

# Create (set) a completely new attribute
if ! snapctl set :bar newslotattribute=bar; then
    echo "Expected prepare-slot-bar hook to be able to create a new attribute"
fi