#!/bin/sh
set -e

if jq -e '.["usb-ports"]' /run/system.json > /dev/null; then
    usb_ports=$(jq -r '.["usb-ports"] | map(.name) | unique | join(" ")' /run/system.json)
else
    usb_ports=""
fi

gen_port()
{
    port="$1"
    cat <<EOF
{
    "class": "infix-hardware:usb",
    "name": "$port",
    "state": {
	"admin-state": "unlocked"
    }
}
EOF
}
first=1
cat <<EOF
{
  "ietf-hardware:hardware": {
    "component": [
EOF
for port in $usb_ports; do
    if [ $first -eq 0 ]; then
	echo -n ','
    fi
    first=0;
    gen_port "$port"
done
cat <<EOF
]}
}
EOF
