summaryrefslogtreecommitdiffstats
path: root/streams/streams
blob: 60a4bfc20c5f4872eb1a64a2892d1b4b51420321 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#! /bin/bash
HERE=$(dirname $(readlink -f $0))
URLS=`cat $HERE/stream.db`
CURRENT_STREAM="no stream" #will be set when calling `status`
if [ ! `id -u` -eq "0" ]; then
  exec sudo "$0" "$@"
fi

#if [ ! `id -u` -eq "0" ]; then
#  echo "we are going sudo..."
#  exec sudo "$0" "$@"
#fi

function start() {
  # start the given stream von $1
  REQ=$1
  tmux start-server 
  if status &>/dev/null ; then
    echo "!! Stream already running!"
    exit 1
  fi

  if echo "$URLS" | while read URL NAME; do
    if [ "$NAME" = "$REQ" ];then
      tmux new-session -s streams -n streams -d "while sleep 1;do mplayer $URL;done"  
      echo "** $REQ started"
      exit 1
    fi
  done; then
    echo "!! Stream '$REQ' not found!"
    exit 1
  fi
}
function stop()
{
  #stops every stream
  if status &>/dev/null; then
    status | cut -d\  -f2 | xargs printf "** killing %s\n"
    tmux kill-session -t streams 2>/dev/null || echo "!! killing session failed" 
  else
    echo "** no stream running"; 
    return 1
  fi
}

function status()
{
  #tmux has-session -t streams 2>/dev/null
  #RET=$?

  #tmux list-sessions  2>/dev/null
  #return $RET
  EV="`ps -ef | grep mplayer`"
  if echo "$URLS" | while read URL NAME; do
    if [ "`echo "$EV" | grep \"$URL\"`" ] ;then
      echo "** $NAME running ($URL)"
      exit 1
    fi
  done; then
    echo "** no stream running"
    return 1
  else
    return 0
  fi
}
function current()
{

  return 1
}
function list()
{
  echo "$URLS" | while read URL NAME ; do
    echo "$NAME : $URL"
  done

}

function shorthelp()
{
    echo "start|stop|restart|status|list [audio stream]"
}
function longhelp()
{
  B=`basename $0`
  echo -n "Usage: $B "
  shorthelp
  echo " get all available streams with '/$B list'
Examples: 
  $B list
  $B start groove
  $B restart deepmix
  $B status
  $B stop"
}


case "$1" in
  start)
    start $2
    ;;
  stop)
    stop
    ;;
  (switch|restart)
    stop
    start $2
    ;;
  status)
    status
    exit $?
    ;;
  list)
    list
    ;;
  (--help)
    shorthelp
    ;;
  *)
    longhelp
  ;;
esac