#!/bin/bash

set -e
DIR=/var/bos-docker
DOCKER_VERSION=$(docker --version)
COMPOSE_VERSION=$(docker-compose --version)

# Check for root privileges
#
if [ x$UID != x0 ]; then
    echo ""
    echo -e "Error: This installer must be run with root privileges. Exiting."
    echo ""
    exit 1
fi

# Confirm yes / no function
function confirm {
    while true; do
        read -p "$1 [Y/n] " yn
        case $yn in
            "") 
                break 
                ;;
            [Yy]* ) 
                break 
                ;;
            [Nn]* ) 
                echo ""
                echo "Ok. Exiting."
                echo ""
                exit 1
                ;;
            * ) echo "Please answer yes or no.";;
        esac
    done
}

#Check if docker is installed yet
if [ -z "$DOCKER_VERSION" ]; then
    confirm "docker isn't installed on this maschine
    Do you want to install docker now?"   
    echo "Installing docker"
    echo "Getting dependencies"
    apt-get update
    apt-get install -y \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
    echo "Getting docker's official GPG key"
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
    echo "Setting up stable release"
    echo \
    "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
    $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    apt-get update
    apt-get install -y docker-ce docker-ce-cli containerd.io
    DOCKER_VERSION=$(docker --version)
fi
echo "Installed docker version: $DOCKER_VERSION"

#Check if docker-compose is installed yet
if [ -z "$COMPOSE_VERSION" ]; then
    confirm "docker-compose isn't installed on this maschine
    Do you want to install docker now?"
    echo "Installing docker-compose"
    echo "download the current stable release of Docker Compose"
    curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    echo "Apply executable permissions to the binary"
    chmod +x /usr/local/bin/docker-compose
    COMPOSE_VERSION=$(docker-compose --version)
fi
echo "Installed docker-compose version: $COMPOSE_VERSION"

if [ -z "$(ls /var |grep bos-docker )" ]; then
    echo "$DIR doesn't exist. Creating $DIR"
    mkdir -p $DIR/apps
    mkdir -p $DIR/settings
    mkdir -p $DIR/logs
else
    echo "$DIR alrady exists. Moving $DIR to $DIR/bos-docker-template"
    mv $DIR $DIR-template
    echo "Creating new volume directories"
    mkdir -p $DIR/apps
    mkdir -p $DIR/settings
    mkdir -p $DIR/logs
fi

echo "Pulling bos-docker image"
curl https://bos-debian-mirror.brinkhaus-gmbh.de/docker/bos-docker-container-latest.tar -o $DIR/bos.tar
echo "Getting docker-compose.yml"

curl https://bos-debian-mirror.brinkhaus-gmbh.de/docker/docker-compose.yml -o $DIR/docker-compose.yml
echo "Checking for previous bos-docker-containers"
PREV_ID="docker image ls | grep bos | awk '{print $3}')"
if [ ! -z  ]; then
    echo "Removing previous bos-docker"
    docker image rm $PREV_ID
fi
echo "loading container"
docker load < $DIR/bos.tar
echo "imported bos_v$(docker image ls | grep bos | awk '{print $2}')"
echo "starting bos-docker-container"
docker-compose -f $DIR/docker-compose.yml up -d 
