#!/bin/bash
[% c("var/set_default_env") -%]
[% pc(c('var/compiler'), 'var/setup', { compiler_tarfile => c('input_files_by_name/' _ c('var/compiler')) }) %]
distdir=/var/tmp/dist/[% project %]
mkdir -p /var/tmp/build

[% IF c("var/android") %]
  TORCONFIGDIR="$distdir/assets/common"
  libsdir=$distdir/jniLibs/[% c("var/abi") %]
  mkdir -p $libsdir $TORCONFIGDIR "$distdir/Tor"
  # We need to make sure our ZSTD dependencey is properly picked up.
  # --enable-zstd is not enough.
  export ZSTD_CFLAGS=-I/var/tmp/dist/zstd/include
  export ZSTD_LIBS=/var/tmp/dist/zstd/lib/libzstd.a
[% END %]

[% IF c("var/linux") || c("var/windows") %]
  TORCONFIGDIR="$distdir/Data/Tor"
  mkdir -p $TORCONFIGDIR
  mkdir -p "$distdir/Tor"
[% END %]

[% IF c("var/osx") %]
  [% IF c("var/nightly") %]
    # We are compiling Tor with Rust enabled for nightlies.
    export TOR_RUST_TARGET=x86_64-apple-darwin
  [% END %]
  TORBINDIR="$distdir/Contents/MacOS/Tor"
  TORCONFIGDIR="$distdir/Contents/Resources/TorBrowser/Tor"
  mkdir -p $distdir/bin
  mkdir -p $TORBINDIR
  mkdir -p $TORCONFIGDIR
[% END %]

[% IF c("var/windows") || c("var/android") %]
  tar -C /var/tmp/dist -xf [% c('input_files_by_name/zlib') %]
  zlibdir=/var/tmp/dist/zlib
[% END %]
[% IF c("var/android") %]
  tar -C /var/tmp/dist -xf [% c('input_files_by_name/zstd') %]
[% END %]
[% IF !c("var/android") && c("var/nightly") %]
  tar -C /var/tmp/dist -xf [% c('input_files_by_name/rust') %]
  export PATH=/var/tmp/dist/rust/bin:$PATH
[% END %]
tar -C /var/tmp/dist -xf [% c('input_files_by_name/openssl') %]
tar -C /var/tmp/dist -xf [% c('input_files_by_name/libevent') %]
tar -C /var/tmp/build -xf [% project %]-[% c('version') %].tar.gz
libeventdir=/var/tmp/dist/libevent
openssldir=/var/tmp/dist/openssl/openssl

[% IF c("var/windows") %]
  [% IF c("var/nightly") %]
    # We are compiling Tor with Rust enabled for nightlies.
    export TOR_RUST_TARGET=[% c("arch") %]-pc-windows-gnu
  [% END %]
  # XXX: It's a bit unfortunate but we need the pthread dll here as libevent
  # gets compiled with pthread support once it is enabled in GCC, which we need
  # to do as otherwise the Rust compilaton breaks.
  mingwlibs=/var/tmp/dist/mingw-w64/[% c("arch") %]-w64-mingw32/bin/
  cp $zlibdir/lib/*.dll $openssldir/bin/*.dll $libeventdir/bin/*.dll \
      $gcclibs/*.dll $mingwlibs/libwinpthread-1.dll $distdir/Tor/
[% END %]

[% IF c("var/linux-i686") && c("var/nightly") -%]
  export TOR_RUST_TARGET=i686-unknown-linux-gnu
[% END -%]
[% IF c("var/linux") %]
  mkdir -p "$distdir/Debug/Tor"
  cp $openssldir/lib/libssl.so.1.1 "$distdir/Tor/"
  cp $openssldir/lib/libcrypto.so.1.1 "$distdir/Tor/"
  cp $libeventdir/lib/libevent-2.1.so.7 "$distdir/Tor/"
  # We need to copy the libstdc++.so.6 for Tor Browser on older Linux distros.
  # Copying it into /Browser, which feels more natural, and amending
  # LD_LIBRARY_PATH breaks updates from a Tor Browser with the old
  # LD_LIBRARY_PATH value to the Tor Browser with the newer one. Thus, we copy
  # the libstdc++ into the directory with the libs tor depends on, too. See bug
  # 13359 for further details.
  mkdir -p "$distdir/Tor/libstdc++"
  cp /var/tmp/dist/gcc/[% c("var/libdir") %]/libstdc++.so.6 "$distdir/Tor/libstdc++/"
  [% IF c("var/asan") -%]
    cp /var/tmp/dist/gcc/[% c("var/libdir") %]/libasan.so.5 "$distdir/Tor/"
    cp /var/tmp/dist/gcc/[% c("var/libdir") %]/libubsan.so.1 "$distdir/Tor/"
  [% END -%]
  chmod 700 "$distdir"/Tor/*.so*
  chmod 700 "$distdir"/Tor/libstdc++/*.so*
  # This is needed to make RPATH unavailable. See bug 9150.
  export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$distdir/Tor/"
[% END %]

[% IF c("var/osx") %]
  cp $libeventdir/lib/libevent-*.dylib $TORBINDIR/
[% END %]

cd /var/tmp/build/[% project %]-[% c('version') %]
# add git hash to micro-revision.i for #24995
echo '"[% c("abbrev", { abbrev_length => 16 }) %]"' > micro-revision.i
./autogen.sh
find -type f -print0 | xargs -0 [% c("var/touch") %]
[% IF !c("var/android") && c("var/nightly") %]TOR_RUST_DEPENDENCIES=`pwd`/src/ext/rust/crates[% END %] ./configure --disable-asciidoc --with-libevent-dir="$libeventdir" --with-openssl-dir="$openssldir" \
    [% IF c("var/asan") %]--enable-fragile-hardening[% END %] \
    [% IF c("var/windows") || c("var/android") %]--with-zlib-dir="$zlibdir"[% END %] \
    [% IF c("var/osx") %]--enable-static-openssl[% END %] \
    [% IF !c("var/android") && c("var/nightly") %]--enable-rust[% END %] \
    --prefix="$distdir" [% c("var/configure_opt") %]
[% IF c("var/osx") -%]
  export LD_PRELOAD=[% c("var/faketime_path") %]
  export FAKETIME="[% USE date; GET date.format(c('timestamp'), format = '%Y-%m-%d %H:%M:%S') %]"
[% END -%]
make -j[% c("buildconf/num_procs") %]
make install
[% IF c("var/osx") -%]
  unset LD_PRELOAD
[% END -%]
cp $distdir/share/tor/geoip $TORCONFIGDIR
cp $distdir/share/tor/geoip6 $TORCONFIGDIR

[% IF c("var/osx") %]
  LIBEVENT_FILE=`basename $libeventdir/lib/libevent-*.dylib`
  cd $distdir
  cp bin/tor $TORBINDIR/
  cd $TORBINDIR/
  x86_64-apple-darwin-install_name_tool -change $libeventdir/lib/$LIBEVENT_FILE @executable_path/$LIBEVENT_FILE tor
[% END %]

cd $distdir
[% IF c("var/windows") %]
  install -s $distdir/bin/tor.exe $distdir/Tor/
[% IF c("var/expert_bundle") %]
  install -s $distdir/bin/tor-gencert.exe $distdir/Tor/
[% END %]
[% END %]

[% IF c("var/linux") %]
  # Strip and generate debuginfo for libs
  objcopy --only-keep-debug $distdir/bin/tor $distdir/Debug/Tor/tor
  install -s $distdir/bin/tor $distdir/Tor/
  objcopy --add-gnu-debuglink=./Debug/Tor/tor $distdir/Tor/tor
  for i in $distdir/Tor/*so*
  do
      LIB=`basename $i`
      objcopy --only-keep-debug $distdir/Tor/$LIB $distdir/Debug/Tor/$LIB
      strip $distdir/Tor/$LIB
      objcopy --add-gnu-debuglink=./Debug/Tor/$LIB $distdir/Tor/$LIB
  done
[% END %]

[% IF c("var/android") %]
  install -s --strip-program=[% c("var/cross_prefix") %]-strip $distdir/bin/tor $libsdir/libTor.so
[% END %]

mkdir -p [% dest_dir _ '/' _ c('filename') %]
[%
   IF c("var/expert_bundle");
     SET win_arch = c('var/windows-x86_64') ? 'win64' : 'win32';
     c('zip', {
        zip_src => [ 'Data', 'Tor', ],
        zip_args => dest_dir _ '/' _ c('filename') _ '/tor-' _ win_arch _ '-' _ c("version") _ '.zip',
      });
   ELSE;
     IF c("var/osx");
       SET tarsrcdir = [ 'Contents' ];
     ELSIF c("var/android");
       SET tarsrcdir = [ 'assets', 'jniLibs' ];
     ELSE;
       SET tarsrcdir = [ 'Data', 'Tor', ];
     END;
     c('tar', {
          tar_src => tarsrcdir,
          tar_args => '-czf ' _ dest_dir _ '/' _ c('filename') _ '/tor.tar.gz',
      });
   END;
%]

[% IF c("var/linux") %]
[% c('tar', {
        tar_src => [ 'Debug' ],
        tar_args => '-cJf ' _ dest_dir _ '/' _ c('filename') _ '/tor-debug.tar.xz',
    }) %]
[% END %]
