#!/usr/bin/env python3
from sys import argv
from os.path import exists

def showhelp():
    print("""Invocation:
{} tracklist.txt

where tracklist.txt can be named anything sane (no spaces, etc)
and it contains lines like this:
01:23 fubar
04:20 hello world
...

ie, track length, followed by space, followed by whatever,""".format(argv[0]))

def trackcalc(trackfile):
    prevhours=0
    prevminutes=0
    prevseconds=0
    times=["00:00:00"]
    titles=[]
    tracklist=[]
    with open(trackfile) as tracks:
        for track in tracks:
            t=track.split(' ', 1)[0]
            title=track.split(' ', 1)[1].strip()
            minutes=int(track.split(':')[0])
            seconds=track.split(':')[1]
            seconds=int(seconds[:2])
            s=((seconds + prevseconds)%60)
            m=int((seconds + prevseconds)/60)
            seconds=s
            minutes=(minutes + m + prevminutes)
            m=(minutes%60)
            hours=int(minutes/60)
            minutes=m
            hours=(hours + prevhours)
            tracktime="{}:{}:{}".format(str(hours).zfill(2),str(minutes).zfill(2),str(seconds).zfill(2))
            times.append(tracktime)
            titles.append(title)
            print("{} {}".format(times.pop(0),titles.pop(0)))
            prevhours=hours
            prevminutes=minutes
            prevseconds=seconds
#}
if len(argv) == 2:
    f=argv[1]
    if exists(f):
        tracks=f
        trackcalc(f)
    else:
        showhelp()
else:
    showhelp()
