#!/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')) }) %]

mkdir -p /var/tmp/dist

tar -C /var/tmp/dist -xf $rootdir/[% c('input_files_by_name/elfutils') %]
tar -C /var/tmp/dist -xf $rootdir/[% c('input_files_by_name/binutils') %]
export PATH="/var/tmp/dist/binutils/bin:$PATH"

tar -C /var/tmp/dist -xf $rootdir/[% project %]-[% c("version") %].tar.gz

cd /var/tmp/dist
mv [% project %]-[% c("version") %] [% project %]
cd [% project %]
scons -Q arch=[% c("var/selfrando_arch") %] LIBELF_PATH="/var/tmp/dist/elfutils" NO_MREMAP=1 FORCE_INPLACE=1 DEBUG_LEVEL=env WRITE_LAYOUTS=env LOG=console
# Fix the path where we install selfrando
sed -i 's|^SR_PATH=.*|SR_PATH=/var/tmp/dist/selfrando/out/[% c("var/selfrando_arch") %]/bin|' Tools/TorBrowser/tc-wrapper/ld

cd /var/tmp/dist
[% c('tar', {
        tar_src => [ project ],
        tar_args => '-czf ' _ dest_dir _ '/' _ c('filename'),
        }) %]
