#!/bin/bash

MINROOT=/usr/local/minerva
VARLOG=/var/log/minerva
LOGSTUB=$VARLOG/videostream
VLC=/usr/bin/vlc

date +"%F %H:%M CMD $0 $*" >>$VARLOG/bearskin/`basename $0`


if [ $# == 0 ]; then
  echo "Usage: $0 <device> <command> [url]"
  echo "   device ::= default"
  echo "   command ::= play stop geturl getcurrent isplaying getip getport"
  exit -1
fi

ALLARGS=$*
CMD=$2
URL=$3

. $MINROOT/conf/videostream.conf

DEVICE=`$MINROOT/bin/finddev videostream $*`
if [ $? == 0 ]; then
  echo $DEVICE
  exit 0;
fi


case $CMD in
  init)
       touch $LOGSTUB.err
       touch $LOGSTUB.pid
       echo 0 > $LOGSTUB.ply
       echo   > $LOGSTUB.url
    ;;

  enumdev)
     $MINBIN/enumdev videostream
     exit $?
     ;;

  getip)
       echo $SERVER_IP
       ;;

  getport)
       echo $SERVER_PORT
       ;;

  play)
     # The '-I dummy' Trick courtesy of Alexis & Christopher from
     # http://osdir.com/ml/video.videolan.vlc.general/2003-06/msg00226.html
     $VLC --sout='#standard{access=http,mux=ogg,dst='$SERVER_IP':'$SERVER_PORT'}' $URL -I dummy 2>>$LOGSTUB.err >/dev/null &

     if [ $? == 0 ]; then
       echo $! > $LOGSTUB.pid
       echo 1 > $LOGSTUB.ply
       echo $URL >$LOGSTUB.url
     fi
     ;;

  isplaying)
     if [ -f $LOGSTUB.ply ]; then
        cat $LOGSTUB.ply
     else
        echo 0
     fi
     ;;

  geturl)
     ISPLAYING=`$0 $DEVICE isplaying`
     if [ "$ISPLAYING" -eq 1 ]; then
        cat $LOGSTUB.url
     else
        echo
     fi
     ;;

  stop)
     ISPLAYING=`$0 $DEVICE isplaying`
     if [ "$ISPLAYING" -eq 1 ]; then
        kill -9 `cat $LOGSTUB.pid`
        echo   > $LOGSTUB.pid
        echo 0 > $LOGSTUB.ply
     fi
     ;;

  *)
    ;;
esac

