#!/bin/bash
SCRIPT_DIR=$(cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd)
TBB_DIR="$SCRIPT_DIR/../.."
function showconf {
   "$TBB_DIR/rbm/rbm" showconf $@
}

# we may want to add a suffix to the release tag, e.g. rc1
mbrtag_suffix="$1"
token_var="buildconf/devmole_auth_token"
github_token=$(showconf release $token_var 2>/dev/null)
if ! [[ $github_token ]]; then
  echo >&2 "Github token ($token_var) missing!"
  exit 1
fi

set -e

GL_REMOTE_TOR=git@gitlab.torproject.org:tpo/applications/tor-browser.git
GL_REMOTE_MULLVAD=git@gitlab.torproject.org:tpo/applications/mullvad-browser.git
GH_REMOTE=git@github.com:mullvad/mullvad-browser.git
CLONE_DIR="$TBB_DIR/git_clones/firefox"
CHANNEL=$(showconf release var/browser_default_channel)
TARGET="--target $CHANNEL --target mullvadbrowser-linux-x86_64"
MB_TAG=$(showconf firefox git_hash $TARGET)
MB_BRANCH=${MB_TAG//-build[0-9]*/}
BB_BRANCH=${MB_BRANCH//mullvad-/base-}
FX_VERSION=$(showconf firefox var/firefox_version $TARGET)
BASE_VERSION=$(showconf release var/torbrowser_version $TARGET)
RELEASE_VERSION="${BASE_VERSION}${mbrtag_suffix}"
RELEASE_URL="https://dist.torproject.org/mullvadbrowser/${RELEASE_VERSION}/"
if ! curl -f "$RELEASE_URL" >/dev/null 2>&1 ; then
  echo >&2 "$RELEASE_URL not found! Maybe you need to run:"
  echo >&2 "ssh staticiforme.torproject.org 'static-update-component dist.torproject.org' && $0"
  exit 2
fi
pushd >/dev/null ../../git_clones/firefox
echo "Fetching $MB_BRANCH and tags..."
git fetch --tags "$GL_REMOTE_MULLVAD" "$MB_BRANCH"
git fetch --tags "$GL_REMOTE_TOR"
BB_TAG=$(git tag -l "$BB_BRANCH-build*" | tail -n1)
FX_TAG=$(git tag -l "FIREFOX_*${FX_VERSION//\./_}*" "FIREFOX_NIGHTLY_${FX_VERSION//\.0a[0-9]*/}*" | tail -n1)
echo "QA build version: $BASE_VERSION"
echo "Firefox version: $FX_VERSION"
echo "MB tag: $MB_TAG"
echo "BB tag: $BB_TAG"
echo "FX tag: $FX_TAG"
echo "Mullvad release tag: $RELEASE_VERSION"
read -p "Do you want to tag mullvad $RELEASE_VERSION & push tags/branches/QA build to ${GH_REMOTE}? (y/N) " -n 1 -r
echo
if ! [[ $REPLY =~ ^[Yy]$ ]]; then
   echo >&2 "Operation cancelled"
   exit 1
fi

git checkout $MB_TAG
[[ $(git tag -l $RELEASE_VERSION) ]] || git tag -s $RELEASE_VERSION -m"$FX_VERSION-based $RELEASE_VERSION"
git tag -n1 $RELEASE_VERSION
git push "$GH_REMOTE" "HEAD:refs/heads/$MB_BRANCH"
git push "$GH_REMOTE" "$FX_TAG"
git push "$GH_REMOTE" "$BB_TAG"
git push "$GH_REMOTE" "$RELEASE_VERSION"

popd >/dev/null

# Call Mullvad's github workflow for new releases (mullvad-browser#466)

version=$BASE_VERSION
response=$(curl -L \
  -X POST \
  -H "Accept: application/vnd.github+json" \
  -H "Authorization: Bearer $github_token" \
  -H "X-GitHub-Api-Version: 2022-11-28" \
  "https://api.github.com/repos/mullvad/mullvad-browser/actions/workflows/new_browser_release.yml/dispatches" \
  -d "{\"ref\":\"main\",\"inputs\":{\"version\":\"$version\"}}")

if ! [ $response ]; then
  echo "Build $BASE_VERSION submitted."
else
  echo >&2 "Something went wrong with build submission:"
  echo >&2 $response
  exit 3
fi
