#!/bin/bash
[% c("var/set_default_env") -%]
[% pc(c('var/compiler'), 'var/setup', { compiler_tarfile => c('input_files_by_name/' _ c('var/compiler')),
                                        hardened_gcc => 0 }) %]
[% IF c("var/linux-cross") -%]
  [% pc('gcc', 'var/setup', {
    compiler_tarfile => c('input_files_by_name/' _ 'gcc-host'),
    hardened_gcc => 0,
    target => [ c('var/channel'), 'basebrowser-linux-x86_64' ]
  }) %]

  # GCC's cc1 expects Jessie libmpfr; we feed it a symlink to Buster's instead
  # as a stupid compat cludge.  TODO: Maybe copy in the actual Jessie version
  # from the GCC build container?
  mkdir -p /var/tmp/dist/libmpfr
  ln -s -T /usr/lib/x86_64-linux-gnu/libmpfr.so.6 /var/tmp/dist/libmpfr/libmpfr.so.4
  export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/var/tmp/dist/libmpfr"
[% END -%]
distdir=/var/tmp/dist/[% project %]
mkdir -p $distdir
mkdir -p /var/tmp/build

[% IF c("var/android") %]
  # 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 %]

TORDATADIR="$distdir/data"
TORBINDIR="$distdir/tor"

mkdir $TORDATADIR
mkdir $TORBINDIR

[% 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 %]
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/linux") %]
  TORDEBUGDIR="$distdir/debug"
  mkdir "$TORDEBUGDIR"

  cp $openssldir/lib/libssl.so.1.1 "$TORBINDIR"
  cp $openssldir/lib/libcrypto.so.1.1 "$TORBINDIR"
  cp $libeventdir/lib/libevent-2.1.so.7 "$TORBINDIR"
  # 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.
  cp [% c("var/libpath") %]/libstdc++.so.6 "$TORBINDIR"
  [% IF c("var/asan") -%]
    cp [% c("var/libpath") %]/libasan.so.6 "$TORBINDIR"
    cp [% c("var/libpath") %]/libubsan.so.1 "$TORBINDIR"
  [% END -%]
  [% IF c("var/linux-cross") %]
    cp -a /var/tmp/dist/gcc-cross/[% c("var/crosstarget") %]/lib/libssp.so* "$distdir/Tor/"
    # Required for libevent
    export LDFLAGS="$LDFLAGS -Wl,-rpath-link=/var/tmp/dist/gcc-cross/[% c("var/crosstarget") %]/lib"
    # Required for zlib
    export LDFLAGS="$LDFLAGS -L/usr/lib/[% c('var/crosstarget') %]"
    cp -a /usr/include/zconf.h /usr/include/zlib.h /var/tmp/dist/gcc-cross/[% c('var/crosstarget') %]/include

    TARGET_OBJCOPY=/var/tmp/dist/gcc-cross/bin/[% c("var/crosstarget") %]-objcopy
    TARGET_STRIP=/var/tmp/dist/gcc-cross/bin/[% c("var/crosstarget") %]-strip
  [% ELSE %]
    TARGET_OBJCOPY=objcopy
    TARGET_STRIP=strip
  [% END %]
  chmod 700 "$TORBINDIR"/*.so*
  # This is needed to make RPATH unavailable. See bug 9150.
  export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$TORBINDIR"
[% END %]

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

[% IF c("var/windows") %]
  export CC=[% c("arch") %]-w64-mingw32-cc
[% END %]

cd /var/tmp/build/[% project %]-[% c('version') %]

[% IF c("var/android") -%]
# Remove reference to OpenSSL 1.1.1b. See https://gitlab.torproject.org/tpo/core/tor/-/issues/40759
  sed -i 's/ A fix is expected to appear in OpenSSL //g' src/lib/tls/tortls_openssl.c
  sed -i 's/1.1.1b.//g' src/lib/tls/tortls_openssl.c
[% END -%]

# 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("touch") %]
./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/macos") %]--enable-static-openssl[% END %] \
    [% IF c("var/windows") %]--enable-static-libevent --enable-static-openssl --enable-static-zlib[% END %] \
    --prefix="$distdir" [% c("var/configure_opt") %]
[% IF c("var/macos") -%]
  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("num_procs") %]
make install
[% IF c("var/macos") -%]
  unset LD_PRELOAD
[% END -%]

cp $distdir/share/tor/geoip "$TORDATADIR"
cp $distdir/share/tor/geoip6 "$TORDATADIR"

[% IF c("var/macos") %]
  LIBEVENT_FILE=`basename $libeventdir/lib/libevent-*.dylib`
  cd $distdir
  cp bin/tor "$TORBINDIR"
  cd "$TORBINDIR"
  [% c("var/build_target") %]-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 "$TORBINDIR"
  install -s $distdir/bin/tor-gencert.exe "$TORBINDIR"
[% END %]

[% IF c("var/linux") %]
  # Strip and generate debuginfo for libs
  $TARGET_OBJCOPY --only-keep-debug $distdir/bin/tor "$TORDEBUGDIR/tor"
  install -s --strip-program="$TARGET_STRIP" $distdir/bin/tor "$TORBINDIR"
  $TARGET_OBJCOPY --add-gnu-debuglink="$TORDEBUGDIR/tor" "$TORBINDIR/tor"
  for i in "$TORBINDIR"/*so*
  do
      LIB=`basename $i`

      if [ $LIB == 'libstdc++.so.6' ]; then
        # keeping this separate to maintain reproducibility; we can probably
        # treat this the same as the rest (though it seems libstdc++ doesn't come with
        # any useful debug symbols since we don't build it, so maybe we should figure
        # out how to package them
        $TARGET_STRIP "$TORBINDIR/$LIB"
      else
        $TARGET_OBJCOPY --only-keep-debug "$TORBINDIR/$LIB" "$TORDEBUGDIR/$LIB"
        $TARGET_STRIP "$TORBINDIR/$LIB"
        $TARGET_OBJCOPY --add-gnu-debuglink="$TORDEBUGDIR/$LIB" "$TORBINDIR/$LIB"
      fi
  done
[% END %]

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

[%
  SET tar_src = [ "tor", "data" ];
  IF c("var/linux");
   tar_src.push("debug");
  END;
  c('tar', {
    tar_src => tar_src,
    tar_args => '-czf ' _ dest_dir _ '/' _ c('filename'),
  });
%]
