#! /usr/bin/env python

import os
import distutils.core
from quixote.ptl.qx_distutils import qx_build_py

local_cfg = None
if os.path.exists('larpe/larpe_cfg.py'):
    local_cfg = file('larpe/larpe_cfg.py').read()
    os.unlink('larpe/larpe_cfg.py')

if not os.path.exists('larpe/qommon'):
    print >> sys.stderr, 'Missing \'qommon\' module'
    print >> sys.stderr, 'Type these commands to get it :'
    print >> sys.stderr, '  $ cvs -d :pserver:anonymous@labs.libre-entreprise.org:/cvsroot/wcs login'
    print >> sys.stderr, 'Then the message "CVS password:" will be displayed, just type Enter'
    print >> sys.stderr, '  $ cvs -d :pserver:anonymous@labs.libre-entreprise.org:/cvsroot/wcs checkout -d larpe/qommon wcs/wcs/qommon'
    sys.exit(1)

def data_tree(destdir, sourcedir):
    extensions = ['.css', '.png', '.jpeg', '.jpg', '.xml', '.html', '.js']
    r = []
    for root, dirs, files in os.walk(sourcedir):
        l = [os.path.join(root, x) for x in files if os.path.splitext(x)[1] in extensions]
        r.append( (root.replace(sourcedir, destdir, 1), l) )
        if '.svn' in dirs:
            dirs.remove('.svn')
    return r

distutils.core.setup(
        name = 'larpe',
        version = "0.2.0",
        maintainer = "Damien Laniel",
        maintainer_email = "dlaniel@entrouvert.com",
        url = "http://larpe.labs.libre-entreprise.org",
        package_dir = { 'larpe': 'larpe' },
        packages = ['larpe', 'larpe.admin', 'larpe.ctl', 'larpe.qommon', 'larpe.qommon.admin', 'larpe.qommon.ident', 'larpe.qommon.backoffice', 
'larpe.filter'],
        cmdclass = {'build_py': qx_build_py},
        data_files = data_tree('share/larpe/web/', 'root/') + \
                data_tree('share/larpe/themes/', 'data/themes/')
    )

if local_cfg:
    file('larpe/larpe_cfg.py', 'w').write(local_cfg)
