#!/bin/bash
set -e

[ -z "$1" ] && {
	echo "need arg 1: deets file"
	exit 1
}

[ -z "$2" ] && {
	echo "need arg 2: input file, optionally followed by decode hints (ffmpeg arguments)"
	exit 1
}

cat >/dev/null <<EOF
# example deets file:
source:hackme
ice.ocv.me:8000
main.mp3

# example usage:
cd ~ed/dev && something-that-produces-44khz-16bit-2ch | ./audio2ice.sh deets-aya - -ac 2 -f s16le -ar 44100

# example usage:
python3 -m venv ~/pe/ve.streamlink
. ~/pe/ve.streamlink/bin/activate
cd ~ed/dev && streamlink -p ./audio2ice.sh -va'deets-aya -' https://www.twitch.tv/kinothedj audio_only
EOF

creds="$(awk NR==1 "$1")"
server="$(awk NR==2 "$1")"
mount="$(awk NR==3 "$1")"

fn="$2"
shift 2

ffmpeg -hide_banner -nostats -re "$@" -i "$fn" -acodec libmp3lame -b:a 192k -f mp3 - |
tee "audio2ice-$(date +%Y-%m%d-%H%M%S).mp3" |
ffmpeg -hide_banner -re -f mp3 -i - -codec copy -content_type audio/mpeg -ice_name memes -legacy_icecast 1 icecast://"$creds"@"$server"/"$mount"
