#!/bin/bash


set -euo pipefail

LOG_PIPE=/tmp/log.pipe.$$
mkfifo "${LOG_PIPE}"
LOG_FILE=/root/docker_install.log
touch "${LOG_FILE}"
chmod 600 "${LOG_FILE}"

tee < "${LOG_PIPE}" "${LOG_FILE}" &
TEE_PID=$!

exec > "${LOG_PIPE}"
exec 2> "${LOG_PIPE}"

cleanup() {
	_rc=$?
	if [[ -n "${TEE_PID:-}" ]] && kill -0 "${TEE_PID}" 2>/dev/null; then
		kill "${TEE_PID}" 2>/dev/null || true
		wait "${TEE_PID}" 2>/dev/null || true
	fi
	rm -f "${LOG_PIPE}"
	exit "${_rc}"
}
trap cleanup EXIT INT TERM

# Detect distro
detect_distro() {
	if [ -f /etc/os-release ]; then
		# shellcheck disable=SC1091
		. /etc/os-release
		echo "${ID}"
	else
		echo "unknown"
	fi
}

# Detect distro version
detect_version() {
	if [ -f /etc/os-release ]; then
		# shellcheck disable=SC1091
		. /etc/os-release
		echo "${VERSION_ID}"
	else
		echo "unknown"
	fi
}

DISTRO=$(detect_distro)
VERSION=$(detect_version)

echo "Detected distribution: ${DISTRO} ${VERSION}"

# Remove old Docker installations
echo "Removing old Docker installations if any..."
for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do
	apt-get remove -y "$pkg" 2>/dev/null || true
done

# Install prerequisites
echo "Installing prerequisites..."
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get install -y \
	ca-certificates \
	curl \
	gnupg \
	lsb-release

# Add Docker's official GPG key
echo "Adding Docker GPG key..."
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/${DISTRO}/gpg -o /etc/apt/keyrings/docker.asc
chmod a+r /etc/apt/keyrings/docker.asc

# Add Docker repository
echo "Adding Docker repository..."
case "${DISTRO}" in
	ubuntu)
		echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
			tee /etc/apt/sources.list.d/docker.list > /dev/null
		;;
	debian)
		echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
			tee /etc/apt/sources.list.d/docker.list > /dev/null
		;;
	*)
		echo "ERROR: Unsupported distribution: ${DISTRO}" >&2
		exit 1
		;;
esac

# Install Docker Engine
echo "Installing Docker Engine..."
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get install -y \
	docker-ce \
	docker-ce-cli \
	containerd.io \
	docker-buildx-plugin \
	docker-compose-plugin

# Start and enable Docker service
echo "Starting Docker service..."
systemctl start docker
systemctl enable docker

# Verify Docker installation
echo "Verifying Docker installation..."
if docker --version; then
	echo "Docker installed successfully: $(docker --version)"
else
	echo "ERROR: Docker installation failed" >&2
	exit 1
fi

# Test Docker with hello-world
echo "Testing Docker with hello-world container..."
docker run --rm hello-world

echo ""
echo "================================"
echo "Docker installation complete!"
echo "Docker version: $(docker --version)"
echo "Docker Compose version: $(docker compose version)"
echo "================================"
