#! /usr/bin/python
#
# This file is part of Advene.
#
# Advene 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.
#
# Advene 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 Foobar; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#

import sys
import os
import StringIO

# Magic stuff before the instanciation of Advene : we set the
# sys.path and the various config.data.path

def fix_paths(path):
  # We override any modification that could have been made in
  # .advenerc. Rationale: if the .advenerc was really correct, it
  # would have set the correct package path in the first place.
  config.data.path['resources']=os.path.sep.join((maindir, 'share'))
  config.data.path['locale']=os.path.sep.join( (maindir, 'locale') )
  config.data.path['web']=os.path.sep.join((maindir, 'share', 'web'))
  config.data.path['advene']=maindir
  config.data.path['plugins']=os.path.sep.join( (maindir, 'vlc') )


# Try to find if we are in a development tree.
(maindir, subdir) = os.path.split(os.path.dirname(os.path.abspath(sys.argv[0])))
if subdir == 'bin' and  os.path.exists(os.sep.join((maindir, "setup.py"))):
  # Chances are that we were in a development tree...
  libpath=os.sep.join((maindir, "lib"))
  print "You seem to have a development tree at:\n%s." % libpath
  sys.path.insert (0, libpath)

  import advene.core.config as config
  fix_paths(maindir)
else:
  try:
    import advene.core.config as config
  except ImportError:
    print """Cannot guess a valid directory.
    Please check your installation or set the PYTHONPATH environment variable."""
    sys.exit(1)

# Maybe we are running from a pyinstaller version
if not os.path.exists(config.data.path['resources']):
  maindir=os.path.abspath(os.path.dirname(sys.executable))
  if os.path.exists(os.path.join( maindir, 'share' )):
    # There is a 'share' directory at the same level as the executable
    # This can mean that we are in a pyinstaller version
    print "Seemingly running from a pyinstaller version in\n%s" % maindir
    fix_paths(maindir)

if config.data.os == 'win32':
  import _winreg

  # Encoding stuff, to please py2exe
  if hasattr(sys, 'setdefaultencoding'):
    import locale
    loc = locale.getdefaultlocale()
    if loc[1]:
      encoding = loc[1]
      sys.setdefaultencoding(encoding)

  #win32 platform, add the "lib" folder to the system path
  os.environ['PATH'] += ";lib;"

import advene.util.importer

if __name__ == '__main__':

    if config.data.args != 2:
        print "Should provide a file name and a package name"
        sys.exit(1)

    fname, pname = config.data.args

    i = advene.util.importer.get_importer(fname)
    if i is None:
        print "No importer for %s" % fname
        sys.exit(1)

    # FIXME: i.process_options()
    # i.process_options(sys.argv[1:])
    # (for .sub conversion for instance, --fps, --offset)
    print "Converting %s to %s using %s..." % (fname, pname, i.name)
    p=i.process_file(fname)
    p.save(pname)
    print "done."
    print i.statistics_formatted()

