#!/usr/bin/env bash

# Copyright 2021 Jonathan Barnett <me@indieisaconcept.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
# IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

################################################################################

# Usage
#
# Review the system information section in the Sonos app in-order to obtain the
# relevant values required.
#
# soundbar   The IP address of the soundbar
# subwoofer  The IP address of the device to use as a subwoofer
#
# $> chmod u+x setup_subwoofer.sh
# $> ./setup_subwoofer.sh --soundbar 192.168.1.2 --subwoofer 192.168.1.3

while [ $# -gt 0 ]; do
   if [[ $1 == *"--"* ]]; then
        param="${1/--/}"
        declare $param="$2"
   fi
  shift
done

################################################################################

generate_error() {
    echo "ERROR | $1"
}

DeviceDescription() {
    echo $(curl -s -H "Host: $1:1400" -H 'Accept: */*' --compressed "http://$1:1400/xml/device_description.xml")
}

get_device_uuid() {
    local uuid=$(DeviceDescription $1 | xpath -q -e '//root/device/UDN/text()[1]' | sed -e 's/uuid://g')
    echo $uuid
}

AddHTSatellite() {
    local soundbar=$(get_device_uuid $1)
    local subwoofer=$(get_device_uuid $2)

cat <<BODY
<?xml version="1.0" encoding="utf-8"?>
<s:Envelope
    xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <s:Body>
        <u:AddHTSatellite
            xmlns:u="urn:schemas-upnp-org:service:DeviceProperties:1">
            <HTSatChanMapSet>$(echo $soundbar):LF,RF;$(echo $subwoofer):SW</HTSatChanMapSet>
            </u:AddHTSatellite>
    </s:Body>
</s:Envelope>
BODY
}

################################################################################

if [ -z ${soundbar+x} ]; then
    echo $(generate_error "Please supply the IP address of the soundbar you wish to pair a subwoofer with");
    args_error=true;
fi

if [ -z ${subwoofer+x} ]; then
    echo $(generate_error "Please supply the IP address of the device you wish to use as a subwoofer");
    args_error=true;
fi

if [ "$args_error" = "true" ]; then
    exit 2;
fi

payload=$(AddHTSatellite $soundbar $subwoofer)

echo $payload | curl -s -v "http://$soundbar:1400/DeviceProperties/Control" \
-H "Host: $soundbar:1400" \
-H 'Content-Type: text/xml; charset=utf-8' \
-H 'SOAPAction: urn:schemas-upnp-org:service:DeviceProperties:1#AddHTSatellite' \
--data-binary @- --compressed