#!/usr/bin/bash

# heos-login  Copyright 2025  Norman Carver

# Script program to login a HEOS player/device to a HEOS account.
# This should be necessary only if the player/device has not been
# addressed from the HEOS app and the device is to be used as the
# Controller by the set of HEOS CLI scripts.
#
# The player/device must be specified with its complete IP Address.
# The HEOS Email/Usename and Password will be prompted for.


function print_usage()
{
    echo "Usage: heos-login HEOS_DEVICE_IP_ADDRESS" >&2
}


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

if [[ $# != 1 ]]; then
    print_usage
    exit 1
fi

heosip=$1
heosdir=$(dirname "$0")


read -p "Enter HEOS Email (username): " heosuser

read -s -p "Enter HEOS Password: " heospwd
echo


result=$("$heosdir"/heosutil-send-command-toipaddr "heos://system/sign_in?un=$heosuser&pw=heospwd" "$heosip")

if ! echo "$result" | grep -F -q '"result": "success"'; then
    echo "FAILED!" >&2
    exit 1
fi

#EOF
