#!/bin/bash
[% c("var/set_default_env") -%]
distdir=/var/tmp/dist/[% project %]
export TORBROWSER_VERSION='[% c("version") %]'

mkdir -p $distdir

scripts_dir=/var/tmp/build_scripts
mkdir -p "$scripts_dir"

OUTDIR='[% dest_dir _ "/" _ c("filename") %]'
mkdir -p $OUTDIR

# When we build with MULTI_LINGUAL=1, the browser will be packaged inside a
# directory named tor-browser (instead of tor-browser_en-US). Therefore we
# stage everything under tor-browser-stage to avoid a conflict.
PKG_DIR='[% c("var/project-name") %]'
TB_STAGE_DIR="$distdir/$PKG_DIR[% IF c('var/windows') %]/[% c('var/Project_Name') %][% END %]"
GENERATEDPREFSPATH=$rootdir/Bundle-Data/generated-prefs.js
# Create initially empty prefs file where we can dump our conditionally included/genetered prefs
touch "$GENERATEDPREFSPATH"

[% IF c("var/macos") %]
  TBDIR="$TB_STAGE_DIR/[% c('var/display_name') %].app"
  [% IF c("var/macos_universal") %]
    TBDIR_AARCH64="$TB_STAGE_DIR/[% c('var/display_name') %]-aarch64.app"
    TBDIRS=("$TBDIR" "$TBDIR_AARCH64")
  [% ELSE -%]
    TBDIRS=("$TBDIR")
  [% END -%]

  DOCSPATH=Contents/Resources/[% c('var/ProjectName') %]/Docs
  EXTSPATH=Contents/Resources/distribution/extensions
  TORBINPATH=Contents/MacOS/Tor
  TORCONFIGPATH=Contents/Resources/TorBrowser/Tor

  tar -C /var/tmp/dist -xf $rootdir/[% c('input_files_by_name/hfsplus-tools') %]
  tar -C /var/tmp/dist -xf $rootdir/[% c('input_files_by_name/libdmg') %]
  export PATH=/var/tmp/dist/hfsplus-tools:/var/tmp/dist/libdmg-hfsplus:$PATH
[% ELSE %]
  TBDIR="$TB_STAGE_DIR/Browser"
  TBDIRS=("$TBDIR")

  DOCSPATH=[% c('var/ProjectName') %]/Docs
  EXTSPATH=distribution/extensions
  TORBINPATH=TorBrowser/Tor
  TORCONFIGPATH=TorBrowser/Data/Tor

  [% IF c("var/tor-browser") -%]
    PROFILEPATH=[% c('var/ProjectName') %]/Data/Browser/profile.default/extensions
    mkdir -p "$TBDIR/$PROFILEPATH"
  [% END -%]
  [% IF !c("var/mullvad-browser") -%]
    # For Mullvad Browser, we keep the data directory at the same level of
    # Browser, instead of creating a Data directory with the program files.
    mkdir -p "$TBDIR/[% c('var/ProjectName') %]/Data/Browser/Caches"
  [% END -%]
[% END %]

for tbdir in "${TBDIRS[@]}"
do
  mkdir -p "$tbdir/$EXTSPATH"
done

[% IF c("var/updater_enabled") -%]
  # Extract the MAR tools.
  unzip -d $rootdir $rootdir/[% c('input_files_by_name/firefox') %]/mar-tools-*.zip
  MARTOOLS=$rootdir/mar-tools
[% END -%]

mv [% c('input_files_by_name/noscript') %] "$TBDIR/$EXTSPATH/{73a6fe31-595d-460b-a920-fcc0f8843232}.xpi"
[% IF c("var/mullvad-browser") -%]
  mv [% c('input_files_by_name/ublock-origin') %] "$TBDIR/$EXTSPATH/uBlock0@raymondhill.net.xpi"
  mv [% c('input_files_by_name/mullvad-extension') %] "$TBDIR/$EXTSPATH/{d19a89b9-76c1-4a61-bcd4-49e8de916403}.xpi"
[% END -%]
[% IF !c("var/macos") && c("var/tor-browser") -%]
  cp "$TBDIR/$EXTSPATH/{73a6fe31-595d-460b-a920-fcc0f8843232}.xpi" "$TBDIR/$PROFILEPATH/"
[% END %]

[% IF c("var/tor-browser") -%]
  function mv_tbdir {
    declare args=($@)
    local dest="${args[-1]}"
    unset args[-1]
    mv ${args[@]} "$TBDIR/$dest"
    [% IF c("var/macos_universal") -%]
      for src in ${args[@]}
      do
        mv "aarch64/$src" "$TBDIR_AARCH64/$dest"
      done
    [% END -%]
  }

  tar -xvf [% c('input_files_by_name/tor-expert-bundle') %]/tor-expert-bundle.tar.gz
  [% IF c("var/macos_universal") -%]
    mkdir aarch64
    tar -C aarch64 -xf [% c('input_files_by_name/tor-expert-bundle-aarch64') %]/tor-expert-bundle.tar.gz
  [% END -%]

  # geoip(6) and anything else that belongs in the data dir from the expert bundle
  mkdir -p "$TBDIR/$TORCONFIGPATH" [% IF c("var/macos_universal") %]"$TBDIR_AARCH64/$TORCONFIGPATH"[% END %]
  mv_tbdir data/{geoip,geoip6} "$TORCONFIGPATH"

  # pt_config.json will be consumed later in `bridges_conf`
  mv tor/pluggable_transports/pt_config.json $rootdir
  [% IF c("var/macos_universal") %]
    rm -f aarch64/tor/pluggable_transports/pt_config.json
  [% END -%]

  # Move READMEs from tor-expert-bundle to the doc dir
  mkdir -p "$TBDIR/$DOCSPATH/conjure" [% IF c("var/macos_universal") %]"$TBDIR_AARCH64/$DOCSPATH/conjure"[% END %]
  mv_tbdir tor/pluggable_transports/README.CONJURE.md "$DOCSPATH/conjure/README.md"

  # Move the PTs to where TB expects them
  mkdir -p "$TBDIR/$TORBINPATH" [% IF c("var/macos_universal") %]"$TBDIR_AARCH64/$TORBINPATH"[% END %]
  mv_tbdir tor/pluggable_transports "$TORBINPATH/PluggableTransports"

  # Move tor and dependencies to where TB expects them
  mv_tbdir tor/* "$TORBINPATH"

  # the expert bundle includes tor-gencert, which isn't needed for browser releases
  [% IF c("var/windows") %]
    rm "$TBDIR/$TORBINPATH/tor-gencert.exe"
  [% END %]

  tar -xf [% c('input_files_by_name/moat-settings') %]

  mkdir -p "$TBDIR/$DOCSPATH/Licenses" [% IF c("var/macos_universal") %]"$TBDIR_AARCH64/$DOCSPATH/Licenses"[% END %]
  for f in docs/*; do
    mv_tbdir $f $DOCSPATH/Licenses
  done
[% END -%]

for tbdir in "${TBDIRS[@]}"
do
  [% IF c("var/macos") -%]
    tbdir="$tbdir/Contents/Resources"
  [% END -%]
  tar -C "$tbdir" -xf [% c('input_files_by_name/fonts') %]
done

[% IF c("var/linux") %]
  cat > "${TB_STAGE_DIR}/start-[% c('var/project-name') %].desktop" << 'RBM_TB_EOF'
[% INCLUDE 'RelativeLink/start-browser.desktop' -%]
RBM_TB_EOF
  cat > "${TB_STAGE_DIR}/Browser/start-[% c('var/project-name') %]" << 'RBM_TB_EOF'
[% INCLUDE 'RelativeLink/start-browser' -%]
RBM_TB_EOF
  cat > "${TB_STAGE_DIR}/Browser/execdesktop" << 'RBM_TB_EOF'
[% INCLUDE 'RelativeLink/execdesktop' -%]
RBM_TB_EOF
  chmod +x "${TB_STAGE_DIR}/start-[% c('var/project-name') %].desktop" \
           "${TB_STAGE_DIR}/Browser/start-[% c('var/project-name') %]" \
           "${TB_STAGE_DIR}/Browser/execdesktop"
  cp "${TB_STAGE_DIR}/start-[% c('var/project-name') %].desktop" \
     "${TB_STAGE_DIR}/Browser"
  [% IF c("var/namecoin") %]
    pushd "${TB_STAGE_DIR}/Browser/"
    patch -p1 < $rootdir/namecoin.patch
    popd
  [% END %]
  # Make sure we get the desired scrollbar behavior with Gtk3, see bug 27546.
  GTK_SETTINGS_DIR="${TB_STAGE_DIR}/Browser/.config/gtk-3.0"
  mkdir -p $GTK_SETTINGS_DIR
  cp $rootdir/gtk3-settings.ini $GTK_SETTINGS_DIR/settings.ini
[% END %]

tar -C "${TB_STAGE_DIR}" -xf [% c('input_files_by_name/firefox') %]/browser.tar.[% c("compress_tar") %]

[% IF c("var/macos_universal") -%]
  # Use symlink in tmp dir to extract "Tor Browser.app" to "Tor Browser-aarch64.app"
  ff_aarch64=$(mktemp -d)
  ln -s "$TBDIR_AARCH64" "$ff_aarch64/[% c('var/display_name') %].app"
  tar -C $ff_aarch64 -xf [% c('input_files_by_name/firefox-aarch64') %]/browser.tar.[% c("compress_tar") %]
[% END %]

[% IF c("var/tor-browser") -%]
  # Include the Tor Browser manual
  TMP_MANUAL_PATH=$rootdir/tmp_manual/
  mkdir $TMP_MANUAL_PATH
  pushd $TMP_MANUAL_PATH
  tar -xf $rootdir/[% c('input_files_by_name/manual') %]
  find . -exec [% c("touch") %] {} \;
  for tbdir in "${TBDIRS[@]}"
  do
    find chrome/ | sort | zip -X -@ "$tbdir[% IF c('var/macos') %]/Contents/Resources[% END %]/browser/omni.ja"
  done
  popd
  rm -rf $TMP_MANUAL_PATH

  # on linux, libstdc++ lives in it's own directory
  [% IF c("var/linux") %]
    # For legacy reasons, libstdc++ is with tor binaries in Tor Browser.
    # We would have to test the updater to move it outside.
    mv "$TBDIR/libstdc++" "$TBDIR/$TORBINPATH/libstdc++"
  [% END %]
[% END -%]

[% IF c("var/namecoin") %]
  # Extract Electrum-NMC
  tar -C "$TBDIR/TorBrowser" -xf [% c('input_files_by_name/electrum-nmc') %]

  # Extract ncprop279
  mkdir "$TBDIR/TorBrowser/ncprop279"
  tar -C "$TBDIR/TorBrowser/ncprop279" -xf [% c('input_files_by_name/ncprop279') %]

  # Extract StemNS
  tar -C "$TBDIR/TorBrowser" -xf [% c('input_files_by_name/stemns') %]
[% END %]

[% IF c("var/linux");
     SET bundledata_osname = 'linux';
   ELSIF c("var/macos");
     SET bundledata_osname = 'mac';
   ELSIF c("var/windows");
     SET bundledata_osname = 'windows';
   END; %]

[% IF c("var/macos") %]
  tar -C "Bundle-Data/[% c('var/ProjectName') %].dmg" -c . | tar -C "$TB_STAGE_DIR" -x
  pushd "$TB_STAGE_DIR"
  cp [% c('var/channel') %].DS_Store .DS_Store
  rm *.DS_Store
  popd
[% END %]

for tbdir in "${TBDIRS[@]}"
do
  mkdir -p "$tbdir/$DOCSPATH"
  cp -a Bundle-Data/Docs/* "$tbdir/$DOCSPATH"
  [% IF c("var/tor-browser") -%]
    cp -a Bundle-Data/Docs-TBB/* "$tbdir/$DOCSPATH"
    tar -C Bundle-Data/[% bundledata_osname %] \
      [% IF ! c("var/namecoin") %]--exclude=*Electrum-NMC* --exclude=*ncprop279*[% END %] \
      -c . | tar -C "$tbdir[% IF ! c("var/macos") %]/[% c('var/ProjectName') %][% END %]" -x
  [% ELSIF c("var/mullvad-browser") -%]
    cp -a Bundle-Data/Docs-MB/* "$tbdir/$DOCSPATH"
  [% END -%]
done

[% IF c("var/tor-browser") -%]
  PT_CONFIG="$rootdir/pt_config.json"

  # Write our ClientTransportPlugin lines to torrc-defults
  for tbdir in "${TBDIRS[@]}"
  do
    PT_PATH='[% c("var/pt_path") %]'
    TORRC_DEFAULTS="$tbdir/$TORCONFIGPATH/torrc-defaults"
    jq --raw-output .pluggableTransports[] "${PT_CONFIG}" | while read -r line; do
      echo "${line/\$\{pt_path\}/${PT_PATH}}" >> "$TORRC_DEFAULTS"
    done
  done
[% END -%]

[% IF c("var/linux") && c("var/tor-browser") %]
  chmod 700 "${TB_STAGE_DIR}/Browser/[% c('var/ProjectName') %]/Data/Browser"
  chmod 700 "${TB_STAGE_DIR}/Browser/TorBrowser/Data/Tor"
[% END %]

for tbdir in "${TBDIRS[@]}"
do
  tbdir="$tbdir[% IF c('var/macos') %]/Contents/Resources[% END %]/"
  [% IF c("var/tor-browser") -%]
    pushd "$rootdir"
    config_dir=chrome/toolkit/content/global
    mkdir -p "$config_dir"
    cp moat_countries.json pt_config.json "$config_dir/"
    [% c("touch") %] "$config_dir/"*.json
    zip -Xm "$tbdir/omni.ja" "$config_dir/moat_countries.json" "$config_dir/pt_config.json"
    rm -rf chrome
    popd
  [% END -%]
  pushd "$tbdir/browser"
  unzip omni.ja defaults/preferences/[% c("var/prefs_file") %] || [ $? -lt 3 ]
  # Append our built extension-overrides.js to the preferences file
  cat "$GENERATEDPREFSPATH" >> defaults/preferences/[% c("var/prefs_file") %]
  cp defaults/preferences/[% c("var/prefs_file") %] $rootdir
  [% c("touch") %] defaults/preferences/[% c("var/prefs_file") %]
  zip -Xm omni.ja defaults/preferences/[% c("var/prefs_file") %]
  rm -rf defaults
  # create tbb_version.json (torbrowser) or version.json (mullvadbrowser)
  # file for tor-browser#25020 and tor-browser-build#41044
  echo '{"version":"[% c("var/torbrowser_version") %]","architecture":"[% c("var/mar_osname") %]","channel":"[% c("var/channel") %]","locale":"en-US"}' > ../[% c("var/version_json") %]
  popd
done

[% IF c("var/windows") %]
  tar -C /var/tmp/dist -xf $rootdir/[% c('input_files_by_name/nsis') %]
  export PATH="/var/tmp/dist/nsis/bin:$PATH"

  mv defines.nsh windows-installer/
  [% IF !c('var/testbuild') -%]
    supported_locales="[% tmpl(c('var/locales').join(' ')) %]"
    tar -xf "[% c('input_files_by_name/translation-base-browser') %]"
    python3 windows-installer/add-strings.py --enable-languages translation-base-browser $supported_locales >> windows-installer/languages.nsh
    [% IF c("var/mullvad-browser") -%]
      tar -xf "[% c('input_files_by_name/translation-mullvad-browser') %]"
      python3 windows-installer/add-strings.py translation-mullvad-browser $supported_locales >> windows-installer/languages.nsh
    [% END -%]
  [% END -%]
  mv windows-installer $distdir/windows-installer

  [% IF c('var/mullvad-browser') -%]
    pushd $distdir/windows-installer
    tar -xf $rootdir/[% c('input_files_by_name/firefox') %]/nsis-plugins.tar.[% c("compress_tar") %]
    # While Firefox re-uses the uninstaller for this, we cannot do it, because
    # we write the uninstaller from the installer.
    # Instead, we need to write an updated postupdate.exe also when updating.
    makensis postupdate.nsi
    mv postupdate.exe "${TB_STAGE_DIR}/Browser/"
    popd
  [% END -%]

  mv "${TB_STAGE_DIR}" "$distdir/windows-installer/[% c('var/Project_Name') %]"
  rmdir "$distdir/$PKG_DIR"
  mv $distdir/windows-installer "$distdir/$PKG_DIR"
[% END %]

[% IF c("var/macos_universal") -%]
  # unify.py requires lipo, so we need to add cctools in the PATH
  tar -C /var/tmp/dist -xf $rootdir/[% c('input_files_by_name/macosx-toolchain') %]
  export PATH="/var/tmp/dist/macosx-toolchain/cctools/bin:$PATH"
  firefox_src=/var/tmp/dist/firefox-src
  mkdir -p $firefox_src
  tar -C $firefox_src -xf $rootdir/[% c('input_files_by_name/src-firefox') %]
  firefox_src=$(echo $firefox_src/firefox-*)
  # Temporarily move noscript outside of $TBDIR to avoid error when running unify.py:
  #   File "/var/tmp/dist/firefox-src/firefox-69721baf14f0/python/mozbuild/mozpack/mozjar.py", line 452, in _getreader
  #     raise JarReaderError(
  # mozpack.mozjar.JarReaderError: Central directory and file header mismatch. Corrupted archive?
  #
  # See https://gitlab.torproject.org/tpo/applications/tor-browser-build/-/issues/40439#note_2838724
  mkdir $rootdir/extensions
  mv "$TBDIR/$EXTSPATH/"* $rootdir/extensions
  MOZ_AUTOMATION=1 $firefox_src/mach python3 $firefox_src/toolkit/mozapps/installer/unify.py "$TBDIR" "$TBDIR_AARCH64"
  mkdir -p "$TBDIR/$EXTSPATH"
  mv $rootdir/extensions/* "$TBDIR/$EXTSPATH/"
  rm -Rf "$TBDIR_AARCH64"
[% END -%]

[% IF c("var/updater_enabled") -%]
  cp $rootdir/[% c('input_files_by_name/firefox') %]/mar-tools-*.zip "$OUTDIR"/
[% END -%]

[% IF c("var/windows") -%]
  archive_ext=zip
[% ELSE -%]
  archive_ext=tar.xz
[% END -%]

debug_symbols="$rootdir/[% c('input_files_by_name/firefox') %]/browser-debug-symbols.$archive_ext"
if [[ -f "$debug_symbols" ]]; then
  cp "$debug_symbols"  "$OUTDIR/[% c('var/project-name') %]-debug-symbols-[% c('var/mar_osname') %]-[% c('var/torbrowser_version') %].$archive_ext"
fi

geckodriver="$rootdir/[% c('input_files_by_name/firefox') %]/geckodriver.$archive_ext"
if [[ -f "$geckodriver" ]]; then
  cp "$geckodriver" "$OUTDIR/geckodriver-[% c('var/osname') %]-[% c('var/torbrowser_version') %].$archive_ext"
fi
[% IF c("var/macos_universal") -%]
  geckodriver="$rootdir/[% c('input_files_by_name/firefox-aarch64') %]/geckodriver.$archive_ext"
  if [[ -f "$geckodriver" ]]; then
    cp "$geckodriver" "$OUTDIR/geckodriver-macos-aarch64-[% c('var/torbrowser_version') %].$archive_ext"
  fi
[% END -%]

[%IF c("var/tor-browser") -%]
  tor_expert_bundle_src="[% c("input_files_by_name/tor-expert-bundle") %]"
  # strip off trailing "$buildid.tar.gz"
  tor_expert_bundle_dest=${tor_expert_bundle_src:0:-7}.tar.gz
  cp $rootdir/[% c("input_files_by_name/tor-expert-bundle") %]/tor-expert-bundle.tar.gz "$OUTDIR"/$tor_expert_bundle_dest
  [% IF c("var/macos_universal") %]
    tor_expert_bundle_src="[% c('input_files_by_name/tor-expert-bundle-aarch64') %]"
    # strip off trailing "$buildid.tar.gz"
    tor_expert_bundle_dest=${tor_expert_bundle_src:0:-7}.tar.gz
    cp $rootdir/[% c('input_files_by_name/tor-expert-bundle-aarch64') %]/tor-expert-bundle.tar.gz "$OUTDIR"/$tor_expert_bundle_dest
  [% END -%]
[% END -%]
[% IF c("var/build_infos_json") -%]
  cp $rootdir/[% c('input_files_by_name/firefox') %]/build-infos.json "$OUTDIR"/build-infos-[% c("var/mar_osname") %].json
[% END -%]

[% IF c("var/updater_enabled") -%]
  pushd "$TBDIR[% IF c("var/macos") %]/Contents/Resources/[% END %]"
  rm -f precomplete
  python3 $MARTOOLS/createprecomplete.py
  popd
[% END -%]
cd $distdir

[% IF c("var/build_mar") && c("var/updater_enabled") -%]
  # Create full MAR file and compressed package.
  [% SET mar_file = c("var/project-name") _ '-' _ c("var/mar_osname") _ '-' _ c("var/torbrowser_version") _ '_ALL.mar' %]
  MAR=$MARTOOLS/mar \
  MOZ_PRODUCT_VERSION=[% c("var/torbrowser_version") %] \
  MAR_CHANNEL_ID=[% c("var/mar_channel_id") %] \
  $MARTOOLS/make_full_update.sh -q $OUTDIR/[% mar_file %] "$TBDIR"
[% END -%]

[% IF c("var/linux") %]
  [% IF c("var/tor-browser") %]
    # We need the old fontconfig files only when updating from old installations
    # that include it, but do not add it to new packages!
    # Remove once we do a watershed release.
    rm -rf "$TBDIR/TorBrowser/Data/fontconfig"
  [% END -%]
  [% c('tar', {
        tar_src => [ '$PKG_DIR' ],
        tar_args => '-cJf $OUTDIR/' _ c("var/project-name") _ '-' _ c("var/osname") _ '-' _ c("var/torbrowser_version") _ '.tar.xz',
    }) %]
[% ELSIF c("var/macos") %]
  [% c('var/ddmg', {
        dmg_src => '"$PKG_DIR"',
        dmg_out => '$OUTDIR/' _ c('var/project-name') _ '-' _ c("var/mar_osname") _ '-' _ c("var/torbrowser_version") _ '.dmg',
    }) %]
[% ELSIF c("var/windows") %]
  find "$PKG_DIR" -exec [% c("touch") %] {} \;
  pushd "$PKG_DIR"
  [% IF c('var/mullvad-browser') -%]
    makensis browser-install.nsi
    python3 $rootdir/pe_checksum_fix.py browser-install.exe
    mv browser-install.exe $OUTDIR/[% c("var/project-name") %]-[% c("var/osname") %]-[% c("var/torbrowser_version") %].exe
  [% ELSE -%]
    makensis browser-portable.nsi
    # Working around NSIS braindamage
    python3 $rootdir/pe_checksum_fix.py browser-portable.exe
    mv browser-portable.exe $OUTDIR/[% c("var/project-name") %]-[% c("var/osname") %]-portable-[% c("var/torbrowser_version") %].exe
  [% END -%]
  popd
[% END %]
