#!/bin/bash

# author: Torsten Tränkner
# version: 0.1
# license: GPLv2

STORAGE_DIRECTORY="/media/storage/"

read -d '' radio_stations << EOF

"87,9", "Star FM", "star|starfm", "https://streams.starfm.de/berlin.mp3"
"88,8", "Radio Berlin", "radioberlin", "https://dispatcher.rndfnk.com/rbb/rbb888/live/mp3/mid"
"89,6", "Deutschlandradio Kultur", "kultur", "https://st02.sslstream.dlf.de/dlf/02/128/mp3/stream.mp3"
"90,2", "Radio Teddy", "teddy", "https://streamtdy.ir-media-tec.com/live/mp3-128"
"91,4", "Berliner Rundfunk", "rundfunk", "https://stream.berliner-rundfunk.de/brf/mp3-128"
"92,4", "RBB Kulturradio", "kulturradio", "https://dispatcher.rndfnk.com/rbb/rbbkultur/live/mp3/high"
"93,1", "Inforadio RBB", "info|inforadio", "https://dispatcher.rndfnk.com/rbb/inforadio/live/mp3/mid"
"93,6", "Jam FM", "jam|jamfm", "https://stream.jam.fm/jamfm-live/mp3-192"
"93,9", "SKW", "skw", "http://edge.audio.3qsdn.com/senderkw-mp3"
"94,3", "RS2", "rs2", "https://stream.rs2.de/rs2/mp3-192"
"95,8", "Radio Eins", "eins|radioeins", "https://dispatcher.rndfnk.com/rbb/radioeins/live/mp3/mid"
"96,3", "Funkhaus Europa", "cosmo", "https://dispatcher.rndfnk.com/wdr/cosmo/live/mp3/128/stream.mp3"
"97,7", "Deutschlandfunk", "dfunk|deutschlandfunk", "https://st01.sslstream.dlf.de/dlf/01/128/mp3/stream.mp3"
"98,2", "Radio Paradiso", "paradiso", "https://streams.paradiso.de/Berlin/mp3-192/radiode/"
"98,8", "Kiss FM", "kiss|kissfm", "https://stream.kissfm.de/kissfm/mp3-192"
"99,7", "RBB Antenne Brandenburg", "antenne|brandenburg", "https://dispatcher.rndfnk.com/rbb/antennebrandenburg/live/mp3/mid"
"100,6", "Flux FM", "flux|fluxfm", "https://streams.fluxfm.de/live/mp3-192/fluxfm.de_webplayer/"
"101,3", "Klassik Radio", "klassik|klassikradio", "https://live.streams.klassikradio.de/klassikradio-deutschland/stream/mp3"
"102,6", "Fritz", "fritz", "https://dispatcher.rndfnk.com/rbb/fritz/live/mp3/mid"
"103,4", "Energy", "energy", "https://edge05.streamonkey.net/energy-berlin"
"104,6", "RTL", "rtl", "https://stream.104.6rtl.com/rtl-live/mp3-192"
"105,5", "Spreeradio", "spree|spreeradio", "https://stream.spreeradio.de/spree-live/mp3-192"
"106,0", "Radio B2", "b2|radiob2", "https://radiob2-regional.cast.addradio.de/radiob2/regional/mp3/high/stream.mp3"
"106,8", "Jazzradio", "jazz|jazzradio", "https://streaming.radio.co/s774887f7b/listen"
"107,5", "BB Radio", "bb|bbradio", "https://streambbr.ir-media-tec.com/live/mp3-192/radioplayer_web/play.mp3"
"stream", "Klassik Lounge", "lounge|klassiklounge", "https://klassikr.streamabc.net/klassikr-loungebeat-aac-128-1610599"

EOF

if [ "$1" == "stop" ];then
  kill -9 $(pgrep -f "mplayer.*mp3")
  killall vlc 2> /dev/null
  exit 0
fi

declare -A stationArray
index=0

rm -f /dev/shm/stations.txt

# convert the string into an array
echo "$radio_stations" | while read line; do
  newString=$(echo "$line" | sed -e 's|", *"|"|g' -e 's|^"||' -e 's|"$||')
  IFS='"' read -r -a temporaryArray <<< "$newString"

  for ((i=0;i<4;i++)) do
    echo "stationArray[$index,$i]=\"${temporaryArray[${i}]}\"" >> /dev/shm/stations.txt
  done
  let index++
done

# load the converted array
source /dev/shm/stations.txt

if [ $# -lt 1 ] || [[ "$1" =~ help ]];then
  echo "Usage:"
  echo "$0 <radio_station> [play]"
  echo
  echo "Choose a radio station:"
  echo
  for ((i=0;i<${#stationArray[@]}/4;i++)) do
    echo "${stationArray[$i,2]}"
  done
  exit 0
fi

radio_station="$1"

filename=""

for ((i=0;i<${#stationArray[@]}/4;i++)) do
  if [ "$(echo "${radio_station}" | egrep "$(echo "${stationArray[$i,2]}" | sed 's#|#$|#g')")" != "" ];then
    echo "Streaming: ${stationArray[$i,1]} ${stationArray[$i,0]}"
    echo

    # mplayer -quiet -cache 100 "${stationArray[$i,3]}";

    filename="$STORAGE_DIRECTORY"/$(date +"%F_%H-%M-%S")_"${stationArray[$i,1]}".mp3
    mplayer "${stationArray[$i,3]}" -dumpstream -dumpfile "$filename" < /dev/null >/dev/null 2>/tmp/mplayer_radio.error &

    break
  fi
done

# wait for the creation of the file
index=0
while true; do
  sleep 1
  let index++
  if [ $index -gt 30 ];then
    echo "No stream found"
    exit 1
  fi

  if [ -e "$filename" ];then

    SIZE=100000
    if [ "$2" == "play" ];then
      SIZE=30000
    fi

    # check that the file is bigger than 100 kB
    if [ $(stat -c"%s" "$filename") -gt $SIZE ]; then
      if [ "$2" == "play" ];then
        mplayer "$filename"
      else
        # stream over the network
        cvlc -A alsa "$filename" --sout '#standard{access=http,mux=raw,dst=:8000/audio.mp3}' < /dev/null >/dev/null 2>/tmp/cvlc_radio.error &
      fi
      exit 0

    fi
  fi
done
