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

update_responses_tar_filename="update-responses-$tbb_version_type-$tbb_version.tar"
update_responses_tar="$script_dir/../../$SIGNING_PROJECTNAME/$tbb_version_type/update-responses/$update_responses_tar_filename"
test -f "$update_responses_tar" && \
  echo "warning: $update_responses_tar_filename already exists. It will be re-generated."
echo "Running 'make $SIGNING_PROJECTNAME-update_responses-$tbb_version_type'"
pushd "$script_dir/../.." > /dev/null
make $SIGNING_PROJECTNAME-update_responses-$tbb_version_type
popd > /dev/null

cd $update_responses_repository_dir
git checkout main
git pull --ff-only
test -n "$(git status --porcelain=v1 | grep -v '^?')" \
  && exit_error 'update_responses_repository_dir has modified files'
is_project torbrowser && cd update_3
is_project mullvadbrowser && cd update_1

old_ur=$(mktemp -d)
trap "rm -Rf $old_ur" EXIT
mv "$tbb_version_type" "$old_ur/$tbb_version_type"
tar -xf "$update_responses_tar"

# Keep download-*.json files from previous release if they are not in
# the new release. This happens when a release does not include some
# platforms. See tor-browser-build#41039.
IFS=$'\n'
for file in $(ls -1 "$old_ur/$tbb_version_type"/download-*.json)
do
  fname=$(basename "$file")
  test -f "$tbb_version_type/$fname" && continue
  mv "$file" "$tbb_version_type/$fname"
done

# Keep directory from previous release if they are not in the new release.
# This happens when a release does not include some platforms.
for file in $(ls -1 "$old_ur/$tbb_version_type")
do
  test -d "$old_ur/$tbb_version_type/$file" || continue
  test -d "$tbb_version_type/$file" && continue
  mv -f "$old_ur/$tbb_version_type/$file" "$tbb_version_type/$file"
done

# Commit each sub-directory separately
for file in $(ls -1 "$tbb_version_type")
do
  test -d "$tbb_version_type/$file" || continue
  git add "$tbb_version_type/$file"
  git add "$tbb_version_type/download-$file.json"
  git diff --quiet  --cached --exit-code || \
    git commit -m "$tbb_version_type: new version, $tbb_version ($file)"
done

platform_android=$(rbm_showconf_boolean var/browser_platforms/signing_android)
if [ -n "$platform_android" ]; then
  git add "$tbb_version_type"/download-android-*.json
  git diff --quiet  --cached --exit-code || \
    git commit -m "$tbb_version_type: new version, $tbb_version (android)"
fi

git add "$tbb_version_type"
git diff --quiet  --cached --exit-code || \
  git commit -m "$tbb_version_type: new version, $tbb_version"
git push

# we just need to push mullvadbrowser's update responses to git, not deploy to staticiforme
is_project mullvadbrowser && exit 0

update_responses_commit=$(git log -1 --format=%H)

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

if test "\$#" -gt 1; then
  echo >&2 "Wrong number of arguments"
  exit 2
fi

commit=$update_responses_commit
test "\$#" -eq 1 && commit="\$1"

test "\$#" -eq 0 && echo "Deploying version $tbb_version"
echo "update_responses_commit: \$commit"

cd "$update_dir"
git fetch
changed_files="\$(git diff --name-only HEAD \$commit)"
if echo "\$changed_files" | grep -qv "$tbb_version_type"
then
  echo >&2 "Error: checking out new update_response_commit will changes"
  echo >&2 "some files outside of the $tbb_version_type directory:"
  echo "\$changed_files" | grep -v "$tbb_version_type" >&2
  echo >&2 "--"
  echo >&2 "If this is really what you want to do, edit this script to"
  echo >&2 "remove the line 'exit 1' and run it again."
  echo >&2 "See tor-browser-build#41168 for more details."
  exit 1
fi
git checkout "\$commit"

static-update-component aus1.torproject.org
EOF

chmod +rx $deploy_script
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"
echo "  sudo -u tb-release ./deploy_update_responses-$tbb_version_type.sh"
