"""
$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/qp/setup.py $
$Id: setup.py 28060 2006-03-13 15:55:59Z dbinger $
"""
import os
import sys
from os import curdir, symlink
from os.path import join, exists
from distutils.core import setup
from distutils.extension import Extension
from shutil import copytree
try:
    from qpy.setup import qpy_build_py
except ImportError:
    print "Please install Qpy first."
    raise SystemExit

if __name__ == '__main__':
    if len(sys.argv) == 3 and sys.argv[1] == 'install_sites_link':
        sites_directory = sys.argv[2]
        if not exists(sites_directory):
            print sites_directory, 'does not exist.'
        else:
            if not exists(join(sites_directory, '__init__.py')):
                print sites_directory, 'needs an __init__.py.'
            else:
                import qp
                sites_place = join(qp.__path__[0], 'sites')
                if exists(sites_place):
                    print sites_place, 'already exists.'
                else:
                    symlink(sites_directory, sites_place)
    elif len(sys.argv) == 3 and sys.argv[1] == 'install_demo_sites':
        sites_directory = sys.argv[2]
        if exists(sites_directory):
            print sites_directory, 'already exists.'
            print "To install the demo sites, copy them "
            print "from the 'demo' directory into %r" % sites_directory
        else:
            copytree('demo', sites_directory)
            f = open(join(sites_directory, "__init__.py"), "w")
            f.close()
    else:
        if os.name == 'posix':
            Passfd = Extension(name="qp.hub.passfd", sources=['hub/passfd.c'])
            ext_modules = [Passfd]
        else:
            ext_modules = []
        setup(name="qp",
              version="1.5",
              author="CNRI",
              author_email="webmaster@mems-exchange.org",
              url="http://www.mems-exchange.org/software/qp/",
              package_dir=dict(qp=curdir),
              scripts=['bin/qp', 'bin/qpcensus.py'],
              license="See LICENSE.txt",
              packages=['qp',
                        'qp.http',
                        'qp.hub',
                        'qp.pub',
                        'qp.fill',
                        'qp.lib',
                        'qp.mail',
                        ],
              ext_modules = ext_modules,
              description="A web-application framework.",
              cmdclass= {'build_py': qpy_build_py},
              long_description=(
            "QP is a package for defining and running multiple web "
            "applications based on Durus for persistence, standard persistent "
            "Session and User classes, easy interactive database sessions, "
            "qpy for assembling html, and Quixote2-style forms and path "
            "traversal.  ")
              )
