#!/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 audiotools
import os.path
import subprocess
import gettext

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

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

    parser.add_option('--format',
                      action='store',
                      type='string',
                      default=None,
                      dest='format',
                      help=_(u'the format string for new filenames'))

    parser.add_option('-V', '--verbose',
                      action='store',
                      dest='verbosity',
                      choices=audiotools.VERBOSITY_LEVELS,
                      default=audiotools.DEFAULT_VERBOSITY,
                      help=_(u'the verbosity level to execute at'))

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

    audiofiles = audiotools.open_files(args, messenger=msg)

    if (len(audiofiles) < 1):
        msg.error(_(u"You must specify at least 1 supported audio file"))
        sys.exit(1)

    try:
        for track in audiofiles:
            track_metadata = track.get_metadata()

            new_name = track.track_name(file_path=track.filename,
                                        track_metadata=track_metadata,
                                        format=options.format)

            (path, filename) = os.path.split(track.filename)
            if (filename != new_name):
                try:
                    audiotools.make_dirs(os.path.join(path, new_name))
                except OSError:
                    msg.error(_(u"Unable to write \"%s\"") % \
                                      (new_name))
                    sys.exit(1)

                if (subprocess.call([audiotools.BIN['mv'], '-i',
                                     track.filename,
                                     os.path.join(path, new_name)]) == 0):
                    if (options.verbosity != 'quiet'):
                        msg.info(
                            u"%s -> %s" %
                            (msg.filename(track.filename),
                             msg.filename(os.path.join(path, new_name))))
                else:
                    sys.exit(1)
    except audiotools.UnsupportedTracknameField, err:
        err.error_msg(msg)
        sys.exit(1)
