#!/bin/bash -e

# Copyright 2016 The Rook Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "${scriptdir}/common.sh"

check_git

if [ ! -z $DOCKER_HOST ]; then
    echo ERROR: we only support the case where docker is running locally for now.
    return 1
fi

# if the container does not exist the pull or build it
if [[ ! $(docker images -q ${container_image}) ]]; then
    echo ==== Pulling ${container_image}
    docker pull ${container_image}
fi

# run the container with the same uid and gid of the user to avoid
# permissions issues when sharing volumes with the host
USER_ARGS="-e BUILDER_UID=$( id -u ) -e BUILDER_GID=$( id -g )"
BUILDER_HOME=/home/rook

# mount arguments
MOUNT_OPTS="\
    -v ${scriptdir}/../bin:${BUILDER_HOME}/go/src/${source_repo}/bin \
    -v ${scriptdir}/../release:${BUILDER_HOME}/go/src/${source_repo}/release \
"

if [ "`uname -s`" != "Linux" ]; then

    # On non-linux hosts, its is EXTREMELY slow to mount the source
    # tree inside the container. As such we use rsync to copy the source
    # tree to a new docker volumer on each run. The first rsync take a
    # little bit of time, but subsequent ones go by fast. This approach
    # is MUCH faster than using osxfs, NFS or other approaches.

    # On non-linux the layout is as follows:
    #
    #  /var/run/docker.sock (bind mounted to host /var/run/docker.sock)
    #  /${BUILDER_HOME}
    #    /.netrc (bind mounted to host ${HOME}/.netrc)
    #    /.docker/config.json (bind mounted to host ${HOME}/.docker/config.json)
    #    /go (bind mounted to a docker volume)
    #       /.work
    #       /.ccache
    #       /src/github.com/rook/rook (rsync'd from host <source>)
    #

    # create a new volume to hold our go workspace. NOTE: while concurrent
    # runs of the build container are supported they will share the same volume
    # and we will be rsyncing to it at different times. This could lead to
    # undefined behavior but this should be a rare case on non-linux envs.
    if [[ ! $(docker volume ls | grep ${container_volume}) ]]; then
        echo ==== Creating docker volume "${container_volume}" and syncing sources
        echo ==== for first time. This could take a few seconds.
        docker volume create --name ${container_volume} &> /dev/null
    fi

    # now copy the source tree to the container volume
    run_rsync "${scriptdir}/..-->rsync://localhost:${rsync_port}/volume/src/${source_repo}"

    MOUNT_OPTS="${MOUNT_OPTS} \
        -v ${container_volume}:${BUILDER_HOME}/go"

    CCACHE_ARGS="-e CCACHE_DIR=${BUILDER_HOME}/go/.ccache -e CCACHE_LOG"
else

    # On linux bind mounts are cheap so we dont need to rsync
    # the layout is as follows:
    #
    #  /var/run/docker.sock (bind mounted to host /var/run/docker.sock)
    #  /${BUILDER_HOME}
    #    /.netrc (bind mounted to host ${HOME}/.netrc)
    #    /.docker/config.json (bind mounted to host ${HOME}/.docker/config.json)
    #    /go
    #       /.work  (bind mounted to host <source>/.work/cross)
    #       /.ccache (bind mounted to host CCACHE_DIR)
    #       /pkg     (bind mounted to host <source>/.work/cross/pkg)
    #       /src/github.com/rook/rook (bind mounted to <source>)
    #
    MOUNT_OPTS="${MOUNT_OPTS} \
        -v ${scriptdir}/../.work/cross:${BUILDER_HOME}/go/.work \
        -v ${scriptdir}/../.work/cross/pkg:${BUILDER_HOME}/go/pkg \
        -v ${scriptdir}/..:${BUILDER_HOME}/go/src/${source_repo}"

    if [[ -d "${CCACHE_DIR}" ]]; then
        CCACHE_ARGS="-e CCACHE_DIR=${BUILDER_HOME}/go/.ccache -e CCACHE_LOG \
            -v ${CCACHE_DIR}:${BUILDER_HOME}/go/.ccache"
    fi
fi

# we copy credential files for github access
[[ -f ${HOME}/.netrc ]] && NETRC_ARGS="-v ${HOME}/.netrc:${BUILDER_HOME}/.netrc"

tty -s && TTY_ARGS=-ti || TTY_ARGS=

# docker seems more reliable when running with net=host.
if [ -z "${DISABLE_HOST_NETWORK}" ]; then
    NET_ARGS="--net=host"
fi

# sometimes we run docker inside docker. bind the docker config and socket
if [ -z "${DISABLE_NESTED_DOCKER}" ]; then
    DOCKER_NESTED_ARGS="\
    -v ${HOME}/.docker/config.json:${BUILDER_HOME}/.docker/config.json \
    -v /var/run/docker.sock:/var/run/docker.sock"
fi

docker run \
    --rm \
    -e GOPATH="${BUILDER_HOME}/go" \
    -e WORKDIR="${BUILDER_HOME}/go/.work" \
    -e GO_PKG_DIR="" \
    -e GITHUB_TOKEN \
    -e VERSION \
    -e LINKMODE=static \
    ${TTY_ARGS} \
    ${CCACHE_ARGS} \
    ${NETRC_ARGS} \
    ${USER_ARGS} \
    ${DOCKER_NESTED_ARGS} \
    ${NET_ARGS} \
    ${MOUNT_OPTS} \
    -w ${BUILDER_HOME}/go/src/${source_repo} \
    ${container_image} \
    "$@"


# copy some folders back to the source. We avoid a bind mount here
# due to https://github.com/rook/rook/issues/93
if [ "`uname -s`" != "Linux" ]; then

    run_rsync \
        "rsync://localhost:${rsync_port}/volume/src/${source_repo}/vendor-->${scriptdir}/.." \
        "rsync://localhost:${rsync_port}/volume/src/${source_repo}/tools-->${scriptdir}/.."

fi
