#!/bin/sh
# Config include string generation macro
_CONFIG() { echo " -include $HOME/.mplayer/config-$1"; }

# Defaults
CONFIG="$(_CONFIG specialradio)"
MPL=mplayer					# MPlayer binary name
ARGS="-quiet -vo null"				# Arguments by default player
IARGS="-quiet -vo null -va null -nocache"	# Arguments for info picker
XTERM=`which xterm`				# XTerm binary name
XTA_UI="-T MPlayer +sb"				# XTerm lookout options
XTA_Geometry="-g 56x3+400-0"			# XTerm geometry
XTA_Font="-fn xtermg-8x14"			# XTerm font
XTARG="$XTA_UI $XTA_Geometry $XTA_Font"		# All XTerm options
SLEEP=10					# Sleep between info picks
# System dependent variables:
#	SED_N		sed's \n char
case `uname` in
  FreeBSD) SED_N='\
'
	;;
  Linux) SED_N='\n'
  	;;
  *) SED_N='n'
  	;;
esac

Channels="1 2 3 4 5 6 7 8 9 0 11 12 13 14 15 16 17 18"
test -n "$DISPLAY" -a "$1" != "-" && { $XTERM $XTARG -e `realpath "$0"` - "$@"& exit; }
test "$1" = "-" && shift

die() {	# [msg [errcode]]
  echo ${1:-"ERROR"} >&2
  exit ${2:-1}
}

ICYInfo() { # Channel translates ICY Info without StreamUrl
  # We need to display ICY at first time for mplayer is too verbose when strted
  $MPL -vo null -ao null -nocache "$1" 2>/dev/null | while read A; do
  case "$A" in 
    ICY*info:*) echo "$A"; break ;;
  esac; done | sed -n "/^ICY Info:/s/.*StreamTitle='\(.*\) - \(.*\)';.*/\1$SED_N\2/p"
  #ICY Info: StreamTitle='Edenbridge - Centennial Legend';
}

ICYInfoM() { # Channel translates ICY Info once a 48K
  # We need to display ICY at first time for mplayer is too verbose when strted
  $MPL -vo null -ao null -frames 0 $CONFIG "$1" 2>/dev/null | sed -n "/^ICY Info:/s/.*StreamTitle='\(.*\) (\(.*\))';.*/\1$SED_N\2/p"
  #ICY Info: StreamTitle='Edenbridge - Centennial Legend';
}

SpecialRadio() {	# Special Radio http://specialradio.ru
	wget -q --header='Accept-Charset: koi8-r' -O - "$1" | sed -n "
s/.*grp=[^>]*>\([^<]*\)[- <>\/a]*\([^<]*\).*/\1$SED_N\2/p"
}

PickChannel () {
  case "$1" in
    mms://81.13.120.206/1button|1) METHOD=SpecialRadio;
      STREAM="mms://81.13.120.206/1button"
      Name="Основной эфир"; URL="http://specialradio.ru/1_sve.php" ;;
    mms://81.13.120.206/2button|2) METHOD=SpecialRadio;
      STREAM="mms://81.13.120.206/2button"
      Name="Блатняк"; URL="http://specialradio.ru/2_sve.php" ;;
    mms://81.13.120.206/3button|3) METHOD=SpecialRadio;
      STREAM="mms://81.13.120.206/3button"
      Name="Русский Рок"; URL="http://specialradio.ru/3_sve.php" ;;
    mms://81.13.120.206/4button|4) METHOD=SpecialRadio;
      STREAM="mms://81.13.120.206/4button"
      Name="Французская музыка"; URL="http://specialradio.ru/4_sve.php" ;;
    mms://81.13.120.206/5button|5) METHOD=SpecialRadio;
      STREAM="mms://81.13.120.206/5button"
      Name="Тяжелая музыка"; URL="http://specialradio.ru/5_sve.php" ;;
    mms://81.13.120.206/6button|6) METHOD=SpecialRadio;
      STREAM="mms://81.13.120.206/6button"
      Name="Ворлд мьюзик"; URL="http://specialradio.ru/6_sve.php" ;;
    mms://81.13.20.10/7button|7) METHOD=SpecialRadio;
      STREAM="mms://81.13.20.10/7button"
      Name="Техно. Транс"; URL="http://specialradio.ru/7_sve.php" ;;
    mms://81.13.20.10/8button|8) METHOD=SpecialRadio;
      STREAM="mms://81.13.20.10/8button"
      Name="Электроакустика"; URL="http://specialradio.ru/8_sve.php" ;;
    mms://81.13.20.10/9button|9) METHOD=SpecialRadio;
      STREAM="mms://81.13.20.10/9button"
      Name="Классическая музыка"; URL="http://specialradio.ru/9_sve.php" ;;
    mms://81.13.20.10/10button|10) METHOD=SpecialRadio;
      STREAM="mms://81.13.20.10/10button"
      Name="Джаз и Блюз"; URL="http://specialradio.ru/10_sve.php" ;;
    mms://81.13.20.10/11button|11) METHOD=SpecialRadio;
      STREAM="mms://81.13.20.10/11button"
      Name="ВИА"; URL="http://specialradio.ru/11_sve.php" ;;
    mms://81.13.20.10/12button|12) METHOD=SpecialRadio;
      STREAM="mms://81.13.20.10/12button"
      Name="Авторская песня"; URL="http://specialradio.ru/12_sve.php" ;;
    mms://81.13.20.10/13button|13) METHOD=SpecialRadio;
      STREAM="mms://81.13.20.10/13button"
      Name="Российская электроника"; URL="http://specialradio.ru/13_sve.php" ;;
    mms://81.13.20.10/14button|14) METHOD=SpecialRadio;
      STREAM="mms://81.13.20.10/14button"
      Name="МедиаКнига"; URL="http://specialradio.ru/14_sve.php" ;;
    mms://81.13.20.10/15button|15) METHOD=SpecialRadio;
      STREAM="mms://81.13.20.10/15button"
      Name="Правительство Звука"; URL="http://specialradio.ru/15_sve.php" ;;
    mms://81.13.20.10/16button|16) METHOD=SpecialRadio;
      STREAM="mms://81.13.20.10/16button"
      Name="Попса"; URL="http://specialradio.ru/16_sve.php" ;;
    mms://81.13.20.10/17button|17) METHOD=SpecialRadio;
      STREAM="mms://81.13.20.10/17button"
      Name="Синти-Радио"; URL="http://specialradio.ru/17_sve.php" ;;
    mms://81.13.20.10/18button|18) METHOD=SpecialRadio;
      STREAM="mms://81.13.20.10/18button"
      Name="Сега-Радио"; URL="http://specialradio.ru/18_sve.php" ;;
    mms://81.13.20.10/19button|19) METHOD=SpecialRadio;
      STREAM="mms://81.13.20.10/19button"
      Name="RnB"; URL="http://specialradio.ru/19_sve.php" ;;
    mms://81.13.20.10/20button|20) METHOD=SpecialRadio;
      STREAM="mms://81.13.20.10/20button"
      Name="Неоклассика"; URL="http://specialradio.ru/20_sve.php" ;;
    http://i-radio.com.ua:8000/metal-hi|iradio) METHOD="ICYInfo"; SLEEP="120";
      STREAM="http://i-radio.com.ua:8000/metal-hi"
      CONFIG="$(_CONFIG iradio)"; Name="I-Radio Metal"; URL="$STREAM" ;;
    http://209.9.233.11:9976|macca) METHOD="ICYInfoM"; SLEEP="120";
      STREAM="http://209.9.233.11:9976"
      CONFIG="$(_CONFIG maccaradio)"; Name="Macca Radio"; URL="$STREAM" ;;
    http://209.9.233.11:7000|generationrock)
      STREAM="http://209.9.233.11:7000"; URL="$STREAM";
      CONFIG="$(_CONFIG maccaradio)"; Name="Generation Rock";
      HTTP="http://generationrockradio.com/";
      METHOD="ICYInfo";;
    http://216.235.91.34:20472/play?session=haroldthebarrel:0?bitrate=512|uk70)
      STREAM="http://216.235.91.34:20472/play?session=haroldthebarrel:0&bitrate=512"
      CONFIG="$(_CONFIG maccaradio)"; Name="UK 70s"; URL="http://www.live365.com/mini/playlist.html?station=haroldthebarrel&hide=W&rows=7"
      MEHOD="" ;;
    *) METHOD=""; SLEEP="120";
      STREAM="$1"; URL="$1"; CONFIG="$(_CONFIG maccaradio)" ;;
  esac
}

# TODO getopts
# TODO stations on wiki + wget + cache of stations

PickChannel "$1" || {
  for N in $Channels
    do
    PickChannel "$N"
    echo "$N:	$Name	($URL)"
  done
exit 1
}

# KILL mplayer on exit
exit_handler() {
  local rc=$? F
  trap - EXIT
  kill $MPID
  exit $rc
}
trap exit_handler EXIT HUP INT QUIT PIPE TERM

export SLEEP METHOD URL Name
# launch info picker on bg
s=2
while sleep $s
do
  s=$SLEEP
  test -z "$METHOD" || $METHOD "$URL" | {
read GROUP
read TITLE
test -z "$DISPLAY" || clear
echo "$Name: $GROUP / $TITLE"
}
done&

# now launch mplayer
MPID="$!"
$MPL $ARGS $CONFIG "$STREAM"
