#!/bin/bash
[% c("var/set_default_env") -%]
distdir="/var/tmp/dist/[% project %]"
[% pc(c('var/compiler'), 'var/setup', { compiler_tarfile => c('input_files_by_name/' _ c('var/compiler')) }) %]
[% IF ! c("var/windows") -%]
  [% c("var/set_PTDIR_DOCSDIR") -%]
  mkdir -p $PTDIR
[% END -%]
mkdir -p /var/tmp/build
tar -C /var/tmp/build -xf $rootdir/Twisted-[% c('version') %].tar.bz2
cd /var/tmp/build/Twisted-[% c('version') %]
[% IF c("var/linux-i686") -%]
  export CFLAGS=-m32
  export CXXFLAGS=-m32
  export LDFLAGS=-m32
[% END -%]
[% IF c("var/windows") -%]
  pydir="$distdir/python"
  export FAKETIME="[% USE date; GET date.format(c('timestamp'), format = '%Y-%m-%d %H:%M:%S') %]"
  export LD_PRELOAD=[% c("var/faketime_path") %]
  # twisted/internet/iocpreactor/iocpsupport/iocpsupport.c includes "python.h"
  # rather than "Python.h".
  ln -sf Python.h /var/tmp/dist/winpython/include/python.h
  # We need to set the "mingw32" compiler to avoid an error in build_ext, but
  # Twisted's "install" command calls build_ext unconditionally, whether the
  # extensions have been built already or not, so we can't just call build_ext
  # separately as with other packages. The "install" command doesn't recognize
  # the -c option, so we set the compiler in a configuration file.
  echo $'[build_ext]\ncompiler=mingw32' > setup.cfg
  mkdir -p $pydir/Lib/site-packages
  export PYTHONPATH="$(wine winepath -w $pydir)\\Lib\\site-packages"
  LD_PRELOAD= $PYTHON setup.py install --single-version-externally-managed --record /dev/null --prefix=$(wine winepath -w $pydir)
[% ELSE -%]
  export PYTHON=python2[% IF c("var/linux-i686") %].7[% END %]
  $PYTHON setup.py build --build-lib build
  cp -a build/twisted $PTDIR/
[% END -%]
cd $distdir
[% c('tar', {
        tar_src => [ '.' ],
        tar_args => '-czf ' _ dest_dir _ '/' _ c('filename'),
    }) %]
