#!/bin/bash

    << CHANGELOG
    2021-03-08: Initial Creation & Add to GIT
    2024-01-17: Adapt support for Docker installations for Debian 11/12
                Remove manual installation for docker-compose for Debian 11/12
                BugFix - Wrong position for if...elif then statement
CHANGELOG


if [[ "$EUID" -ne 0 ]]; then
	echo "This installer needs to be run with superuser privileges."
	exit
fi




echo -e "
$(tput setaf 3; tput setab 0)
    >> # [Docker-01] - Basic Dependencies (apt upgrade)
$(tput sgr 0)
"

apt -qq update




echo -e "
$(tput setaf 3; tput setab 0)
    >> # [Docker-02] - Install Docker
$(tput sgr 0)
"

# Import variables to determine version of Debian
source /etc/os-release
# if [[ "$VERSION_ID" == 9 ]]
if [[ "$VERSION_ID" -le 10 ]]
then
    sudo apt -qq update && \
    sudo apt -qq install -y apt-transport-https ca-certificates curl gnupg2 software-properties-common && \
    (curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -) && \
    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable" && \
    sudo apt -qq update && \ 
    sudo apt -qq install -y docker-ce docker-ce-cli containerd.io && \
    echo -e "$(tput setaf 7; tput setab 0)" && \
    echo "Running test docker container..." && \
    echo -e "$(tput sgr 0)" && \
    sudo docker run hello-world && \
    echo -e "$(tput setaf 7; tput setab 0)" && \
    echo "Docker Version: " && \
    docker version --format '{{.Server.Version}}' && \
    echo -e "$(tput sgr 0)"
elif [[ $VERSION_ID -ge 11 ]]
then
    ## Source - https://docs.docker.com/engine/install/debian/#install-using-the-repository
    # 1) Set up Docker's apt repository.

    # Add Docker's official GPG key:
    # Add the repository to Apt sources:
    # 2) Install the Docker packages
    # 3) Verify installation successful
    
    
    sudo apt -qq update && \
    sudo apt -qq install -y ca-certificates curl gnupg && \
    sudo install -m 0755 -d /etc/apt/keyrings && \
    (curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg) && \
    sudo chmod a+r /etc/apt/keyrings/docker.gpg && \
    (echo \
      "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \
      $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
    sudo tee /etc/apt/sources.list.d/docker.list > /dev/null) && \
    sudo apt -qq update && \
    sudo apt -qq install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin && \
    echo -e "$(tput setaf 7; tput setab 0)" && \
    echo "Running test docker container..." && \
    echo -e "$(tput sgr 0)" && \
    sudo docker run hello-world && \
    echo -e "$(tput setaf 7; tput setab 0)" && \
    echo "Docker Version: " && \
    docker version --format '{{.Server.Version}}' && \
    echo -e "$(tput sgr 0)"
else
    echo -e "$(tput setaf 1; tput setab 0)ERR: Unsupported version of debian! Skipping docker installation...$(tput sgr 0)"
    : '
    Install from a package
    If you cannot use Docker apt repository to install Docker Engine, you can download the deb file for your release and install it manually. You need to download a new file each time you want to upgrade Docker Engine.

    Go to https://download.docker.com/linux/debian/dists/.

    Select your Debian version in the list.

    Go to pool/stable/ and select the applicable architecture (amd64, armhf, arm64, or s390x).

    Download the following deb files for the Docker Engine, CLI, containerd, and Docker Compose packages:

        containerd.io_<version>_<arch>.deb
        docker-ce_<version>_<arch>.deb
        docker-ce-cli_<version>_<arch>.deb
        docker-buildx-plugin_<version>_<arch>.deb
        docker-compose-plugin_<version>_<arch>.deb
    Install the .deb packages. Update the paths in the following example to where you downloaded the Docker packages.

     sudo dpkg -i ./containerd.io_<version>_<arch>.deb \
      ./docker-ce_<version>_<arch>.deb \
      ./docker-ce-cli_<version>_<arch>.deb \
      ./docker-buildx-plugin_<version>_<arch>.deb \
      ./docker-compose-plugin_<version>_<arch>.deb
    The Docker daemon starts automatically.

    Verify that the Docker Engine installation is successful by running the hello-world image:

        sudo service docker start
        sudo docker run hello-world
    This command downloads a test image and runs it in a container. When the container runs, it prints a confirmation message and exits.
    '
fi



echo -e "
$(tput setaf 3; tput setab 0)
    >> # [Docker-03] - Start Docker automatically on boot
$(tput sgr 0)
"

sudo systemctl enable docker




echo -e "
$(tput setaf 3; tput setab 0)
    >> # [Docker-04] - Enable running Docker commands without sudo
$(tput sgr 0)
"
    ## 'head -1' is to select only first result.
    ## '-F:' means split on ':' character

NewGroup=docker
Acct=$(getent passwd {1000..60000} | head -1 | awk  -F: '{ print $1 }') && echo "Account: ${Acct}" && \
usermod -aG ${NewGroup} ${Acct} && \
echo "Found group '${NewGroup}' for user '${Acct}': $((sudo -u ${Acct} id -nG) | grep -c "${NewGroup}")"




echo -e "
$(tput setaf 3; tput setab 0)
    >> # [Docker-05] - Install Docker-Compose
$(tput sgr 0)
"

# Import variables to determine version of Debian
source /etc/os-release
# if [[ "$VERSION_ID" == 9 ]]
if [[ "$VERSION_ID" -le 10 ]]
then
    # [13] - Install Docker-Compose
    sudo curl -L "https://github.com/docker/compose/releases/download/1.28.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && \
    sudo chmod +x /usr/local/bin/docker-compose && \
    echo -e "$(tput setaf 7; tput setab 0)" && \
    docker-compose --version && \
    echo -e "$(tput sgr 0)"
elif [[ $VERSION_ID -ge 11 ]]
then
    echo -e "$(tput setaf 1; tput setab 0)SKIPPING..: Docker-compose ('docker-compose-plugin') is already installed through apt on Debian 11/12$(tput sgr 0)"
else
    echo -e "$(tput setaf 1; tput setab 0)ERR: Unsupported version of debian! Skipping docker-compose installation...$(tput sgr 0)"
fi


echo -e "
$(tput setaf 3; tput setab 0)
    >> # [Deploy-06] - Deployment Script Cleanup
$(tput sgr 0)
"

if ! command -v realpath &> /dev/null
then
    echo -e "COMMAND 'realpath' could not be found. $(tput setaf 3; tput setab 0)Please move script manually.$(tput sgr 0)"
else
    SCRIPT=`realpath -s $0` && \
    DESTINATION=~/Deploy && \
    mkdir -p ${DESTINATION} && \
    mv ${SCRIPT} ${DESTINATION} && \
    ls -laR ${DESTINATION}
fi
