#!/bin/sh
mkdir -p "$SNAP_COMMON"
exec >> "$SNAP_COMMON/prepare-device-hook.log" 2>&1

if test -z "$MODEL_API_KEY"; then
    echo "API key is unset"
    exit 1
fi


if [ -f "$SNAP/serial" ]; then
    product_serial="$(cat "$SNAP/serial")"
else
    product_serial="i-have-no-serial-and-i-must-register"
fi

echo "product_serial:[$product_serial]"

snapctl set registration.proposed-serial="\"${product_serial}\""
snapctl set device-service.url="https://serial-vault-partners.canonical.com/v1/"
snapctl set device-service.headers="{\"api-key\": \"${MODEL_API_KEY}\"}"
