branding_dir=[% IF c('var/android') %]mobile/android[% ELSE %]browser[% END %]/branding/[% c("var/project_initials") %]-[% c("var/channel") %]

[% IF c("var/has_l10n") -%]
  [% IF !c("var/android") -%]
    supported_locales="[% tmpl(c('var/locales').join(' ')) %]"
  [% ELSE -%]
    supported_locales="[% tmpl(c('var/locales_mobile').join(' ')).replace('-r', '-').replace('in', 'id').replace('iw', 'he') %]"
  [% END %]

  l10ncentral="$HOME/.mozbuild/l10n-central"
  mkdir "$l10ncentral"
  [% IF c('input_files_by_name/firefox-l10n') -%]
    for tarball in $rootdir/[% c('input_files_by_name/firefox-l10n') %]/*; do
      tar -C "$l10ncentral" -xf "$tarball"
    done
  [% END -%]

  [% IF c('input_files_by_name/translation-base-browser') -%]
    tar -C "$rootdir" -xf "$rootdir/[% c('input_files_by_name/translation-base-browser') %]"
    pushd "$rootdir/translation-base-browser"
    ln -s ja ja-JP-mac
    for lang in $supported_locales; do
      mv $lang/base-browser.ftl "$l10ncentral/$lang/toolkit/toolkit/global/"
    done
    popd
  [% END -%]

  [% IF c('input_files_by_name/translation-tor-browser') -%]
    tar -C "$rootdir" -xf "$rootdir/[% c('input_files_by_name/translation-tor-browser') %]"

    [%#
    # For the purpose of Weblate, all releases share a single brand.ftl and
    # brand.properties file per locale in the translations repository.
    # See tor-browser-build#41372.
    # In brand.ftl, both `-brand-short-name` and `-brand-full-name` should
    # differ between releases. As such, they have additional entries in the
    # translations repository file (Weblate):
    #   -brand-short-name for the stable release.
    #   -brand-short-name_alpha for the alpha release.
    #   -brand-short-name_nightly for the nightly release.
    # And similarly for -brand-full-name.
    # For the final build, we only want to keep the string that matches the
    # built release, and remove its suffix if it has one. So for the stable
    # release we want to keep -brand-short-name. For the alpha release we want
    # to keep -brand-short-name_alpha instead, and rename it to be
    # -brand-short-name.
    #
    # As such, we parse the brand.ftl file to rename these strings to keep the
    # version we want using rename-branding-strings.py.
    #
    # We do a similar thing with brandShortName and brandFullName in
    # brand.properties.
    -%]

    # For the stable release, the suffix is empty.
    # I.e. we want to select `-brand-short-name` directly.
    [% IF !c("var/release"); branding_string_suffix = '_' _ c('var/channel'); END -%]

    # Instructions for the script to perform the renames.
    brand_ftl_renames='{
      "suffix": "[% branding_string_suffix %]",
      "ids": ["-brand-short-name", "-brand-full-name"]
    }'
    brand_properties_renames='{
      "suffix": "[% branding_string_suffix %]",
      "ids": ["brandShortName", "brandFullName"]
    }'

    pushd "$rootdir/translation-tor-browser"
    ln -s ja ja-JP-mac
    for lang in $supported_locales; do
      mv $lang/tor-browser.ftl "$l10ncentral/$lang/toolkit/toolkit/global/"
      # Branding.
      l10n_branding_dir="$l10ncentral/$lang/$branding_dir/"
      mkdir -p "$l10n_branding_dir"
      # Convert the translations repository branding files into files that work
      # for this specific build.
      python3 $rootdir/rename-branding-strings.py $lang/branding/brand.ftl "$brand_ftl_renames" > "$l10n_branding_dir/brand.ftl"
      python3 $rootdir/rename-branding-strings.py $lang/brand.properties "$brand_properties_renames" > "$l10n_branding_dir/brand.properties"
    done
    popd

    [% IF !c('var/android') -%]
      # torbutton properties files.
      # TODO: Remove once we no longer have torbutton locale files.
      torbutton_locales="toolkit/torbutton/chrome/locale/"
      torbutton_jar="toolkit/torbutton/jar.mn"
      for lang in $supported_locales; do
        mkdir -p "$torbutton_locales/$lang"
        mv "$rootdir/translation-tor-browser/$lang"/*.properties "$torbutton_locales/$lang/"
        echo "% locale torbutton $lang %locale/$lang/" >> "$torbutton_jar"
        echo "    locale/$lang (chrome/locale/$lang/*)" >> "$torbutton_jar"
      done
    [% END -%]
  [% ELSIF c('input_files_by_name/translation-mullvad-browser') -%]
    tar -C "$rootdir" -xf "$rootdir/[% c('input_files_by_name/translation-mullvad-browser') %]"
    pushd "$rootdir/translation-mullvad-browser"
    ln -s ja ja-JP-mac
    for lang in $supported_locales; do
      cp -Lr $lang "$l10ncentral/"
    done
    popd
  [% END -%]
[% ELSE -%]
  supported_locales=""
[% END -%]
