#!/bin/bash
set -e
ADB=${ADB:-adb}
VLC=${VLC:-vlc}
SNDCPY_PORT=${SNDCPY_PORT:-27182}
WIFISNDCPY_PORT=${WIFISNDCPY_PORT:-27185}

serial=
clientIP=
if [[ $1 == "27183" ]]
then
    serial="-s $2"
    echo "Waiting for device $2..."
    "$ADB" $serial wait-for-device
    echo am start...
    "$ADB" $serial shell appops set com.kylin.mobileassistant PROJECT_MEDIA allow
    #"$ADB" $serial shell appops set com.kylin.mobileassistant RECORD_AUDIO allow
    "$ADB" $serial shell pm grant com.kylin.mobileassistant android.permission.RECORD_AUDIO
    #"$ADB" $serial shell pm grant com.kylin.mobileassistant android.permission.FOREGROUNG_SERVICE
    "$ADB" $serial shell pm grant com.kylin.mobileassistant android.permission.READ_EXTERNAL_STORAGE
    "$ADB" $serial shell pm grant com.kylin.mobileassistant android.permission.WRITE_EXTERNAL_STORAGE
    #"$ADB" $serial shell pm grant com.kylin.mobileassistant android.permission.MANGE_EXTERNAL_STORAGE
    "$ADB" $serial forward tcp:$SNDCPY_PORT localabstract:sndcpy

    sleep 2

    echo Playing audio...
    "$VLC" -Idummy --demux rawaud --network-caching=0 --play-and-exit tcp://localhost:"$SNDCPY_PORT"
elif [[ $1 == "27186" ]]
then
    clientIP="$2"
    echo "Waiting for device $2..."
    sleep 2
    echo Playing audio...
    "$VLC" -Idummy --demux rawaud --network-caching=0 --play-and-exit tcp://$clientIP:"$WIFISNDCPY_PORT"
fi

