#!/usr/bin/env bash

set -eo pipefail

DOCKER_MACHINE_VERSION=${DOCKER_MACHINE_VERSION:-0.14.0}
DOCKER_MACHINE_CHECKSUM=${DOCKER_MACHINE_CHECKSUM:-a4c69bffb78d3cfe103b89dae61c3ea11cc2d1a91c4ff86e630c9ae88244db02}
DUMB_INIT_VERSION=${DUMB_INIT_VERSION:-1.0.2}
DUMB_INIT_CHECKSUM=${DUMB_INIT_CHECKSUM:-a8defac40aaca2ca0896c7c5adbc241af60c7c3df470c1a4c469a860bd805429}
GIT_LFS_VERSION=${GIT_LFS_VERSION:-2.7.1}
GIT_LFS_CHECKSUM=${GIT_LFS_CHECKSUM:-c8952ee72af214e3669f834d829e8a0a3becd160dead18237f99e40d75a3e920}

RUNNER_BINARY=${RUNNER_BINARY:-out/binaries/gitlab-runner-linux-amd64}

IS_LATEST=${IS_LATEST:-}

ref_tag=${CI_COMMIT_TAG}
if [[ -z "${ref_tag}" ]]; then
    ref_tag=${CI_COMMIT_REF_SLUG:-master}
fi

if [[ "${ref_tag}" = "master" ]]; then
    ref_tag=bleeding
fi

REVISION=${REVISION:-}
if [[ -z "${REVISION}" ]]; then
  REVISION=$(git rev-parse --short=8 HEAD || echo "unknown")
fi

_docker() {
    docker "${@}"
}

build() {
    echo -e "\033[1mBuilding image: \033[32m${1}\033[0m"
    _docker build \
        --no-cache \
        --build-arg DOCKER_MACHINE_VERSION="${DOCKER_MACHINE_VERSION}" \
        --build-arg DUMB_INIT_VERSION="${DUMB_INIT_VERSION}" \
        --build-arg GIT_LFS_VERSION="${GIT_LFS_VERSION}" \
        -t "${1}" \
        "${2}"
}

import() {
    echo -e "\033[1mImporting image: \033[32m${2}\033[0m"
    _docker import "${1}" "${2}"
}

tag() {
    echo -e "\033[1mTagging image: \033[32m${2}\033[0m"
    _docker tag "${1}" "${2}"
}

tag_latest() {
    if [[ -z "${IS_LATEST}" ]]; then
        return
    fi

    tag "${@}"
}

push() {
    echo -e "\033[1mPushing image: \033[32m${1}\033[0m"
    _docker push "${1}"
}

push_latest() {
    if [[ -z "${IS_LATEST}" ]]; then
        return
    fi

    push "${@}"
}

release_docker_helper_images() {
    helper_image_x86_64="gitlab/gitlab-runner-helper:x86_64-${REVISION}"
    helper_image_x86_64_latest="gitlab/gitlab-runner-helper:x86_64-latest"
    helper_image_arm="gitlab/gitlab-runner-helper:arm-${REVISION}"
    helper_image_arm_latest="gitlab/gitlab-runner-helper:arm-latest"
    helper_image_arm64="gitlab/gitlab-runner-helper:arm64-${REVISION}"
    helper_image_arm64_latest="gitlab/gitlab-runner-helper:arm64-latest"

    import out/helper-images/prebuilt-x86_64.tar.xz "${helper_image_x86_64}"
    import out/helper-images/prebuilt-arm.tar.xz "${helper_image_arm}"
    import out/helper-images/prebuilt-arm64.tar.xz "${helper_image_arm64}"

    tag_latest "${helper_image_x86_64}" "${helper_image_x86_64_latest}"
    tag_latest "${helper_image_arm}" "${helper_image_arm_latest}"
    tag_latest "${helper_image_arm64}" "${helper_image_arm64_latest}"

    push "${helper_image_x86_64}"
    push "${helper_image_arm}"
    push "${helper_image_arm64}"

    push_latest "${helper_image_x86_64_latest}"
    push_latest "${helper_image_arm_latest}"
    push_latest "${helper_image_arm64_latest}"
}

login() {
    echo "${2}" | _docker login --username "${1}" --password-stdin "${3}"
}

logout() {
    _docker logout "${1}"
}

tee dockerfiles/checksums <<EOF
${DOCKER_MACHINE_CHECKSUM}  /usr/bin/docker-machine
${DUMB_INIT_CHECKSUM}  /usr/bin/dumb-init
${GIT_LFS_CHECKSUM}  /usr/bin/git-lfs
EOF

cp out/deb/gitlab-runner_amd64.deb dockerfiles/ubuntu/gitlab-runner_amd64.deb
cp dockerfiles/checksums dockerfiles/ubuntu
cp "${RUNNER_BINARY}" dockerfiles/alpine/gitlab-runner-linux-amd64
cp dockerfiles/checksums dockerfiles/alpine

build "gitlab/gitlab-runner:ubuntu-${ref_tag}" dockerfiles/ubuntu
build "gitlab/gitlab-runner:alpine-${ref_tag}" dockerfiles/alpine

tag "gitlab/gitlab-runner:ubuntu-${ref_tag}" "gitlab/gitlab-runner:${ref_tag}"

tag_latest "gitlab/gitlab-runner:ubuntu-${ref_tag}" gitlab/gitlab-runner:ubuntu
tag_latest "gitlab/gitlab-runner:ubuntu-${ref_tag}" gitlab/gitlab-runner:latest
tag_latest "gitlab/gitlab-runner:alpine-${ref_tag}" gitlab/gitlab-runner:alpine

if [[ -z "${PUBLISH_IMAGES}" ]] || [[ "${PUBLISH_IMAGES}" != "true" ]]; then
    echo "Skipping images pushing"
    exit 0
fi

if [[ -n "${CI_REGISTRY}" ]] && [[ -n "${CI_REGISTRY_IMAGE}" ]]; then
    tag "gitlab/gitlab-runner:ubuntu-${ref_tag}" "${CI_REGISTRY_IMAGE}:ubuntu-${ref_tag}"
    tag "gitlab/gitlab-runner:alpine-${ref_tag}" "${CI_REGISTRY_IMAGE}:alpine-${ref_tag}"
    tag "gitlab/gitlab-runner:${ref_tag}" "${CI_REGISTRY_IMAGE}:${ref_tag}"

    tag_latest gitlab/gitlab-runner:ubuntu "${CI_REGISTRY_IMAGE}:ubuntu"
    tag_latest gitlab/gitlab-runner:latest "${CI_REGISTRY_IMAGE}:latest"
    tag_latest gitlab/gitlab-runner:alpine "${CI_REGISTRY_IMAGE}:alpine"

    if [[ -n "${CI_REGISTRY_USER}" ]] && [[ -n "${CI_REGISTRY_PASSWORD}" ]]; then
        login "${CI_REGISTRY_USER}" "${CI_REGISTRY_PASSWORD}" "${CI_REGISTRY}"

        push "${CI_REGISTRY_IMAGE}:ubuntu-${ref_tag}"
        push "${CI_REGISTRY_IMAGE}:alpine-${ref_tag}"
        push "${CI_REGISTRY_IMAGE}:${ref_tag}"

        push_latest "${CI_REGISTRY_IMAGE}:ubuntu"
        push_latest "${CI_REGISTRY_IMAGE}:latest"
        push_latest "${CI_REGISTRY_IMAGE}:alpine"

        logout "${CI_REGISTRY}"
    fi
fi

if [[ -z "${PUSH_TO_DOCKER_HUB}" ]] || [[ "${PUSH_TO_DOCKER_HUB}" != "true" ]]; then
    echo "Skipping push to Docker Hub"
    exit 0
fi

if [[ -n "${DOCKER_HUB_USER}" ]] && [[ -n "${DOCKER_HUB_PASSWORD}" ]]; then
    login "${DOCKER_HUB_USER}" "${DOCKER_HUB_PASSWORD}"

    push "gitlab/gitlab-runner:ubuntu-${ref_tag}"
    push "gitlab/gitlab-runner:alpine-${ref_tag}"
    push "gitlab/gitlab-runner:${ref_tag}"

    push_latest gitlab/gitlab-runner:ubuntu
    push_latest gitlab/gitlab-runner:latest
    push_latest gitlab/gitlab-runner:alpine

    release_docker_helper_images

    logout
fi
