#!/usr/bin/bash

# heos-menu  Copyright 2025  Norman Carver

# Script program to loop on a menu of HEOS actions for a HEOS player.
# Player is specified using a (sub)string of the player's HEOS name, or
# its IP num (host only, last quad in player's dotted quad IP address).
#
# If the "player" is actually a HEOS Group, use the -g option
# with the "player" name being the group name.
#
# By default, sends commands to the HEOS controller listed below, but
# can select alternative using the CONTROLLER_IPNUM argument option.


# LAN settings:
controllernum=151   #default HEOS controller host 


function print_usage()
{
    echo "Usage: heos-menu [-g] PLAYER_NAME_OR_IPNUM [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

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

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

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


# Validate player:
if ! state=$("$heosdir"/heos-state $groupopt "$player" "$controllernum"); then
    exit 1
fi


options=(Play Pause Next Prev Volume "Queue Item" "Fav Item" "Playlist Item" Mute UnMute "Now Playing" State)
PS3='Enter choice (^-d to Quit): '
select opt in "${options[@]}"; do
    case "$opt" in
        Play)
            "$heosdir"/heos-play $groupopt "$player"
            if [[ $? == 0 ]]; then echo "Playing..."; fi ;;
        Pause)
            "$heosdir"/heos-pause $groupopt "$player"
            if [[ $? == 0 ]]; then echo "Paused!"; fi ;;
        Next)
            "$heosdir"/heos-next $groupopt "$player" ;;
        Prev)
            "$heosdir"/heos-prev $groupopt "$player" ;;
        "Fav Item")
            read -p "Enter Favorites item (>=1) or Return to list: " item
            if [[ -n "$item" ]]; then
                "$heosdir"/heos-favplay $groupopt "$player" "$item"
                if [[ $? == 0 ]]; then
                    entry=$("$heosdir"/heos-favlist | grep -E "^ ?${item}\) ")
                    echo "Playing: $entry"
                fi
            else
                "$heosdir"/heos-favlist
            fi ;;
        "Playlist Item")
            read -p "Enter Playlists item (>=1) or Return to list: " item
            if [[ -n "$item" ]]; then
                "$heosdir"/heos-plplay $groupopt "$player" "$item"
            else
                "$heosdir"/heos-pllist
            fi ;;
        "Queue Item")
            read -p  "Enter Queue item (>=1) or Return to list: " item
            if [[ -n "$item" ]]; then
                "$heosdir"/heos-qplay $groupopt "$player" "$item"
                if [[ $? == 0 ]]; then
                    entry=$("$heosdir"/heos-qlist $groupopt "$player" | grep -E "^ ?${item}\) ")
                    echo "Playing: $entry"
                fi
            else
                "$heosdir"/heos-qlist $groupopt "$player"
            fi ;;
        Volume)
            read -p "Enter volume level or Return for current: " vol
            if [[ -n "$vol" ]]; then
                "$heosdir"/heos-volset $groupopt "$player" "$vol"
            else
                "$heosdir"/heos-volget $groupopt "$player"
            fi ;;
        Mute)
            "$heosdir"/heos-muteon $groupopt "$player" ;;
        UnMute)
            "$heosdir"/heos-muteoff $groupopt "$player" ;;
        "Now Playing")
            "$heosdir"/heos-nowplaying $groupopt "$player" ;;
        State)
            "$heosdir"/heos-state $groupopt "$player"
            "$heosdir"/heos-volget $groupopt "$player"
	    "$heosdir"/heos-mode $groupopt "$player" ;;
        *) echo "Error: invalid option '$REPLY'" ;;
    esac
done

#EOF
