#!/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 $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
[% ELSIF c("var/linux-cross") %]
  # Since 1. we are using Debian's zlib1g-dev:$arch_debian, 2. our
  # cross-toolchain's default paths (i.e. -I and -L) are not the same
  # as those of Debian's cross-toolchain, and 3. tor's configure does
  # not support separate header and library directories for zlib, we
  # need to make the headers and $arch_debian library available to
  # configure manually.
  # DO NOT use CPPFLAGS="-I/usr/include" to include the headers, the
  # build will fail (probably because some of our cross-$arch_debian
  # headers get masked by the native ones).
  CROSS_INCLUDEDIR=/var/tmp/dist/gcc/[% c("var/crosstarget") %]/include
  ln -s /usr/include/zconf.h $CROSS_INCLUDEDIR
  ln -s /usr/include/zlib.h $CROSS_INCLUDEDIR
  export LDFLAGS="-L/usr/lib/[% c("var/crosstarget") %] $LDFLAGS"
[% 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.[% c('compress_tar') %]
libeventdir=/var/tmp/dist/libevent
openssldir=/var/tmp/dist/openssl

[% IF c("var/linux") %]
  TORDEBUGDIR="$distdir/debug"
  mkdir "$TORDEBUGDIR"

  cp $openssldir/lib/libssl.so.3 "$TORBINDIR"
  cp $openssldir/lib/libcrypto.so.3 "$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.
  libdir=[% c("var/libdir") %]
  [% IF c("var/linux-cross") -%]
    libdir="[% c("var/crosstarget") %]/$libdir"
  [% END -%]
  cp "/var/tmp/dist/gcc/$libdir/libstdc++.so.6" "$TORBINDIR"
  [% IF c("var/asan") -%]
    cp "/var/tmp/dist/gcc/$libdir/libasan.so.6" "$TORBINDIR"
    cp "/var/tmp/dist/gcc/$libdir/libubsan.so.1" "$TORBINDIR"
  [% 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') %]

# 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 %] \
    [% IF c("var/linux-cross") %]--build=x86_64-linux-gnu[% END %] \
    --enable-gpl --prefix="$distdir" [% c("var/configure_opt") %]
[% IF c("var/macos") -%]
  [% c("var/faketime_setup") %]
[% 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") %]
  # With Debian bookworm strip changes the date time, llvm-strip doesn't do it.
  install -s --strip-program=llvm-strip $distdir/bin/tor.exe "$TORBINDIR"
  install -s --strip-program=llvm-strip $distdir/bin/tor-gencert.exe "$TORBINDIR"
[% END %]

[% IF c("var/linux") %]
  [% IF c("var/linux-cross") -%]
    CROSS_PREFIX=[% c("var/crosstarget") %]-
  [% END -%]

  OBJCOPY="${CROSS_PREFIX}objcopy"
  STRIP="${CROSS_PREFIX}strip"

  # Strip and generate debuginfo for libs
  "$OBJCOPY" --only-keep-debug $distdir/bin/tor "$TORDEBUGDIR/tor"
  install -s --strip-program="$STRIP" $distdir/bin/tor "$TORBINDIR"
  "$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
        "$STRIP" "$TORBINDIR/$LIB"
      else
        "$OBJCOPY" --only-keep-debug "$TORBINDIR/$LIB" "$TORDEBUGDIR/$LIB"
        "$STRIP" "$TORBINDIR/$LIB"
        "$OBJCOPY" --add-gnu-debuglink="$TORDEBUGDIR/$LIB" "$TORBINDIR/$LIB"
      fi
  done
[% END %]

[% IF c("var/android") %]
  install -s --strip-program=llvm-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 => '-caf ' _ dest_dir _ '/' _ c('filename'),
  });
%]
