#!/usr/bin/bash

# heos-plplay  Copyright 2025  Norman Carver

# Script program to Play a particular Playlist.
#
# 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-plplay [-v] [-g] PLAYER_NAME_OR_IPNUM PLAYLIST_ITEM [CONTROLLER_IPNUM]" >&2
    echo "(PLAYLIST_ITEM is the 1-based index number of an item in the Playlists 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 PLAYLIST_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: PLAYLIST_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"; }


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

plsrc=${sources#*\{\"name\": \"Playlists\"*\"sid\":\ }
plsid=${plsrc%%,*}

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

# Make list on separate lines of the playlists objects:
pllist=$(grep -Eo '\{"container": [^}]+\}' <<<"$pljson")

# Get PLAYLIST_ITEM object:
itemobj=$(line_n $item <<<"$pllist")
if [[ -z "$itemobj" ]]; then
    echo "Error: no PLAYLIST_ITEM '$item'" >&2
    exit 1
fi

itemobj=${itemobj#*\"cid\":\ \"}
cid=${itemobj%%\",\ *}

result=$("$heosdir"/heosutil-run-command  --player="$player" $groupopt --numjson=2 "heos://browse/add_to_queue?pid=\${pid}\&sid=${plsid}\&cid=${cid}\&aid=4" $controller | line2)
if [[ ($? != 0) || ("${result}" != *'"result": "success"'*) ]]; then
    echo "Action FAILED!" >&2
    exit 1
fi

if $verbose; then
    itemobj=${itemobj#*\"name\":\ \"}
    name=${itemobj%%\",\ *}
    echo "Replacing Queue with Playlist '$name' and playing..." >&2
fi

#EOF
