#!/bin/bash
set -e
script_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
source "$script_dir/functions"

check_torbrowser_version_var

update_responses_tar_filename="update-responses-$tbb_version_type-$tbb_version.tar"
update_responses_tar="$script_dir/../../$tbb_version_type/update-responses/$update_responses_tar_filename"
if test -f "$update_responses_tar"
then
  echo "$update_responses_tar_filename already exists: not running 'make update_responses-$tbb_version_type'"
else
  echo "Running 'make update_responses-$tbb_version_type'"
  pushd "$script_dir/../.." > /dev/null
  make update_responses-$tbb_version_type
  popd > /dev/null
fi

update_dir=/srv/aus1-master.torproject.org/htdocs/torbrowser/update_3
deploy_script=$(mktemp)
trap "rm -Rf $deploy_script" EXIT
cat << EOF > "$deploy_script"
#!/bin/bash
set -e

tmpdir="\$(mktemp -d)"

trap "rm -Rf \$tmpdir" EXIT

rm -Rf "$update_dir/$tbb_version_type.old"
test -d "$update_dir/$tbb_version_type" && \\
  mv -v "$update_dir/$tbb_version_type" "$update_dir/$tbb_version_type.old"

tar -C "\$tmpdir" -xf ~/$update_responses_tar_filename
chmod 775 "\$tmpdir"/$tbb_version_type
chmod 664 "\$tmpdir"/$tbb_version_type/* "\$tmpdir"/$tbb_version_type/.htaccess
chgrp -R torwww "\$tmpdir"/$tbb_version_type
mv -v "\$tmpdir"/$tbb_version_type "$update_dir/$tbb_version_type"

static-update-component aus1.torproject.org
EOF

chmod +x $deploy_script
scp -p "$update_responses_tar" "$ssh_host_staticiforme:"
scp -p $deploy_script $ssh_host_staticiforme:deploy_update_responses-$tbb_version_type.sh

echo 'To enable updates you can now run:'
echo "  ssh $ssh_host_staticiforme ./deploy_update_responses-$tbb_version_type.sh"
