#!/bin/bash
#
# shellcheck disable=SC1090,SC2220

ROON=/usr/local/Roon
ROONAPI=${ROON}/api
ROONETC=${ROON}/etc
ROONCONF=${ROONETC}/pyroonconf
LIST=list_zones.py
INFO=zone_properties.py

[ -d ${ROONAPI} ] || exit 1

cd ${ROONAPI} || exit 1

[ -f $LIST ] || exit 2

usage() {
  printf "\nUsage: get_zone_info [-ahlnpsv] [-z zone]"
  printf "\nWhere:"
  printf "\n\t-a indicates display all zone info types"
  printf "\n\t-n indicates display now playing zone info"
  printf "\n\t-p indicates display zone properties"
  printf "\n\t-s indicates display zone settings"
  printf "\n\t-l indicates output a comma separated list of zones"
  printf "\n\t-v indicates verbose output"
  printf "\n\t-z 'zone' specifies the zone from which to retrieve info"
  printf "\n\t  '-z default' indicates the default zone, '-z last' the last zone used"
  printf "\n\t-h displays this usage message and exits\n\n"
  exit 1
}

# Use a Python virtual environment
[ -f ${ROON}/venv/bin/activate ] && source ${ROON}/venv/bin/activate
[[ ":$PATH:" == *":/usr/local/Roon/venv/bin:"* ]] || {
  export PATH=/usr/local/Roon/venv/bin:${PATH}
}

DEFZONE=$(grep ^DefaultZone ${ROONETC}/roon_api.ini | awk -F '=' ' { print $2 } ')
# Remove leading and trailing spaces in DEFZONE
DEFZONE="$(echo -e "${DEFZONE}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"

if [ -f ${ROONCONF} ]
then
    . ${ROONCONF}
else
    ${ROON}/bin/set_zone "${DEFZONE}"
    . ${ROONCONF}
fi

getwith=
infoflags=
verbose=
ZONE=
while getopts "ahlnpsvz:" flag; do
    case $flag in
        a)
            infoflags="-a ${infoflags}"
            ;;
        h)
            usage
            ;;
        l)
            getwith="-g"
            ;;
        n)
            infoflags="-n ${infoflags}"
            ;;
        p)
            infoflags="-p ${infoflags}"
            ;;
        s)
            infoflags="-s ${infoflags}"
            ;;
        v)
            verbose=1
            infoflags="-v ${infoflags}"
            ;;
        z)
            ZONE="$OPTARG"
            ;;
    esac
done
[ "${infoflags}" ] || infoflags="-a"

[ "${ZONE}" ] && {
    if [ "${ZONE}" == "default" ]; then
        ROON_ZONE="${DEFZONE}"
    else
        [ "${ZONE}" == "last" ] || ROON_ZONE="${ZONE}"
    fi
}
# Get all zones if no zone is specified
[ "${ROON_ZONE}" ] || ROON_ZONE="__all__"

have_python3=$(type -p python3)
if [ "${have_python3}" ]
then
    PYTHON=python3
else
    PYTHON=python
fi

if [ "${getwith}" ]; then
    ${PYTHON} $LIST ${getwith} -i -z "${ROON_ZONE}"
else
    if [ "${verbose}" ]; then
        use_jq=
    else
        have_jq=$(type -p jq)
        if [ "${have_jq}" ]; then
            use_jq=1
        else
            use_jq=
        fi
    fi
    if [ "${use_jq}" ]; then
        ${PYTHON} $INFO ${infoflags} -z "${ROON_ZONE}" | jq -r .
    else
        ${PYTHON} $INFO ${infoflags} -z "${ROON_ZONE}"
    fi
fi
