# -*- mode: python; -*-
import os
import subprocess
import sys

vpath = '#/third_party/thrift-0.8.0'

env = DefaultEnvironment()
env.Replace(CC = 'gcc')
env.Replace(CXX = 'g++')
config_opts = '--prefix=' + str(Dir('#/build')) + ' ' + '--disable-dependency-tracking ' + '--with-boost=' + str(Dir('#/build')) + ' ' + '--without-csharp --without-java --without-erlang --without-python ' + '--without-perl --without-php --without-ruby --without-haskell  --without-go --host x86_64-linux'

if not sys.platform.startswith('freebsd'):
    cmd = ('(cd ' + Dir('.').abspath + '; ' + str(Dir(vpath)) +
           '/configure' + ' ' + config_opts + '; make clean; make; make install)')
else:
    cmd = ('(cd ' + Dir('.').abspath + '; ' + str(Dir(vpath)) +
           '/configure' + ' ' + config_opts + '; gmake clean; gmake; gmake install)')

version = '0.8.0'
libs = [ str(File('#build/lib/libthrift.a')),
         str(File('#build/lib/libthriftasio.a')) ]
shlibs = [ str(File('#build/lib/libthrift-' + version + '.so')),
           str(File('#build/lib/libthriftasio.so.0.0.0')) ]


products = [ str(File('#build/bin/thrift')),
             str(File('#build/include/thrift/Thrift.h')) ]
products += libs + shlibs

thrift_cfg = env.Command('config.status', str(Dir(vpath)), cmd)

env.SideEffect(products, thrift_cfg)
env.Requires(thrift_cfg, '#/build/include/boost')

env.Alias('install', env.Install(env['INSTALL_LIB'], shlibs))
env.Alias('install',
          env.Symlink(env['INSTALL_LIB'] + '/libthriftasio.so' + '.0',
                      env['INSTALL_LIB'] + '/libthriftasio.so' + '.0.0.0'))

async_files = ['TAsioAsync.h', 'TAsync.h', 'TFuture.h']
for file in async_files:
    env.Install('#build/include/thrift/async',
                vpath + '/lib/cpp/src/async/' + file)

