#!/usr/bin/env python
import os, time

waitSeconds = 10
command = "/home/pmcnett/RealPlayer9/realplay http://www.kqed.org/w/streamingfiles/kqed_real.ram"

def startRealPlay():
  return os.system("%s &" % command )

def getPID():
  pidFile = os.popen("ps x | grep -i '%s'" % command[:32], 'r',-1)
  pids = pidFile.readlines()
  pid = None
  if len(pids) > 0:
    for entry in pids:
      if "grep" not in entry:
        pid = entry.split()[0]
        break
  return pid

# Kill existing:
while 1:
  pid = getPID()
  if pid == None:
    break
  else:
    print "Killing process %s" % pid
    os.system("kill %s" % pid)
 
while 1:
  pid = getPID()
  if pid == None:
    retval = startRealPlay()
    if retval == 0:
      pid = getPID()
      print "Successfully started RealPlayer with PID %s" % pid
    else:
      print "Error Starting Realplay: %s" % retval

  time.sleep(waitSeconds)

