#!/usr/bin/python

# subtitles = Copyright (c) 2002, 2003 John Morton.
#
# Permission to use, copy, modify, distribute, and sell this software and
# its documentation for any purpose is hereby granted without fee, 
# provided that the above copyright notice appear in all copies and that 
# both that copyright notice and this permission notice appear in supporting
# documentation.  No representations are made about the suitability of this
# software for any purpose.  It is provided "as is" without express or 
# implied warranty.

"""
Reads a file supplied on command line and displays each line
in sequence. With some sort of time sequence information, this
could be used to drive lyrics or subtitles, or something.
"""

import sys, os, time

command = 'yaf-splash -transparent -bw 0 -placement bottom -timeout 2 -text "%s" >/dev/null'

if len(sys.argv) != 2:
    print "Usage: %s [file]" % sys.argv[0]
    sys.exit(1)
    
filename = sys.argv[1]

if os.path.isfile(filename):
    file_h = open(sys.argv[1], 'r')
    for line in file_h.xreadlines():
        line = line.rstrip()
        if line == "":
            # Blank lines act as a pause.
            time.sleep(2)
        else:
            # escape any double quotes and dollars
            line = line.replace('"','\\\"')
            line = line.replace("$","\\\$")            
            res = os.system(command % line)
            if res << 8 != 0:
                # yaf-splash was killed by some signal
                sys.exit(1)
            elif res >> 8 != 0:
                # yaf-splash died for some other reason
                sys.exit(1)

    file_h.close()
