#!/bin/bash

# Function to print in yellow
print_yellow() {
    tput setaf 3
    echo "$1"
    tput sgr0
}

# Function to print in green
print_green() {
    tput setaf 2
    echo "$1"
    tput sgr0
}

# Function to print in red
print_red() {
    tput setaf 1
    echo "$1"
    tput sgr0
}

# Function to clear temporary files
clear_temp_files() {
    print_yellow "Clearing temporary files..."
    rm -rf /tmp/*
    apt clean
}

# 1. Update & Upgrade the OS
clear
print_yellow "Starting the script..."

print_yellow "Step 1: Updating and upgrading the system..."
apt update && apt upgrade -y
if [ $? -eq 0 ]; then
    print_green "System updated and upgraded successfully."
else
    print_red "Error updating the system. Please check the logs."
    exit 1
fi

# 2. Install Docker from the Official Docker Repository
print_yellow "Step 2: Installing Docker from the official repository..."

# Install dependencies for Docker
apt install -y apt-transport-https ca-certificates curl software-properties-common
if [ $? -eq 0 ]; then
    print_green "Dependencies installed successfully."
else
    print_red "Error installing dependencies."
    exit 1
fi

# Add Docker's official GPG key
curl -fsSL https://download.docker.com/linux/debian/gpg | tee /etc/apt/trusted.gpg.d/docker.asc
if [ $? -eq 0 ]; then
    print_green "Docker GPG key added successfully."
else
    print_red "Error adding Docker GPG key."
    exit 1
fi

# Add Docker's official repository
echo "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list
if [ $? -eq 0 ]; then
    print_green "Docker repository added successfully."
else
    print_red "Error adding Docker repository."
    exit 1
fi

# Install Docker
apt update && apt install -y docker-ce docker-ce-cli containerd.io
if [ $? -eq 0 ]; then
    print_green "Docker installed successfully."
else
    print_red "Error installing Docker."
    exit 1
fi

# Verify Docker installation
docker --version
if [ $? -eq 0 ]; then
    print_green "Docker is running successfully."
else
    print_red "Error verifying Docker installation."
    exit 1
fi

# 3. Install Docker Compose
print_yellow "Step 3: Installing Docker Compose..."

# Install Docker Compose plugin via APT
apt update && apt install -y docker-compose-plugin
if [ $? -eq 0 ]; then
    print_green "Docker Compose installed successfully."
else
    print_red "Error installing Docker Compose."
    exit 1
fi

# Verify Docker Compose installation
docker compose version
if [ $? -eq 0 ]; then
    print_green "Docker Compose is ready."
else
    print_red "Error verifying Docker Compose installation."
    exit 1
fi

# 4. Install Portainer CE (Optional)
print_yellow "Step 4: Do you want to install Portainer CE? (y/n)"
read -r install_portainer

if [[ "$install_portainer" == "y" || "$install_portainer" == "Y" ]]; then
    print_yellow "Installing Portainer CE using Docker Compose..."

    # Create a directory for Portainer
    mkdir -p ~/portainer
    cd ~/portainer || exit

    # Create docker-compose.yml file for Portainer
    cat << EOF > docker-compose.yml
version: "3.9"
services:
  portainer:
    image: portainer/portainer-ce:latest
    container_name: portainer
    restart: always
    networks:
      - portainer_network
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - portainer_data:/data
    ports:
      - 9000:9000
    environment:
      - TZ=Europe/Amsterdam  # Set your timezone

networks:
  portainer_network:
    driver: bridge

volumes:
  portainer_data:
    driver: local
EOF

    if [ $? -eq 0 ]; then
        print_green "Portainer Docker Compose file created successfully."
    else
        print_red "Error creating Portainer Docker Compose file."
        exit 1
    fi

    # Run Portainer using Docker Compose
    docker compose up -d
    if [ $? -eq 0 ]; then
        print_green "Portainer is running successfully."
    else
        print_red "Error starting Portainer."
        exit 1
    fi

    print_yellow "You can access Portainer at http://<your-server-ip>:9000"
else
    print_yellow "Skipping Portainer installation."
fi

# Clear temporary files
clear_temp_files

# Final message
print_green "Script completed successfully."
