#!/bin/bash

# Update and upgrade system packages
echo "Updating system packages..."
sudo apt-get update
sudo apt-get upgrade -y

# Remove old Docker versions if any
echo "Removing old Docker versions..."
for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do
  sudo apt-get remove -y $pkg
done

# Install prerequisites for Docker Compose
echo "Installing prerequisites for Docker Compose..."
sudo apt-get install -y libffi-dev libssl-dev python3 python3-pip

# Install Docker using the official convenience script
echo "Installing Docker..."
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

# Add current user to the Docker group
echo "Adding user to docker group..."
sudo usermod -aG docker $USER

# Enable and start Docker service
echo "Enabling and starting Docker service..."
sudo systemctl enable docker
sudo systemctl start docker

# Install Docker Compose
echo "Installing Docker Compose..."
sudo pip3 install docker-compose

# Verify Docker and Docker Compose versions
echo "Docker version:"
docker --version
echo "Docker Compose version:"
docker-compose --version

echo "Installation complete. Please reboot your Raspberry Pi."
