#!/usr/bin/bash

# heos-favplay  Copyright 2025  Norman Carver

# Script program to Play a particular Favorites item,
# where the player is specified with:
# (1) a (sub)string of the player's HEOS name, or
# (2) its IP num (i.e., host portion of its dotted quad IP address).
#
# If the "player" is actually a HEOS Group, use the -g option
# with the "player" name being the group name.
#
# Sends commands to the default HEOS controller, but can select alternative
# using the CONTROLLER_IPNUM argument option.


function print_usage()
{
    echo "Usage: heos-favplay [-v] [-g] PLAYER_NAME_OR_IPNUM FAVORITES_ITEM [CONTROLLER_IPNUM]" >&2
    echo "(FAVORITES_ITEM is the 1-based index number of an item in the Favorites list)" >&2
    echo "(IPNUM means the last quad of an IP address only: i.e., 0--255)" >&2
    echo "Options:" >&2
    echo "  -v -- verbose: print out name of FAVORITES_ITEM when started" >&2
    echo "  -g -- group: PLAYER_NAME_OR_IPNUM is a Group Name" >&2
}


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

verbose=false
if [[ "$1" == -v ]]; then
    verbose=true
    shift
fi

groupopt=
if [[ "$1" == -g ]]; then
    groupopt=--group
    shift
fi

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

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

if [[ ! ("$item" =~ ^[0-9]+$ && "$item" -ge 1) ]]; then
    echo "Error: FAVORITES_ITEM must be an integer >= 1 ('$item')" >&2
    exit 1
fi


# Functions used below:
function line_2() { read; read line; echo "$line"; }
function line_n() { i=0; while [[ $i -lt $1 ]]; do read line; i=$((i+1)); done; echo "$line"; }


# Start FAVORITES_ITEM playing (look at 2nd JSON object for play result):
result=$("$heosdir"/heosutil-run-command --player="$player" $groupopt --numjson=2 "heos://browse/play_preset?pid=\${pid}\&preset=${item}" $controller | line_2)
if [[ ($? != 0) || ("$result" != *'"result": "success"'*) ]]; then
    echo "Action FAILED!" >&2
    exit 1
fi

if $verbose; then
    # Get Music Sources info so can get Favorites SID:
    sources=$("$heosdir"/heosutil-run-command "heos://browse/get_music_sources" $controller)
    if [[ ($? != 0) || ("${sources}" != *'"result": "success"'*) ]]; then
    echo "Error: failed printing FAVORITES_ITEM" >&2
    exit 1
    fi

    favsrc=${sources#*\{\"name\": \"Favorites\"*\"sid\":\ }
    favsid=${favsrc%%,*}

    # Get Favorites browse list (list is in 2nd JSON response):
    favjson=$("$heosdir"/heosutil-run-command --numjson=2 "heos://browse/browse?sid=${favsid}" $controller | line_2)
    if [[ ($? != 0) || ("${favjson}" != *'"result": "success"'*) ]]; then
        echo "Error: failed getting Favorites listing" >&2
        exit 1
    fi

    # Make list of the favorites and select FAVORITES_ITEM:
    favitem=$(grep -Eo '\{"container": [^}]+\}' <<<"$favjson" | line_n $item ) 

    # Reformat and output favitem:
    { read line
      line=${line#*\"type\":\ \"}
      type=${line%%\",*}
      line=${line##*\"name\":\ \"}
      name=${line%%\",*}
      echo "Starting Favorites item '$type: $name' playing..." >&2
    } <<<"$favitem"
fi

#EOF
