#!/bin/bash
# Install Docker CE
set -euo pipefail

SCRIPTDIR="$(dirname "$0")"
. "$SCRIPTDIR/functions.sh"

set +e
docker_count=$(dpkg -s docker-ce 2>&1 | grep -c "Status:.*installed")
set -e

if [ $docker_count -eq 0 ]; then
  KEYRING=/usr/share/keyrings/docker-archive-keyring.gpg

  curl -fsSL https://download.docker.com/linux/debian/gpg \
    | gpg --dearmor -o $KEYRING

  APT_FILE=/etc/apt/sources.list.d/docker.list
  ARCH=$(dpkg --print-architecture)

  echo "deb [arch=$ARCH signed-by=$KEYRING] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | tee $APT_FILE > /dev/null
  apt-get -q=2 update && apt-get -q=2 -o=Dpkg::Use-Pty=0 -y install \
    docker-ce docker-ce-cli containerd.io docker-compose-plugin
else
  echo "Docker CE - already installed."
fi
