#!/bin/bash

[% c("var/set_default_env") -%]

cat > l10n-changesets.json << 'EOF_CHANGESETS'
[% pc('firefox', 'var/l10n-changesets.json') %]
EOF_CHANGESETS

clone_dir='[% c("basedir") %]/hg_clones/[% project %]'
mkdir -p "$clone_dir"
cd "$clone_dir"
tmpdir=$(mktemp -d)

[% FOREACH lang = c('var/locales') %]
  [% SET lang = tmpl(lang);
     # mk is unavailable on mobile.
     NEXT IF lang == 'mk';
     SET hgurl = "https://hg.mozilla.org/l10n-central/" _ lang %]
  if test -d [% lang %]
  then
      cd [% lang %]
      [% c("hg") %] pull [% hgurl %]
  else
      [% c("hg") %] clone [% hgurl %] [% lang %]
      cd [% lang %]
  fi
  hg_hash=$([% c("basedir") %]/projects/firefox-locale-bundle/get_hg_hash \
                $rootdir/l10n-changesets.json \
                [% lang %])
  [% c("hg") %] archive -r "$hg_hash" -t files "$tmpdir"/[% lang %]
  cd ..
[% END %]

tar -C "$tmpdir" -czf [% dest_dir %]/[% c("filename") %] .
rm -Rf "$tmpdir"
