#!/usr/bin/bash

# heos-ungroup  Copyright 2025  Norman Carver

# Script program to Ungroup a set of Players currnelt in a HEOS Group.
# Specify the Group using the Group's HEOS name.
#
# Sends commands to the default HEOS controller, but can select alternative
# using the CONTROLLER_IPNUM argument option.


volmax=75


function print_usage()
{
    echo "Usage: heos-ungroup GROUP_NAME [CONTROLLER_IPNUM]" >&2
    echo "(IPNUM means the last quad of an IP address only: i.e., 0--255)" >&2
}


if [[ "$1" == --help ]]; then
    print_usage
    exit 0
fi

if [[ $# -lt 1 || $# -gt 2 ]]; then
    print_usage
    exit 1
fi

player=$1
if [[ $# == 2 ]]; then
    controller=${2}
else
    controller=""
fi
heosdir=$(dirname "$0")

result=$("$heosdir"/heosutil-run-command --player="$player" --group "heos://group/set_group?pid=\${gid}" $controller)
if [[ ($? != 0) || ("$result" != *'"result": "success"'*) ]]; then
    echo "Action FAILED!" >&2
    exit 1
fi

#EOF
