#!/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

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

mkdir $TORDATADIR $TORBINDIR $TORDOCSDIR

[% 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') %]
  zstddir=/var/tmp/dist/zstd
  # We need to make sure our ZSTD dependencey is properly picked up.
  # --enable-zstd is not enough.
  export ZSTD_CFLAGS=-I$zstddir/include
  export ZSTD_LIBS=$zstddir/lib/libzstd.a
[% 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"

  # 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"

cp LICENSE "$TORDOCSDIR/tor.txt"

[% 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`

      "$OBJCOPY" --only-keep-debug "$TORBINDIR/$LIB" "$TORDEBUGDIR/$LIB"
      "$STRIP" "$TORBINDIR/$LIB"
      "$OBJCOPY" --add-gnu-debuglink="$TORDEBUGDIR/$LIB" "$TORBINDIR/$LIB"
  done
[% END %]

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

pushd $TORDOCSDIR
cp $libeventdir/LICENSE libevent.txt
cp $openssldir/LICENSE.txt openssl.txt
[% IF c("var/windows") || c("var/android") -%]
  cp $zlibdir/LICENSE zlib.txt
[% END -%]
[% IF c("var/android") -%]
  cp $zstddir/LICENSE zstd-bsd.txt
  cp $zstddir/COPYING zstd-gpl2.txt
[% END -%]
popd

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