#!/bin/sh

# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2018-present Team LibreELEC (https://libreelec.tv)

. /etc/profile
oe_setup_addon service.snapclient

. /etc/os-release
case "$DISTRO_ARCH" in
  RPi*.arm)
    if [ "$sc_a" == "true" ]; then
      ALSA="/proc/asound/ALSA"
      if [ ! -e "$ALSA" ]; then
        echo "Starting Raspberry Pi onboard audio"
        dtparam audio=on
        sleep 1
      fi
      if [ -e "$ALSA" ]; then
        echo "Setting Raspberry Pi onboard audio playback route"
        index="$(readlink $ALSA)"
        index="${index##*card}"
        amixer -c "$index" cset name="PCM Playback Route" "$sc_r"
      fi
    fi
    ;;
esac

[ -n "$sc_h" ] && sc_H="--hostID $sc_h"
[ -n "$sc_s" ] && sc_S="--soundcard $sc_s"
[ -n "$sc_addr" ] && HOST="--host $sc_addr"

HOME="$ADDON_HOME" \
nice -n "$sc_n" \
snapclient \
  $sc_H \
  --latency "$sc_l" \
  $HOST \
  --port "$sc_p" \
  $sc_S \
  > /dev/null
