#!/bin/sh

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

. /etc/profile
oe_setup_addon service.snapserver

config="pulseaudio.conf"
link="/storage/.config/pulse-daemon.conf.d/50-snapserver.conf"
mkdir -p "$(dirname $link)"
ln -sf "$ADDON_HOME/$config" "$link"
if [ ! -e "$ADDON_HOME/$config" ]; then
  cp "$ADDON_DIR/config/$config" "$ADDON_HOME"
fi

file="/tmp/snapkodi"
sink_name="Snapserver"
if [ -z "$(pactl list short | grep $sink_name)" ]; then
  pactl load-module module-pipe-sink \
                    file="$file" \
                    rate=48000 \
                    sink_name="$sink_name" \
                    sink_properties=device.description="$sink_name"
fi

case "$ss_st" in
  Default)
    stream="pipe:///tmp/snapfifo?name=Default"
    ;;
  Kodi)
    stream="pipe://$file?name=Kodi"
    ;;
  Spotify)
    stream="spotify:///librespot?name=Spotify"
    ;;
  *)
    stream="airplay:///shairport-sync?name=AirPlay"
    ;;
esac

HOME="$ADDON_HOME" \
nice -n "$ss_ni" \
snapserver \
  --controlPort  "$ss_cp"  \
  --port         "$ss_sp"  \
  --stream       "$stream" \
  > /dev/null
