#!/bin/sh
####### ######################################################################
# Blockbridge installer for the Blockbridge container
# - Checks for sudo and that docker is installed and running
# - runs the blockbridge/simulator-up container, which installs and runs the
#   Blockbridge storage container
##############################################################################
set -e

##############################################################################
# defines
##############################################################################
readonly CONTAINER_UP_IMAGE="blockbridge/simulator-up:4"

###########################################################
# trap handlers 
###########################################################
on_exit()
{
    res=$?

    echo

    if [ $res -ne 0 ]; then
        ERROR_CMD=$(eval echo "$BASH_COMMAND")
        if [ -n "$ERROR_CMD" ]; then
            echo "ERROR: command failed: $ERROR_CMD"
        fi
    fi

    return $res
}

trap on_exit EXIT

##############################################################################
# check that the command specified exists in the current path
##############################################################################
command_exists()
{
    command -v "$@" >/dev/null 2>&1
}

exit_sudo_needed()
{
    cat >&2 <<-'EOF'
Error: this installer needs the ability to run commands as root.
We are unable to find the "sudo" command to make this happen.
EOF
    exit 1
}

exit_docker_needed()
{
    cat >&2 <<-'EOF'
Error: this installer requires docker to be installed and running.
Install docker, make sure docker is running, or see
https://docs.docker.com/engine/installation/
EOF
    exit 1
}

# if root or can execute docker directly without permissions issue, then no
# sudo needed. otherwise, sudo is required.
set_shell_cmd()
{
    user="$(id -un 2>/dev/null || true)"
    sh_c=''
    if [ "$user" != 'root' ]; then
        if docker version >/dev/null 2>&1; then
            break
        elif command_exists sudo; then
            sh_c='sudo -E '
        else
            exit_sudo_needed
        fi
    fi
}

install_check()
{
    set_shell_cmd
    if ! command_exists docker; then
        exit_docker_needed
    elif ! $sh_c docker version >/dev/null 2>&1; then
        exit_docker_needed
    fi
}

install_container()
{
    $sh_c docker pull $REGISTRY${CONTAINER_UP_IMAGE}
    $sh_c docker run --rm -t -e REGISTRY -v /var/run/docker.sock:/var/run/docker.sock $REGISTRY${CONTAINER_UP_IMAGE}
}

wait_container()
{
    echo -n "Waiting for container to boot"
    i=0
    while [ $i -ne 60 ]; do
        if ! STARTED=$($sh_c docker exec bbsim-converged cat /tmp/boot.timestamp 2>/dev/null); then
            echo -n "."
            sleep 1
            i=$(($i+1))
            continue
        fi
        break;
    done

    echo

    if [ -z "$STARTED" ]; then
        echo "Timed out waiting for container to boot."
        exit 1
    fi
}

issue()
{
    $sh_c docker exec bbsim-converged cat /bb/etc/issue
}

check_args()
{
    export REGISTRY="$1"
}

up()
{
    check_args "$@"
    install_check
    install_container
    wait_container
    issue
    exit 0
}

up "$@"
