# q - quit
# B - fast backwards
# F - fast forwards
# b - backwards
# f - forwards
# 0 - pause
# + - step forwards
# - - step backwards
# s - start clip
# e - end clip

import string, sys, os, popen2, fcntl, select


def makeNonBlocking(fd):
    fl = fcntl.fcntl(fd, fcntl.F_GETFL)
    try:
	fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NDELAY)
    except AttributeError:
	fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.FNDELAY)

print "LAV Edit List"
print "PAL"

f = os.popen("ls "+sys.argv[1]+"*-*.avi|grep --count ^", "r")
while 1:
    line = f.readline()
    if not line:
        break
    print string.strip(line)
f.close()
f = os.popen("ls -1 "+sys.argv[1]+"*-*.avi|sed s*"+sys.argv[1]+"**", "r")
while 1:
    line = f.readline()
    if not line:
        break
    print string.strip(line)
f.close()

sys.stdout.flush()

child = popen2.Popen3("xgamma -gamma 2.2;lavplay -q -a 15 -g -S 704x576 -F "+sys.argv[1]+"*.avi;xgamma -gamma 1.0", 1) # capture stdout and stderr from command
#child.tochild.close()             # don't need to talk to child
outfile = child.fromchild 
outfd = outfile.fileno()
infile = child.tochild 
infd = infile.fileno()
errfile = child.childerr
errfd = errfile.fileno()
stdin = sys.stdin
stdinfd = stdin.fileno()
makeNonBlocking(outfd)            # don't deadlock!
makeNonBlocking(infd)            # don't deadlock!
makeNonBlocking(stdinfd)            # don't deadlock!
makeNonBlocking(errfd)
outdata = errdata = ''
outeof = erreof = 0
speed = ":"
#frame = Array()
#frame[0] = "a"
#frame[1] = "a"
frame = string.split("1/1/1", "/")
while 1:
    ready = select.select([outfd,errfd,stdinfd],[],[]) # wait for input
#    gurk = gurk + 1
#    if gurk == 100:
#        print gurk
#        infile.write("q\n")
#        infile.flush()
    if outfd in ready[0]:
        outchunk = outfile.read()
        if outchunk == '':
            outeof = 1
        if outchunk != '' and outchunk[0] == "@":
            frame = string.split(outchunk, "/")
    if errfd in ready[0]:
        errchunk = errfile.read()
        if errchunk == '': erreof = 1
#        errdata = errdata + errchunk
#        print "ERR: " + errchunk
    if stdinfd in ready[0]:
        stdinchunk = stdin.read()
        if stdinchunk[0] == 'q':
            infile.write("q\n")
            infile.flush()
        if stdinchunk[0] == 'B' or stdinchunk[0] == '7':
            speed = "b"
            infile.write("p-1\n")
            infile.flush()
        if stdinchunk[0] == 'F' or stdinchunk[0] == '9':
            speed = "f"
            infile.write("p1\n")
            infile.flush()
        if stdinchunk[0] == 'b' or stdinchunk[0] == '4':
            speed = ":"
            infile.write("p-1\n")
            infile.flush()
        if stdinchunk[0] == 'f' or stdinchunk[0] == '6':
            speed = ":"
            infile.write("p1\n")
            infile.flush()
        if stdinchunk[0] == '0' or stdinchunk[0] == '5':
            speed = ":"
            infile.write("p0\n")
            infile.flush()
        if stdinchunk[0] == '+' or stdinchunk[0] == '3':
            speed = ":"
            infile.write("s+1\n")
            infile.flush()
        if stdinchunk[0] == '-' or stdinchunk[0] == '1':
            speed = ":"
            infile.write("s-1\n")
            infile.flush()
        if stdinchunk[0] == 's' or stdinchunk[0] == '8':
            startfile = frame[1][:-4]
            if not startfile:
                startfile = 0
            else:
                startfile = string.atoi(startfile)
            print startfile, frame[1][-4:],
#            sys.stdout.flush()
        if stdinchunk[0] == 'e' or stdinchunk[0] == '2':
            endfile = frame[1][:-4]
            if not endfile:
                endfile = 0
            else:
                endfile = string.atoi(endfile)
            if startfile != endfile:
                file = startfile
#                print file, startfile, endfile, frame
                while file != endfile:
                    print "9999"
                    file = file + 1
                    print file, "0000",
            print frame[1][-4:]
            sys.stdout.flush()
            
    if (string.atoi(frame[1]) % 25) == 24:
        if speed == "f":
            infile.write("s+726\n")
            infile.flush()
        if speed == "b":
            infile.write("s-101\n")
            infile.flush()
    if outeof and erreof: break
    
#    select.select([],[],[],.5) # give a little time for buffers to fill

err = child.wait()
if err != 0: 
    raise RuntimeError, '%s failed w/ exit code %d\n%s' % (command, err, errdata)
