#!/usr/bin/env bash

set -Eeuo pipefail

# Variables for cloud "yc.wall-e.cloud" folder "yc.wall-e.main-folder" in production YC.
YC_STAGE_PROD="prod"
DOCKER_REGISTRY_URL_PROD="cr.yandex"
DOCKER_REPOSITORY_ID_PROD="crpvl8m4ln9v4lmob49h"
YC_PROFILE_NAME_PROD="prod-fed-user"

# Variables for cloud "yc.wall-e.cloud" folder "yc.wall-e.main-folder" in preprod YC.
YC_STAGE_PREPROD="preprod"
DOCKER_REGISTRY_URL_PREPROD="cr.cloud-preprod.yandex.net"
DOCKER_REPOSITORY_ID_PREPROD="crt77n6uhi53apofvno3"
YC_PROFILE_NAME_PREPROD="preprod-fed-user"

# Variables for cloud "yc.wall-e.cloud-testing" folder "yc.wall-e.main-testing-folder" in preprod YC.
YC_STAGE_TESTING="testing"
DOCKER_REGISTRY_URL_TESTING="cr.cloud-preprod.yandex.net"
DOCKER_REPOSITORY_ID_TESTING="crt3j5diek0bllejocj4"
YC_PROFILE_NAME_TESTING="preprod-fed-user"

# Manage CLI arguments.
USAGE="Usage: $(basename "$0") -s [${YC_STAGE_PROD} | ${YC_STAGE_PREPROD} | ${YC_STAGE_TESTING}] -i [walled-python | walled-go | walle-shell | webapp] -t some-docker-image-tag"

while getopts ":i:s:t:" opt; do
  case "$opt" in
    s) YC_STAGE=${OPTARG} ;;
    i) DOCKER_IMAGE=${OPTARG} ;;
    t) DOCKER_TAG=${OPTARG} ;;
    \?) echo "Invalid option -$OPTARG" >&2
    ;;
  esac
done

if [ "${YC_STAGE:-not-specified}" = "not-specified" ];
then
    echo "${USAGE}"
    exit 1
fi

if [ "${DOCKER_IMAGE:-not-specified}" = "not-specified" ];
then
    echo "${USAGE}"
    exit 1
fi

if [ "${DOCKER_IMAGE}" = "walled-python" ];
then
    PACKAGE_JSON_FILE_PATH="infra/walle/server/walled/build/package.json"
elif [ "${DOCKER_IMAGE}" = "walled-go" ];
then
    PACKAGE_JSON_FILE_PATH="infra/walle/server/go/build/package.json"
elif [ "${DOCKER_IMAGE}" = "walle-shell" ];
then
    PACKAGE_JSON_FILE_PATH="infra/walle/server/walle-shell/build/package.json"
elif [ "${DOCKER_IMAGE}" = "webapp" ];
then
    PACKAGE_JSON_FILE_PATH="infra/walle/webapp/ya-package-walle-webapp-yc.json"
else
    echo "${USAGE}"
    exit 1
fi

if [ "${DOCKER_TAG:-not-specified}" = "not-specified" ];
then
    echo "${USAGE}"
    exit 1
fi

# Set variables according to provided stage name.
if [ "${YC_STAGE}" = "${YC_STAGE_PROD}" ];
then
    DOCKER_REGISTRY_URL="${DOCKER_REGISTRY_URL_PROD}"
    DOCKER_REPOSITORY_ID="${DOCKER_REPOSITORY_ID_PROD}"
elif [ "${YC_STAGE}" = "${YC_STAGE_PREPROD}" ];
then
    DOCKER_REGISTRY_URL="${DOCKER_REGISTRY_URL_PREPROD}"
    DOCKER_REPOSITORY_ID="${DOCKER_REPOSITORY_ID_PREPROD}"
elif [ "${YC_STAGE}" = "${YC_STAGE_TESTING}" ];
then
    DOCKER_REGISTRY_URL="${DOCKER_REGISTRY_URL_TESTING}"
    DOCKER_REPOSITORY_ID="${DOCKER_REPOSITORY_ID_TESTING}"
else
    echo "ERROR: Unknown YC Stage \"${YC_STAGE}\"."
    echo "${USAGE}"
    exit 1
fi

# cd to arcadia root, ask for confirmation,
# authenticate in container registry, and run 'ya package --docker'.
(cd "${ARCADIA_ROOT}" || exit 1; \

echo
echo "package.json file path: ${PACKAGE_JSON_FILE_PATH}"
echo
echo "Docker registry:        ${DOCKER_REGISTRY_URL}"
echo "Docker repository:      ${DOCKER_REPOSITORY_ID}"
echo "Docker image tag:       ${DOCKER_TAG}"
echo

read -p "Press 'Y' or 'y' to build and upload." -n 1 -r
echo

if [[ $REPLY =~ ^[Yy]$ ]]
then
    echo "* Authenticating..."
    if [ "${YC_STAGE}" = "${YC_STAGE_PROD}" ];
    then
        yc container registry configure-docker --profile "${YC_PROFILE_NAME_PROD}"
    elif [ "${YC_STAGE}" = "${YC_STAGE_PREPROD}" ];
    then
        yc iam create-token --profile "${YC_PROFILE_NAME_PREPROD}" | \
            docker login --username iam --password-stdin "${DOCKER_REGISTRY_URL_PREPROD}"
    elif [ "${YC_STAGE}" = "${YC_STAGE_TESTING}" ];
        then
            yc iam create-token --profile "${YC_PROFILE_NAME_TESTING}" | \
                docker login --username iam --password-stdin "${DOCKER_REGISTRY_URL_TESTING}"
    fi
    echo "* Authenticated"
    echo

    ya package --docker \
        --docker-registry "${DOCKER_REGISTRY_URL}" \
        --docker-repository "${DOCKER_REPOSITORY_ID}" \
        --docker-push \
        --custom-version "${DOCKER_TAG}" \
        "${PACKAGE_JSON_FILE_PATH}"
fi
)
