#!/usr/bin/python

#Audio Tools, a module and set of tools for manipulating audio data
#Copyright (C) 2007-2010  Brian Langenberger

#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA


import os.path
import audiotools
import gettext

gettext.install("audiotools",unicode=True)

OUTPUT = _(u"%(hours)d:%(minutes)2.2d:%(seconds)2.2d")

if (__name__ == '__main__'):
    parser = audiotools.OptionParser(
        usage=_(u"%prog <track 1> [track 2] ..."),
        version="Python Audio Tools %s" % (audiotools.VERSION))

    (options,args) = parser.parse_args()
    msg = audiotools.Messenger("tracklength",options)

    total_length = 0l

    audio_files = audiotools.open_files(filter(os.path.isfile,args),
                                        messenger=msg)
    total_length += sum([file.cd_frames() for file in audio_files])

    for parent_dir in filter(os.path.isdir,args):
        for f in audiotools.open_directory(parent_dir,sorted=False,
                                           messenger=msg):
            total_length += f.cd_frames()

    if (total_length > 0):
        msg.output(OUTPUT % {"hours":total_length / (75 * 60 * 60),
                             "minutes":total_length / (75 * 60) % 60,
                             "seconds":int(round(total_length / 75.0)) % 60})
    else:
        msg.output(OUTPUT % {"hours":0,"minutes":0,"seconds":0})
