#!/usr/bin/python
# -*- coding: latin1 -*-
#
# Copyright (C) 2003 Ayose Cazorla Len
#
# Authors
#       Ayose Cazorla <ayose.cazorla@hispalinux.es>
#
# This file is part of Pyslide.
# 
# Pyslide 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.
# 
# Pyslide 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 Pyslide; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

'''
pyslide [options] file

    -h --help               Shows this help
    -c dest --compile=dest  Compile the presentation into dest
    -f                      Use fullscreen on start
    -K --keep-dir           Don't change the current directory. By default,
                            Pyslide sets the CWD to the presentation 
                            directory.
'''


if __name__ == '__main__':

    import sys
    import getopt

    try:
        opts, args = getopt.getopt(sys.argv[1:], 'c:hfK',
                    ['help', 'compile=', 'keep-dir'])
    except getopt.GetoptError:
        print >> sys.stderr, __doc__
        sys.exit(2)

    if len(args) < 1:
        print >> sys.stderr, __doc__
        sys.exit(2)


    compile = None
    fullscreen = False
    keep_dir = False

    for opt, arg in opts:
        if opt in ('-c', '--compile'):
            if len(args) != 1:
                print >> sys.stderr, 'You can only compile one presentation at time'
                sys.exit(2)
            compile = arg

        elif opt == '-f':
            fullscreen = True

        elif opt in ('-h', '--help'):
            print __doc__
            sys.exit(0)

        elif opt in ('-K', '--keep-dir'):
            keep_dir = True

    import os.path, os
    from Pyslide.Presentation import BuildPresentation
    from Pyslide.File import ReadFile, Compile, InvalidXML

    # Load psyco if it is available
    try:
        import psyco
        psyco.profile()
    except ImportError:
        print >> sys.stderr, ('\n'
            'Psyco is not available. If you install it you will get '
            'a better performance')

    for filename in args:
        cwd = os.getcwd()
        if not keep_dir:
            path, filename = os.path.split(filename)
            if path: os.chdir(path)

        try:
            f = ReadFile(filename)
        except InvalidXML, i:
            print >> sys.stderr, 'InvalidXML in %s:' % filename, i
            os.chdir(cwd)
            continue

        if compile is None:
            from Pyslide import misc
            try:
                p = BuildPresentation(f, fullscreen)
                p.run()
            except misc.PyslideError, e:
                print 'pyslide: error:', e

        else:
            Compile(f, open(compile, 'w'), fullscreen)

        os.chdir(cwd)

