#!/usr/bin/python

#Audio Tools, a module and set of tools for manipulating audio data
#Copyright (C) 2007-2012  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 sys
import os.path
import audiotools
import audiotools.text as _

if (__name__ == '__main__'):
    parser = audiotools.OptionParser(
        usage=_.USAGE_DVDAINFO,
        version="Python Audio Tools %s" % (audiotools.VERSION))

    parser.add_option('-A', '--audio-ts', action='store', default=None,
                      type='string', dest='audio_ts', metavar='DIR',
                      help=_.OPT_AUDIO_TS)

    (options, args) = parser.parse_args()

    msg = audiotools.Messenger("dvdainfo", options)

    if (options.audio_ts is None):
        msg.error(_.ERR_NO_AUDIO_TS)
        sys.exit(1)

    try:
        dvda = audiotools.DVDAudio(options.audio_ts)
    except audiotools.InvalidDVDA, err:
        msg.error(unicode(err))
        sys.exit(1)
    except OSError, err:
        msg.os_error(err)
        sys.exit(1)

    titleset = dvda[0]
    for (title_num, title) in enumerate(titleset):
        (sample_rate,
         channels,
         channel_mask,
         bits_per_sample,
         stream_type) = title.info()

        msg.new_row()
        msg.output_column(_.LAB_DVDA_TITLE % (title_num + 1), True)
        msg.output_column(_.LAB_DVDA_TRACKS % (len(title.tracks)), True)
        msg.output_column(u" : ")
        msg.output_column(_.LAB_DVDA_TITLE_INFO %
                          {"minutes": title.pts_length / 90000 / 60,
                           "seconds": title.pts_length / 90000 % 60,
                           "channels": channels,
                           "rate": audiotools.khz(sample_rate),
                           "bits": bits_per_sample,
                           "type": {0xA0: u"PCM",
                                    0xA1: u"MLP"}.get(stream_type,
                                                      u"UNKNOWN")})
    msg.blank_row()
    msg.output_rows()

    msg.new_row()
    msg.output_column(_.LAB_DVDAINFO_TITLE)
    msg.output_column(u" ")
    msg.output_column(_.LAB_DVDAINFO_TRACK)
    msg.output_column(u" ")
    msg.output_column(_.LAB_DVDAINFO_LENGTH)
    msg.output_column(u"   ")
    msg.output_column(_.LAB_DVDAINFO_FILENAME)
    msg.output_column(u" ")
    msg.output_column(_.LAB_DVDAINFO_STARTSECTOR)
    msg.output_column(u" ")
    msg.output_column(_.LAB_DVDAINFO_ENDSECTOR)
    msg.output_column(u" ")
    msg.output_column(_.LAB_DVDAINFO_TICKS)

    msg.divider_row([u"-", u" ", u"-", u" ", u"-", u" ", u"-",
                     u" ", u"-", u" ", u"-", u" ", u"-"])

    for (title_num, title) in enumerate(titleset):
        for (track_num, track) in enumerate(title.tracks):
            for (i, (aob_file,
                     start_sector,
                     end_sector)) in enumerate(track.sectors()):
                msg.new_row()
                if (i == 0):
                    msg.output_column(unicode(title_num + 1), True)
                    msg.output_column(u" ")
                    msg.output_column(unicode(track_num + 1), True)
                    msg.output_column(u" ")
                    msg.output_column(_.LAB_TRACK_LENGTH %
                                      (track.pts_length / 90000 / 60,
                                       track.pts_length / 90000 % 60),
                                      True)
                else:
                    msg.output_column(u"")
                    msg.output_column(u" ")
                    msg.output_column(u"")
                    msg.output_column(u" ")
                    msg.output_column(u"")
                msg.output_column(u"   ")
                msg.output_column(
                    unicode(audiotools.Filename(os.path.basename(aob_file))),
                    True)
                msg.output_column(u" ")
                msg.output_column(unicode(start_sector), True)
                msg.output_column(u" ")
                msg.output_column(unicode(end_sector - 1), True)
                msg.output_column(u" ")
                if (i == 0):
                    msg.output_column(unicode(track.pts_length), True)
                else:
                    msg.output_column(u" ")

    msg.output_rows()
