#!/usr/bin/bash

# heos-repeat  Copyright 2025-2026  Norman Carver

# Script program to print out (to stdout) or set the current Repeat Mode of a HEOS player/group.
#
# A 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).
#
# With the -g option, a group is specified:
# (1) a (sub)string of the group's HEOS name, or
# (2) its Group ID (GID).
#
# Sends commands to the default HEOS master device, but can select alternative
# using the MASTER_IPNUM argument option.


function print_usage()
{
    echo -e "Print out or Set the Repeat mode of a HEOS Player/Group.\n" >&2
    echo    "Usage: heos-repeat [-g] PLAYER [ACTION [MASTER_IPNUM]]" >&2
    echo    "where:" >&2
    echo    "  PLAYER -- a HEOS Player's Name (substring) or its IPNUM;" >&2
    echo    "  ACTION -- action to take with player's Repeat mode, one of:" >&2
    echo    "          -- default: print out current repeat mode;" >&2
    echo    "     all  -- repeat mode to All;" >&2
    echo    "     one  -- repeat mode to One;" >&2
    echo    "     off  -- repeat mode to Off;" >&2
    echo    "     ?    -- print out current repeat mode (allows MASTER_IPNUM);" >&2
    echo    "  MASTER_IPNUM -- use a non-default HEOS master device;" >&2
    echo -e "  (IPNUM means the last quad of an IP address only: i.e., 0--255.)\n" >&2
    echo    "Options:" >&2
    echo    "  -g -- group: PLAYER is a group Name or GID;" >&2
}


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

# Process supplied options:
pgopt=--player
pidopt=""
debugopt=""
while [[ "$1" == -* ]]; do
    if [[ "$1" == -g ]]; then
        pgopt=--group
    elif [[ "$1" == --pid=?* ]]; then
        pidopt=$1
    elif [[ "$1" == -- ]]; then
        shift;break
    elif [[ "$1" == --debug ]]; then
        debugopt=--debug
    else
        echo "Error: invalid option '$1'" >&2
        echo "(use '--' to signal end of options, with -* arguments)" >&2
        exit 1
    fi
    shift
done

# Process command arguments:
if [[ $# == 0 || $# -gt 3 ]]; then
    print_usage
    exit 1
fi

player=$1
action=print
master=""
if [[ $# -ge 2 ]]; then
    case "$2" in
        [aA][lL][lL]) action=all ;;
        [oO][nN][eE]) action=one ;;
        [oO][fF][fF]) action=off ;;
        ?) action=print ;;
        *) echo "Error: invalid ACTION '$2'!" >&2; exit 1;;
    esac
    if [[ $# == 3 ]]; then
        master=${3}
    fi
fi
heosdir=$(dirname "$0")


# heosutil-run-command error checking function (one json output):
# args: 1) command result variable; 2) error message; [3) error action]
# (default action on error is 'exit 1', use $3 of 'return 1' to continue execution)
function error_check_run_command()
{
    cmdresult=${!1}
    if [[ ($? != 0) || ("$cmdresult" != *'"result": "success"'*) ]]; then
        if [[ "$cmdresult" == Error:* || ($debugopt == --debug && "$cmdresult" == ERROR:*) ]]; then
            echo "$cmdresult" >&2
        fi
        if [[ -n "$2" ]]; then echo "$2" >&2; fi
        if [[ $# == 3 ]]; then $3; else exit 1; fi
    else
        return 0
    fi
}


if [[ "$action" == print ]]; then
    # Get and print out Repeat mode info for player/group:
    result=$("$heosdir"/heosutils/heosutil-run-command --player="$player" "heos://player/get_play_mode?pid=\${pid}" $master)
    error_check_run_command result "Error: failed getting Mode state!"
    repeat=$(grep -Eo 'repeat=[^"&]+' <<<"$result")
    repeat=${repeat#repeat=}
    echo -n "Repeat: "
    case "$repeat" in
        on_all) echo "ALL";;
        on_one) echo "ONE";;
        off) echo "OFF";;
    esac

elif [[ "$action" == all ]]; then
    # Turn Repeat All on for player/group:
    result=$("$heosdir"/heosutils/heosutil-run-command ${pgopt}="$player" $pidopt "heos://player/set_play_mode?pid=\${pid}&repeat=on_all" $master)
    error_check_run_command result "Error: failed to set Repeat mode to All!"

elif [[ "$action" == one ]]; then
    # Turn Repeat One on for player/group:
    result=$("$heosdir"/heosutils/heosutil-run-command ${pgopt}="$player" $pidopt "heos://player/set_play_mode?pid=\${pid}&repeat=on_one" $master)
    error_check_run_command result "Error: failed to set Repeat mode to One!"

elif [[ "$action" == off ]]; then
    # Turn Repeat Off for player/group:
    result=$("$heosdir"/heosutils/heosutil-run-command ${pgopt}="$player" $pidopt "heos://player/set_play_mode?pid=\${pid}&repeat=off" $master)
    error_check_run_command result "Error: failed to set HEOS Player's Reapet mode to Off!"

fi

#EOF
