#!/bin/bash
[% c("var/set_default_env") -%]
[% pc(c('var/compiler'), 'var/setup', { compiler_tarfile => c('input_files_by_name/' _ c('var/compiler')) }) %]
mkdir -p /var/tmp/build
tar -C /var/tmp/build -xf nsis-[% c('version') %].tar.bz2

# We need the GNU assembler for the system plugin
tar -C /var/tmp/dist -xf [% c('input_files_by_name/binutils') %]

# NSIS requires zlib and we later set the path using ZLIB_W32.
tar -C /var/tmp/build -xf [% c('input_files_by_name/zlib') %]

# This trick is adapted from Firefox's
# taskcluster/scripts/misc/build-mingw32-nsis.sh
compiler_prefix=/var/tmp/dist/mingw-w64-clang/bin/[% c("arch") %]-w64-mingw32
cat <<'EOF' >"$compiler_prefix-gcc"
#!/bin/sh
# SCons ignores the external $PATH, so we add binutils here.
export PATH=/var/tmp/dist/binutils/bin:$PATH
case "$@" in
*/Call*.S)
  $(dirname $0)/[% c("arch") %]-w64-mingw32-clang -fno-integrated-as "$@"
  ;;
*)
  $(dirname $0)/[% c("arch") %]-w64-mingw32-clang "$@"
  ;;
esac
EOF

chmod +x "$compiler_prefix-gcc"
ln -s "$compiler_prefix-clang++" "$compiler_prefix-g++"

builddir=/var/tmp/build/nsis-[% c('version') %]-src
cd "$builddir"

# These two sed commands also come from build-mingw32-nsis.sh
sed -i 's/-Wl,--exclude-libs,msvcrt.a/-Wl,-Xlink=-fixed/' SCons/Config/gnu
sed -i '2i extern "C"' SCons/Config/{memcpy,memset}.c

# Resource.dll does not obey the source date epoch...
patch -p1 < "$rootdir/resource-reproducible.diff"

[% IF c("var/windows-x86_64") %]
  # Seems like setting TARGET_ARCH is not enough so we need to patch build.cpp
  # manually; this is confirmed by a comment in the same file.
  sed -i 's/m_target_type=TARGET_X86UNICODE/m_target_type=TARGET_AMD64/' Source/build.cpp
  [% SET target = "amd64" %]
[% ELSE %]
  [% SET target = "x86" %]
[% END %]

[% SET scons_args = 'VERSION=' _ c("version")
        _ " SKIPUTILS='NSIS Menu,Makensisw' XGCC_W32_PREFIX=" _ c("arch") _ "-w64-mingw32-"
        _ " TARGET_ARCH=" _ target
        _ " ZLIB_W32=/var/tmp/build/zlib/"
        _ ' PREFIX=/var/tmp/dist/nsis' -%]
# Parallel compilation might result in errors, but they seems to be recoverable,
# so we first run scons in parallel, and suppress any error, then we try again,
# but the second time they make the build fail.
scons [% scons_args %] -j[% c("num_procs") %] || true
scons [% scons_args %]
scons [% scons_args %] install

# Plugins
target="[% target %]-unicode"
plugins="/var/tmp/dist/nsis/share/nsis/Plugins/$target"
plugin_lib="$builddir/build/urelease/api/nsis/libpluginapi-$target.a"

# This is not needed to build installers, but it makes easier to adapt plugins
# outside our build system.
mkdir /var/tmp/dist/nsis/share/nsis/Lib
cp $plugin_lib /var/tmp/dist/nsis/share/nsis/Lib/

$compiler_prefix-clang++ -I "$builddir/build/urelease/api" -O2 $plugin_lib \
  -DUNICODE "$rootdir/ShellLink.cpp" \
  -Wl,--no-insert-timestamp -lole32 -luuid -shared -o "$plugins/ShellLink.dll"
llvm-strip "$plugins/ShellLink.dll"

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