#!/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
    --version               Shows the version
    -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.
    -H dir --html=dir       Export to html. The files are saved in dir/
    -s --size=size          Window size. This overrides the size value
                            of the presentation
    -p PAGE_NUM             Jumps to page PAGE_NUM
    --color-names           Show all the available color names
'''


if __name__ == '__main__':

    import sys
    import getopt

    try:
        opts, args = getopt.getopt(sys.argv[1:], 'c:H:s:p:hfK',
            ('help', 'version', 'compile=', 'keep-dir', 
             'html=', 'size=', 'color-names'))
    except getopt.GetoptError, e:
        print >> sys.stderr, __doc__
        sys.exit(2)

    compile = None
    fullscreen = False
    keep_dir = False
    export_dir = None
    size = None
    start_page = 0

    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 in ('-H', '--html'):
            export_dir = arg

        elif opt in ('-s', '--size'):
            size = arg

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

        elif opt == '--version':
            import Pyslide
            print 'Pyslide', Pyslide.VERSION
            sys.exit(0)

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

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

        elif opt == '-p':
            try:
                start_page = int(arg)
            except ValueError:
                print 'Invalid page number', arg
                sys.exit(1)

        elif opt == '--color-names':
            from pygame.colordict import THECOLORS as d
            width = max(map(lambda x: len(x), d.keys()))
            for c, v in d.items():
                print c.ljust(width), '%3d, %3d, %3d' % v[:3]
            sys.exit(0)

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

    # Load psyco if it is available, only for a x86 machines.
    # This is because psyco is only available in this arch :-(.
    # This check avoids unnecesary message in non-x86 machines.
    import os
    if os.uname()[4] in ('i386', 'i486', 'i586', 'i686'):
        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')

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

    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 PyslideError, i:
            print >> sys.stderr, 'Invalid file "%s":' % filename, i
            os.chdir(cwd)
            continue

        if compile is not None:
            if compile[0] != '/':
                compile = os.path.join(cwd,  compile)

            Compile(f, open(compile, 'w'), fullscreen, size=size)

        elif export_dir is not None:
            if export_dir[0] != '/':
                export_dir = os.path.join(cwd, export_dir)

            from Pyslide.Export import Html
            Html(f, size).export(export_dir)

        else:
            try:
                p = BuildPresentation(f, 
                    fullscreen=fullscreen,
                    start_page = start_page,
                    size=size)
                p.run()
            except PyslideError, e:
                print 'pyslide: error:', e

        os.chdir(cwd)

