#!/bin/bash
#
# automates receiving internet radio stream in VLC and forwarding audio to Jack
# for KAMUNA 2018
#
# Dan Wilcox <dan.wilcox@zkm.de> 2018
# from idea by Barbara Nerness
#
# instructions:
# * start qjackctl/JackPilot and the Jack server
# * run this script with "start", stream pairs appear in order VLC was started
# * when finished, run this script with "stop" to cleanup
#
# for testing, run this script with "test" to receiver form the test streams
# 
# note: radio server *must* already be streaming
#
# --auhal-audio-device is not an index or name string, but the CoreAudio
# AudioDeviceID, what a pain! I only found it via a custom C program which uses
# the CoreAudioAPI to print out the current device information.
#

##### VARS

USAGE="usage: \"start\", \"stop\", or \"test\""
HELP="
usage: vlc-client.sh [OPTIONS] COMMAND

  start VLC receivers for each KAMUNA stream

options:
  -h,--help            display this help message

  -g,--gui             start VLC with gui (default: no)

  -d,--device NAME     specify the audio device VLC should use
                       by name, ie. "JackRouter"
  
  -l,--list-devices    print the avilable audio devices

arguments:

  COMMAND              \"start\", \"stop\", \"test\"
"

VLC=/Applications/VLC.app/Contents/MacOS/VLC
VLC_OPTIONS="--ignore-config --aout=auhal --http-reconnect --quiet"
VLC_GUI="--intf=dummy"
VLC_GUI_SIG="-TERM"
DEVICE="JackRouter"

# KAMUNA streams
SERVER=http://stream.zkm.de:8000
STREAMS=($SERVER/sspace.ogg $SERVER/halo.ogg $SERVER/fuss.ogg)

# testing streams
TEST_STREAMS=( \
	http://wfmu.org/wfmu.pls \
	http://wfmu.org/wfmu_drummer.pls \
	http://wfmu.org/wfmu_ubu.pls
)

##### OPTIONS

while [ "$1" != "" ] ; do
    case $1 in
        -g|--gui)
            VLC_GUI=""
            VLC_GUI_SIG="-KILL"
            ;;
        -d|--audio-device)
            if [ $# == 0 ] ; then
                echo "-d,--audio-device option requires an argument"
                exit 1
            fi
            shift 1
            DEVICE=$1
            ;;
        -l|--list-devices)
			$(dirname $0)/lsaudio/lsaudio
			exit 0
			;;
        -h|--help)
            echo -e "$HELP"
            exit 0
            ;;
        *)
            break ;;
    esac
    shift 1
done

if [ "$#" -lt 1 ] ; then
	echo "$USAGE"
	exit 1
fi
CMD="$1"

DEVICE_ID=$($(dirname $0)/lsaudio/lsaudio "$DEVICE")
if [ "$DEVICE_ID" == "" ] ; then
	echo "device not found: $DEVICE"
	exit 1
fi

VLC_OPTIONS="$VLC_OPTIONS --auhal-audio-device=$DEVICE_ID $VLC_GUI"

##### GO

# stop on error
set -e

case "$CMD" in
	"start")
		echo "starting vlc clients"
		for i in ${!STREAMS[*]} ; do
			echo "  $((i+1)): ${STREAMS[$i]}"
			$VLC $VLC_OPTIONS ${STREAMS[$i]} > /dev/null 2>&1 &
			sleep 1
		done
		;;
	"test")
		echo "starting vlc clients (test streams)"
		for i in ${!TEST_STREAMS[*]} ; do
			echo "  $((i+1)): ${TEST_STREAMS[$i]}"
			$VLC $VLC_OPTIONS ${TEST_STREAMS[$i]} > /dev/null 2>&1 &
			sleep 1
		done
		;;
	"stop")
		echo "stopping vlc clients"
		killall $VLC_GUI_SIG VLC > /dev/null 2>&1
		;;
	*)
		echo "unknown command: \"$CMD\""
		echo "$USAGE"
		exit 1
		;;
esac
