*** empty log message ***
[nethome.git] / bin / engine-di.fm
1 #! /bin/bash
2 #
3 # $Id$
4
5
6 if [ "$1" = "build" ];then
7         old="`find -maxdepth 1 '(' -name "di.fm-*" -o -name "sky.fm-*" ')' -type l`"
8         if [ -z "$old" ];then echo >&2 "No existing symlinks found.";exit 1;fi
9         for site in di.fm sky.fm;do
10                 newdashed="`wget -q -O - http://$site/|tr ' ' '\n' \
11                                 |perl -n -e '
12                                                 s{^.*"(?:http://www.'$site')?([^":]*(?<!_low)[.](?:pls|asx))".*$}{$1} or next;
13                                                 m{^/aacplus/} and next;
14                                                 tr{/}{-};
15                                                 print;
16                                                 ' \
17                                 `"
18                 if [ -z "$newdashed" ];then echo >&2 "Failed to download the new listing found.";exit 1;fi
19                 TARGET="engine-di.fm"
20                 if [ ! -x "$TARGET" ];then echo >&2 "Target engine $TARGET not found.";exit 1;fi
21                 rm -f $old
22                 old=""
23                 PREFIX="$site"
24                 for pathdashed in $newdashed;do
25                         test -L "$PREFIX$pathdashed" && continue
26                         ln -s "$TARGET" "$PREFIX$pathdashed"
27                 done
28         done
29         echo OK
30         exit 0
31 fi
32
33 set -ex
34 pathslashed="`basename "$0"|tr - /`"
35 if false;then
36         stream="`lynx -source http://di.fm${pathslashed}|sed -n 's/^File1=//p'|tr -d '\r'`"
37         exec mplayer -ao oss:/dev/dsp -cache 128 "$stream" "$@"
38         exit 1
39 else
40         while :;do
41                 streamlist="`lynx -source http://${pathslashed}|sed -n \
42                                 -e 's/^File[0-9]*=//p' \
43                                 -e 's#^.*"\(mms://[^"]*\)".*$#\1#p' \
44                                 |tr -d '\r'`"
45                 for stream in $streamlist;do
46                         # Using "set -e" here:
47                         # mplayer rc 0 on failure to read the stream
48                         # mplayer rc 0 on 'q'
49                         # mplayer rc 1 on ctrl-c
50                         ip="`  echo "$stream"|sed -n 's#^http://\([^:/]*\).*$#\1#p'`"
51                         if [ -n "$ip" ];then
52                                 port="`echo "$stream"|sed -n 's#^http://[^:/]*:\([0-9][0-9]*\).*$#\1#p'`"
53                                 port="${port:-80}"
54                                 echo >&2 "Checking $stream -> {$ip}:{$port} ..."
55                                 if ! nc -w 2 "$ip" "$port";then
56                                         echo >&2 "TIMEOUT: $stream"
57                                         continue
58                                 fi
59                         fi
60                         mplayer -ao alsa:device=hw=0 -cache 128 "$stream" "$@"
61                 done
62         done
63 fi