Extended for 'sky.fm' to find out 'ambient' is still on 'di.fm'.
[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         streamlist="`lynx -source http://${pathslashed}|sed -n \
41                         -e 's/^File[0-9]*=//p' \
42                         -e 's#^.*"\(mms://[^"]*\)".*$#\1#p' \
43                         |tr -d '\r'`"
44         for stream in $streamlist;do
45                 # Using "set -e" here:
46                 # mplayer rc 0 on failure to read the stream
47                 # mplayer rc 0 on 'q'
48                 # mplayer rc 1 on ctrl-c
49                 ip="`  echo "$stream"|sed -n 's#^http://\([^:/]*\).*$#\1#p'`"
50                 if [ -n "$ip" ];then
51                         port="`echo "$stream"|sed -n 's#^http://[^:/]*:\([0-9][0-9]*\).*$#\1#p'`"
52                         port="${port:-80}"
53                         echo >&2 "Checking $stream -> {$ip}:{$port} ..."
54                         if ! nc -w 2 "$ip" "$port";then
55                                 echo >&2 "TIMEOUT: $stream"
56                                 continue
57                         fi
58                 fi
59                 mplayer -ao oss:/dev/dsp -cache 128 "$stream" "$@"
60         done
61 fi