#!/bin/bash
[% c("var/set_default_env") -%]
[% c("var/setarch") -%]
[% pc(c('var/compiler'), 'var/setup', { compiler_tarfile => c('input_files_by_name/' _ c('var/compiler')) }) %]
distdir="/var/tmp/dist/[% project %]"
[% c("var/set_PTDIR_DOCSDIR") -%]
mkdir -p $PTDIR $DOCSDIR
mkdir -p /var/tmp/build
tar -C /var/tmp/build -xf $rootdir/[% project %]-[% c('version') %].tar.gz
cd /var/tmp/build/[% project %]-[% c('version') %]
tar -C /var/tmp/dist -xf $rootdir/[% c('input_files_by_name/gmp') %]
ln -s /var/tmp/dist/gmp thirdparty/gmp
mkdir -p $distdir
tar -C $distdir -xf $rootdir/[% c('input_files_by_name/obfsproxy') %]
[% IF c("var/linux-i686") -%]
  export CFLAGS=-m32
  export CXXFLAGS=-m32
  export LDFLAGS=-m32
[% END -%]
[% IF c("var/windows") -%]
  patch -p1 < $rootdir/bug20302.patch
  pydir=$distdir/python
  export FAKETIME="[% USE date; GET date.format(c('timestamp'), format = '%Y-%m-%d %H:%M:%S') %]"
  export LD_PRELOAD=
  export PYTHONPATH="$(wine winepath -w $pydir)\\Lib\\site-packages"
  # FTE only needs libgmp-10.dll and no libgmpxx anymore.
  cp -a /var/tmp/dist/gmp/bin/libgmp-10.dll .
  cp -a /var/tmp/dist/gmp/bin/libgmp-10.dll $distdir/TorBrowser/Tor/
  WINDOWS_BUILD=1 CROSS_COMPILE=1 make
  $PYTHON setup.py install --prefix=$(wine winepath -w "$pydir")
  $PYTHON setup.py install
[% ELSE -%]
  export PYTHON=python2[% IF c("var/linux-i686") %].7[% END %]
  make
  cp -ra fte $PTDIR/
  mkdir -p $PTDIR/fteproxy-lib
  cp /var/tmp/dist/gmp/lib/libgmp.so.10 $PTDIR/fteproxy-lib
[% END -%]
cp -a {LICENSE,README.md} $DOCSDIR/
cp -a thirdparty/re2/LICENSE $DOCSDIR/LICENSE.re2
cd $distdir
[% c('tar', {
        tar_src => [ '.' ],
        tar_args => '-czf ' _ dest_dir _ '/' _ c('filename'),
    }) %]
