#!/bin/sh
[% c("var/set_default_env") -%]
# reset HOME which was changed by var/set_default_env, for gpg
[% IF ENV.HOME %]export HOME="[% ENV.HOME %]"[% END %]
destdir="[% dest_dir _ '/' _ c("var/publish_dir") %]"
mkdir -p "$destdir"
[% IF c("var/browser_platforms/android-armv7") -%]
  mv [% c('input_files_by_name/android-armv7') %]/* "$destdir"/
[% END -%]
[% IF c("var/browser_platforms/android-x86") -%]
  mv [% c('input_files_by_name/android-x86') %]/* "$destdir"/
[% END -%]
[% IF c("var/browser_platforms/android-x86_64") -%]
  mv [% c('input_files_by_name/android-x86_64') %]/* "$destdir"/
[% END -%]
[% IF c("var/browser_platforms/android-aarch64") -%]
  mv [% c('input_files_by_name/android-aarch64') %]/* "$destdir"/
[% END -%]
[% IF c("var/browser_platforms/windows-i686") -%]
  mv [% c('input_files_by_name/windows-i686') %]/* "$destdir"/
[% END -%]
[% IF c("var/browser_platforms/windows-x86_64") -%]
  mv [% c('input_files_by_name/windows-x86_64') %]/* "$destdir"/
[% END -%]
[% IF c("var/browser_platforms/macos") -%]
  mv [% c('input_files_by_name/macos') %]/* "$destdir"/
[% END -%]
[% IF c("var/browser_platforms/macos-x86_64") -%]
  mv [% c('input_files_by_name/macos-x86_64') %]/* "$destdir"/
[% END -%]
[% IF c("var/browser_platforms/macos-aarch64") -%]
  mv [% c('input_files_by_name/macos-aarch64') %]/* "$destdir"/
[% END -%]
[% IF c("var/browser_platforms/linux-i686") -%]
  mv [% c('input_files_by_name/linux-i686') %]/* "$destdir"/
[% END -%]
[% IF c("var/browser_platforms/linux-x86_64") -%]
  mv [% c('input_files_by_name/linux-x86_64') %]/* "$destdir"/
[% END -%]
[% IF c("var/browser_platforms/linux-aarch64") -%]
  mv [% c('input_files_by_name/linux-aarch64') %]/* "$destdir"/
[% END -%]
[% IF c("var/linux-packages") || c("var/linux-packages-aarch64") -%]
  [% IF c("var/linux-packages") -%]
    mv [% c('input_files_by_name/deb-packages') %]/* "$destdir"/
  [% END -%]
  [% IF c("var/linux-packages-aarch64") -%]
    mv [% c('input_files_by_name/deb-packages-aarch64') %]/* "$destdir"/
  [% END -%]
  mv [% c('input_files_by_name/rpm-packages') %]/* "$destdir"/
[% END -%]
[% IF c("var/browser-src") -%]
  mv  [% c('input_files_by_name/src-firefox') %]      \
      "$destdir"/
[% END -%]
cd "$destdir"
cat > .htaccess <<'EOF'
RewriteEngine On
RewriteRule ^sha256sums.txt$ sha256sums-unsigned-build.txt
RewriteRule ^sha256sums.txt.asc$ sha256sums-unsigned-build.txt.asc
RewriteRule ^sha256sums.incrementals.txt$ sha256sums-unsigned-build.incrementals.txt
RewriteRule ^sha256sums.incrementals.txt.asc$ sha256sums-unsigned-build.incrementals.txt.asc
[% IF c("var/tor-browser") -%]
RewriteRule ^tor-browser-linux64-(.*)_ALL.tar.xz.asc$ tor-browser-linux-x86_64-$1.tar.xz.asc
RewriteRule ^tor-browser-linux64-(.*)_ALL.tar.xz$ tor-browser-linux-x86_64-$1.tar.xz
RewriteRule ^tor-browser-linux32-(.*)_ALL.tar.xz.asc$ tor-browser-linux-i686-$1.tar.xz.asc
RewriteRule ^tor-browser-linux32-(.*)_ALL.tar.xz$ tor-browser-linux-i686-$1.tar.xz
[% END -%]
EOF

# empty any existing sh256sums file
echo -n > sha256sums-unsigned-build.txt
# concat sha256sum entry for each file in set
for i in $(ls -1 *.exe *.tar.xz *.dmg *.mar *.zip *.tar.gz *.apk *.bspatch *.json *.deb *.rpm | grep -v '\.incremental\.mar$' | sort)
do
  sha256sum $i >> sha256sums-unsigned-build.txt
done

[% IF c("var/sign_build") -%]
  gpg -abs [% c("var/sign_build_gpg_opts") %] sha256sums-unsigned-build.txt
[% END -%]
cat sha256sums-unsigned-build.txt
