#!/bin/sh
#
# Start/stop bluealsa-aplay
#

SERVICE=bluealsa-aplay
DAEMON="/usr/bin/$SERVICE"
CONF=/etc/$SERVICE.conf
[ -f $CONF ] && source $CONF

case "$1" in
start)
  echo "Starting $SERVICE..."

  if [ "$BLUEALSA_APLAY_CONTROL" -a "$BLUEALSA_APLAY_DEVICE" ]; then
    # Soft volume control will appear only after first usage of device
    /usr/bin/aplay -D "$BLUEALSA_APLAY_DEVICE" -t raw -f S16_LE -c 2 -r 48000 -s 1 /dev/zero
    /usr/bin/amixer sset "$BLUEALSA_APLAY_CONTROL" -- "$BLUEALSA_APLAY_VALUE"
  fi

  start-stop-daemon -S -b -x "$DAEMON" -n "$SERVICE" -- \
                    ${BLUEALSA_APLAY_DEVICE:+-D "$BLUEALSA_APLAY_DEVICE"} \
                    -M disable $BLUEALSA_APLAY_ARGS -S
  ;;
stop)
  echo "Stopping $SERVICE..."
  start-stop-daemon -K -x "$DAEMON" -n "$SERVICE" -o
  ;;
restart)
  "$0" stop
  "$0" start
  ;;
*)
  echo "Usage: $0 {start|stop|restart}"
  exit 1
esac
