#!/usr/bin/env bash set -euo pipefail source functions.sh application_name=tom-hue-stuff read_vars if [[ -z $var_ip || -z $var_bridgeid ]]; then echo >&2 "Variables ip and bridgeid required in my_bridge.txt" exit 1 fi echo "Registering a new user on the Hue Bridge (using API V1)." echo "Application name: $application_name" read -r -p "Device name (e.g. iphone peter): " device_name if [[ -z $device_name ]]; then echo >&2 "Device name required" exit 1 fi if [[ -n "$(echo "$device_name" | tr -d 'a-zA-Z90-9 _-')" ]]; then echo >&2 "Invalid characters in device name" exit 1 fi echo "Now press the button on the Bridge, and press enter here." echo "Please press enter within 30 seconds of pressing the button." read reqdata=$(jq -c --arg app "$application_name" --arg dev "$device_name" '{"devicetype": ($app + "#" + $dev)}' <<<0) # reqdata=$(jq -c --arg app "$application_name" --arg dev "$device_name" '{}' <<<0) response=$(curl -s --cacert signify_root_cert.pem --resolve "$var_bridgeid:443:$var_ip" -d "$reqdata" https://"$var_bridgeid"/api) if [[ $(jq '.[0] | has("success")' <<<"$response") = true ]]; then username=$(jq '.[0].success.username' <<<"$response") echo "username=$username" else echo "Registration failed:" echo "$response" exit 1 fi