#!/usr/bin/env python

import os,sys,glob
from distutils import util
from distutils.core import setup
from distutils.command.install_data import install_data
############################################################
#
#  Overriding/extending functionality of DistUtils
#
############################################################

# A simplified data_files
class data_files:
    def __init__(self, base_dir=None, files=None, copy_to=None):
        self.base_dir = base_dir
        self.files = files
        self.copy_to = copy_to

class install_data_files(install_data):
    def run(self):
        self.outfiles = []
        install_cmd = self.get_finalized_command('install')
 
        for d in self.data_files:
            if d.base_dir is None:
                install_dir = self.install_dir
            else:
                # alternative base dir given => overwrite install_dir
                install_dir = getattr(install_cmd, d.base_dir)

            # copy to an other directory 
            if d.copy_to != None:
                if not os.path.isabs(d.copy_to):
                    # relatiev path to install_dir
                    install_dir = os.path.join(install_dir, d.copy_to)
                elif install_cmd.root:
                    # absolute path and alternative root set 
                    install_dir = change_root(self.root, d.copy_to)
                else:
                    # absolute path
                    install_dir = d.copy_to                  
 
            dir = os.path.normpath(install_dir)
            # create path
            self.mkpath(dir)
 
            # copy all files    
            for src in d.files:
                (out, copied) = self.copy_file(src, dir)
                self.outfiles.append(out)
        return self.outfiles
 
    def get_inputs (self):
        inputs = []
        for d in self.data_files:
            inputs.extend(d.files)
        return inputs
  
    def get_outputs (self):
         return self.outfiles
        
    
############################################################
#
#  Actual Setup Code
#
############################################################

longDesc = """
BisonGen is a general-purpose parser generator that converts a grammar
description for an LALR(1) context-free grammar into a program to
parse that grammar.
"""

if util.get_platform()[:3] == 'win':
    scripts = ['scripts/BisonGen.bat']
else:
    scripts = ['scripts/BisonGen']

from BisonGen import __version__

setup(name="BisonGen",
      version=__version__,
      description="A general-purpose parser generator for Python",
      author="Fourthought, Inc.",
      author_email="support@4suite.org",
      url="http://www.4suite.org",
      long_description=longDesc,

      # Command function overrides
      cmdclass={'install_data' : install_data_files,
                },

      # Install data files to doc/{name}-{version}/
      options={'install' : {'install_data' : '$base/doc/$dist_fullname'}},

      # This implies packages are in ./BisonGen
      packages=['BisonGen',
                'BisonGen.Lexer',
                'BisonGen.C',
                'BisonGen.Grammar',
                'BisonGen.Python',
                ],

      scripts=scripts,

      data_files=[data_files(base_dir='install_lib',
                             copy_to='BisonGen/C',
                             files=['BisonGen/Templates/Simple.c']),
                  data_files(base_dir='install_lib',
                             copy_to='BisonGen/Python',
                             files=['BisonGen/Templates/Simple.py']),
                  ],
      )

