#!/usr/bin/python
#
# Trktime: Report the total time of all of the tracks in a CD track file.
# G. Forrest Cook 2002/2/18
#
# usage: Trktime file.trk
#
import sys
import string

if len(sys.argv) != 2:
    print 'usage: %s file.trk' % sys.argv[0]
    sys.exit (1)

#
# Get the track file name.
#
trkfile = sys.argv[1]

totalmin = 0
totalsec = 0
totalsubsec = 0

#
# Read the track file, pull the FILE lines out and
# grab the 4th item in each line, that's the track length
# in min:sec:subsec format.
#
trkf = open (trkfile, 'r')
for line in trkf.readlines ():
    if line[0:4] == 'FILE':
#	print "Line:" + line
	tline = string.split (line)
#	print "sub:" + tline[3]

	sline = string.split (tline[3],':')
	min = string.atoi(sline[0])
	sec = string.atoi(sline[1])
	subsec = string.atoi(sline[2])
	totalmin = totalmin + min 
	totalsec = totalsec + sec 
	totalsubsec = totalsubsec + subsec

trkf.close ()

#
# Now do the div/mod stuff to total the minutes and seconds.
#
totalsec = totalsec + (totalsubsec + 50)/100
totalmin = totalmin + totalsec/60 
totalsec = totalsec%60
print "\n%s total time: %02d:%02d\n" %(trkfile,totalmin,totalsec)

sys.exit (0)
