import os
import os.path

NAME = 'tintii'
VERSION = '2.2.3'
DISTRO = NAME + '-' + VERSION

##
## Environment
##
env = Environment(ENV = os.environ)

# Command line options
opts = Variables('config.py')
opts.AddVariables(PathVariable('PREFIX',
                               'Installation directory', '/usr/local'),
                  BoolVariable('NDEBUG',
                               'Set to disable assertion checking', 0),
                  BoolVariable('SKIPCONFIG',
                               'Set to skip configuration', 0),
                  ('CXX', 'C++ compiler'),
                  ('CXXFLAGS', 'C++ compiler flags'),
                  ('LINKFLAGS', 'Linker flags'),
               )
opts.Update(env)
if env['NDEBUG']:
  env.Append(CPPDEFINES = 'NDEBUG')

# Help
Help(opts.GenerateHelpText(env))

##
## Configuration
##
if not env.GetOption('clean') and not env['SKIPCONFIG']:
  conf = Configure(env)

  # Check for wxWidgets
  if not conf.CheckCXXHeader(os.path.join('wx', 'wx.h')):
    print 'wxWidgets not found'
    Exit(1)

  # Check for boost ublas
  if not (conf.CheckCXXHeader(os.path.join('boost', 'numeric', 'ublas',
      'vector.hpp')) and conf.CheckCXXHeader(os.path.join('boost', 'numeric',
      'ublas', 'matrix.hpp'))):
    print 'Boost.uBLAS not found'
    Exit(1)

##
## Build
##

# Compile
objects = []
headers = []
releases = []

Export(['env', 'objects', 'headers', 'releases'])
env.SConscript(os.path.join('src', 'SConscript'))

# Build program
program = env.Program(target = NAME, source = objects)


##
## Release
##

# Add meta files to release
releases += ['README.txt',
             'LICENSE.txt',
             'VERSION.txt',
             'Doxyfile',
             'SConstruct',
             'config.py',
             os.path.join('src', 'nuvola', 'README.txt'),
             os.path.join('images', 'splash.xpm'),
             os.path.join('images', 'nuvola', 'license.txt'),
             os.path.join('images', 'nuvola', 'readme.txt'),
             os.path.join('images', 'nuvola', 'button_accept.png'),
             os.path.join('images', 'nuvola', 'button_cancel.png'),
             os.path.join('images', 'nuvola', 'decrypted.png'),
             os.path.join('images', 'nuvola', 'down.png'),
             os.path.join('images', 'nuvola', 'encrypted.png'),
             os.path.join('images', 'nuvola', 'exit.png'),
             os.path.join('images', 'nuvola', 'fileclose.png'),
             os.path.join('images', 'nuvola', 'filenew.png'),
             os.path.join('images', 'nuvola', 'fileopen.png'),
             os.path.join('images', 'nuvola', 'filesaveas.png'),
             os.path.join('images', 'nuvola', 'filesave.png'),
             os.path.join('images', 'nuvola', 'finish.png'),
             os.path.join('images', 'nuvola', 'messagebox_info.png'),
             os.path.join('images', 'nuvola', 'up.png'),
             os.path.join('images', 'nuvola', 'viewmag1.png'),
             os.path.join('images', 'nuvola', 'viewmagfit.png'),
             os.path.join('images', 'nuvola', 'viewmag-.png'),
             os.path.join('images', 'nuvola', 'viewmag.png'),
             os.path.join('images', 'nuvola', 'viewmag+.png')
             ]

# Copy files to directory
oldDir = os.getcwd()
newDir = DISTRO
for release in releases:
  # for absolute paths...
  to = release.replace(oldDir, newDir)

  # for relative paths...
  if not to.startswith(newDir):
    to = os.path.join(newDir, to)

  env.Command(to, release, Copy(to, release))

# Tar
releaseEnv = Environment(TARFLAGS = '-cz')
tar = releaseEnv.Tar(DISTRO + '.tar.gz', DISTRO)


##
## Build control
##
Alias('build', [ program ])
Alias('release', [ tar ])
Alias('releasedir', [ DISTRO ])
Alias('all', [ 'build', 'release' ])
Default('build')

Clean('release', [ Dir(DISTRO) ])
