#!/bin/bash
# Deploys the site to the production environment.
set -e

QUIET="false"
FORCE="false"
SKIP_ISSUE="false"

while getopts ':qfbh' flag; do
  case "${flag}" in
    q) QUIET="true" ;;
    f) FORCE="true" ;;
    b) SKIP_ISSUE="true" ;;
    h) echo "Usage: $0 [-q] [-f] [-b]"; exit 0 ;;
    *) ;;
  esac
done

# Verify we are on the main branch (or detached HEAD on main in CI)
if [[ "$FORCE" != "true" ]]; then
  BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "HEAD")
  if [ "$BRANCH" != "main" ]; then
    if ! git merge-base --is-ancestor HEAD main 2>/dev/null && ! git merge-base --is-ancestor HEAD origin/main 2>/dev/null; then
      echo "Not on the main branch. Aborting deployment."
      exit 1
    fi
  fi
fi

# Check for uncommitted changes
if [ -n "$(git status --porcelain)" ]; then
  echo "Uncommitted changes detected:"
  git status
  if [[ "$QUIET" != "true" ]]; then
    read -p "Do you want to proceed with the deployment? (y/n) " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
      echo "Deployment aborted."
      exit 1
    fi
  fi
fi

# Get the directory of the script
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# Create the GitHub issue for this deployment if it doesn't already exist.
if [[ "$SKIP_ISSUE" != "true" ]]; then
  "$DIR/create-deployment-issue" "$@"
fi

export APP_NAME="cr-status"
export APP_YAML="app.yaml"
export NOTIFIER_YAML="notifier.yaml"

"$DIR/deploy-env" "$@"

SERVICES="default notifier"
VERSION_URL=$(gcloud app --project="$APP_NAME" versions list --sort-by=~last_deployed_time --filter='service=default' --limit=1 --format=json | jq -r '.[] | .version.versionUrl')
LATEST_VERSION=$(gcloud app --project="$APP_NAME" versions list --sort-by=~last_deployed_time --filter='service=default' --limit=1 --format=json | jq -r '.[] | .id')

echo -e "\nDeployment complete. Validating..."
echo "Visit $VERSION_URL to confirm that everything works."

if [[ "$QUIET" != "true" ]]; then
  read -p "Press 'y' to redirect traffic to version $LATEST_VERSION for all services. (y/n) " -n 1 -r
  echo
fi

if [[ "$QUIET" == "true" || $REPLY =~ ^[Yy]$ ]]; then
  for SERVICE in $SERVICES
  do
    gcloud app --project="$APP_NAME" services set-traffic $SERVICE --splits $LATEST_VERSION=1 --quiet
  done
  echo "Traffic migrated."
else
  echo "Traffic migration skipped. Don't forget to migrate traffic manually:"
  echo "- 'default' service: https://console.cloud.google.com/appengine/versions?serviceId=default&project=$APP_NAME"
  echo "- 'notifier' service: https://console.cloud.google.com/appengine/versions?serviceId=notifier&project=$APP_NAME"
fi

# Update and close deployment bug.
REPO="GoogleChrome/chromium-dashboard"
LAST_DEPLOYMENT_ISSUE=$(gh issue list --state open --label "release" --repo "$REPO" --limit 1 --json number --jq '.[] | .number')

if [ -n "$LAST_DEPLOYMENT_ISSUE" ]; then
  gh issue close "$LAST_DEPLOYMENT_ISSUE" -c "Deployment is now complete." -R "$REPO"
  echo "Closed GitHub release issue #$LAST_DEPLOYMENT_ISSUE."
else
  echo "Could not find an open release issue to close."
fi
