#!/bin/bash
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

cd $HOME

. /etc/sysconfig/get_iplayer

xterm -geometry 72x6-0+0 -bg white -fg black -title "get_iplayer Web PVR Manager" -iconic -e /usr/share/get_iplayer/get_iplayer.cgi -p $GET_IPLAYER_PORT -l $GET_IPLAYER_ADDRESS &

# Wait to start browser until server is started
sleep 1s
iterate=0
while [ -z "`ps -C get_iplayer.cgi -o pid=`" ]
do
  echo "Waiting for web pvr to start"
  sleep 1s
  ((iterate = iterate + 1))
  if [ $iterate -eq 15 ]
    then echo "The web pvr was unable to start."
    echo
    exit 1
  fi
done

# Start browser
if [ -z $BROWSER ]
then
  if [ "`which opera 2>/dev/null`" ]
    then export BROWSER=`which opera`
  elif [ "`which firefox 2>/dev/null`" ]
    then export BROWSER=`which firefox`
  elif [ "`which konqueror 2>/dev/null`" ]
    then export BROWSER=`which konqueror`
  elif [ "`which seamonkey 2>/dev/null`" ]
    then export BROWSER=`which seamonkey`
  elif [ "`which mozilla 2>/dev/null`" ]
    then export BROWSER=`which mozilla`
  else 
    echo "I cannot seem to find an internet browser"
    echo "Please run one and point it to:"
    echo ""
    echo "http://$GET_IPLAYER_ADDRESS:$GET_IPLAYER_PORT"
    exit 1
  fi
fi
sleep 2s
$BROWSER http://$GET_IPLAYER_ADDRESS:$GET_IPLAYER_PORT &

